| [b69759d] | 1 | import * as drizzleQueries from "../database/drizzle/queries";
|
|---|
| [6196d60] | 2 | import {ctx, getAuthState, parseSessionUserId, requireUser} from "../server/telefunc/ctx";
|
|---|
| [b69759d] | 3 | import {Abort} from "telefunc";
|
|---|
| [226fbbb] | 4 | import type {Database} from "../database/drizzle/db";
|
|---|
| [b69759d] | 5 |
|
|---|
| [67dfe57] | 6 | export async function onGetAuthState() {
|
|---|
| [5ce5904] | 7 | const context = getAuthState();
|
|---|
| [b69759d] | 8 |
|
|---|
| [5ce5904] | 9 | return context;
|
|---|
| [b69759d] | 10 | }
|
|---|
| 11 |
|
|---|
| [226fbbb] | 12 | export async function onGetAllComponents({ componentType, limit, q }
|
|---|
| [5ce5904] | 13 | : { componentType?: string; limit?: number; q?: string }) {
|
|---|
| [b69759d] | 14 | const c = ctx();
|
|---|
| 15 |
|
|---|
| [3fca46c] | 16 | const components = await drizzleQueries.getAllComponents(c.db, limit, componentType, q);
|
|---|
| [5ce5904] | 17 |
|
|---|
| 18 | return components;
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| [226fbbb] | 21 | export async function onGetComponentDetails({ componentId }
|
|---|
| [5ce5904] | 22 | : { componentId: number }) {
|
|---|
| 23 | const context = ctx();
|
|---|
| 24 |
|
|---|
| 25 | if(!Number.isInteger(componentId) || componentId <= 0) throw Abort();
|
|---|
| 26 |
|
|---|
| 27 | const componentDetails = await drizzleQueries.getComponentDetails(context.db, componentId);
|
|---|
| 28 |
|
|---|
| 29 | if(!componentDetails) throw Abort();
|
|---|
| 30 |
|
|---|
| 31 | return componentDetails;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| [226fbbb] | 34 | export async function onSuggestComponent({ link, description, componentType }
|
|---|
| [5ce5904] | 35 | : { link: string; description: string; componentType: string }) {
|
|---|
| 36 | const { c, userId } = requireUser()
|
|---|
| 37 |
|
|---|
| [226fbbb] | 38 | const newSuggestionId = await drizzleQueries.addNewComponentSuggestion(c.db, userId, link, description, componentType);
|
|---|
| [5ce5904] | 39 |
|
|---|
| 40 | if(!newSuggestionId) throw Abort();
|
|---|
| 41 |
|
|---|
| 42 | return newSuggestionId;
|
|---|
| [b69759d] | 43 | }
|
|---|
| 44 |
|
|---|
| [59f8f5a] | 45 | export async function onGetApprovedBuilds({ limit, sort, q }
|
|---|
| [2c6d75a] | 46 | : { limit?: number; sort?: string; q?: string }) {
|
|---|
| [b69759d] | 47 | const context = ctx();
|
|---|
| 48 |
|
|---|
| [59f8f5a] | 49 | const approvedBuilds = await drizzleQueries.getApprovedBuilds(context.db, limit, sort, q);
|
|---|
| [b69759d] | 50 |
|
|---|
| 51 | return approvedBuilds;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | // Shared
|
|---|
| 55 |
|
|---|
| [226fbbb] | 56 | export async function onGetBuildDetails({ buildId }
|
|---|
| [b69759d] | 57 | : { buildId: number }) {
|
|---|
| [6196d60] | 58 | const context = ctx();
|
|---|
| [b69759d] | 59 |
|
|---|
| 60 | if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 61 |
|
|---|
| [6196d60] | 62 | const userId = parseSessionUserId(context.session?.user?.id);
|
|---|
| 63 | const buildDetails = await drizzleQueries.getBuildDetails(context.db, buildId, userId);
|
|---|
| [b69759d] | 64 |
|
|---|
| 65 | if(!buildDetails) throw Abort();
|
|---|
| 66 |
|
|---|
| 67 | return buildDetails;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| [226fbbb] | 70 | export async function onToggleFavorite({ buildId }
|
|---|
| [b69759d] | 71 | : { buildId: number }) {
|
|---|
| 72 | const { c, userId } = requireUser()
|
|---|
| 73 |
|
|---|
| 74 | if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 75 |
|
|---|
| 76 | const isFavorite = await drizzleQueries.toggleFavoriteBuild(c.db, userId, buildId);
|
|---|
| 77 |
|
|---|
| 78 | return isFavorite.favorite;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| [226fbbb] | 81 | export async function onSetRating({ buildId, value }
|
|---|
| [b69759d] | 82 | : { buildId: number; value: number }) {
|
|---|
| 83 | const { c, userId } = requireUser()
|
|---|
| 84 |
|
|---|
| 85 | if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 86 |
|
|---|
| [5ce5904] | 87 | if (!Number.isInteger(value) || value < 1 || value > 5) throw Abort();
|
|---|
| 88 |
|
|---|
| [b69759d] | 89 | const result = await drizzleQueries.setBuildRating(c.db, userId, buildId, value);
|
|---|
| 90 |
|
|---|
| 91 | return { success: true };
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| [226fbbb] | 94 | export async function onSetReview({ buildId, content }
|
|---|
| [b69759d] | 95 | : { buildId: number; content: string }) {
|
|---|
| 96 | const { c, userId } = requireUser()
|
|---|
| 97 |
|
|---|
| 98 | if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 99 |
|
|---|
| 100 | const result = await drizzleQueries.setBuildReview(c.db, userId, buildId, content);
|
|---|
| 101 |
|
|---|
| 102 | return { success: true };
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| [226fbbb] | 105 | export async function onCloneBuild({ buildId }
|
|---|
| [b69759d] | 106 | : { buildId: number }) {
|
|---|
| 107 | const { c, userId } = requireUser()
|
|---|
| 108 |
|
|---|
| 109 | if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 110 |
|
|---|
| [226fbbb] | 111 | const newBuild = await drizzleQueries.cloneBuild(c.db, userId, buildId);
|
|---|
| [b69759d] | 112 |
|
|---|
| [226fbbb] | 113 | if (!newBuild) throw Abort();
|
|---|
| [b69759d] | 114 |
|
|---|
| [226fbbb] | 115 | return newBuild;
|
|---|
| [3fca46c] | 116 | } |
|---|