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