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