source: pages/+Layout.telefunc.ts@ 83fb5e2

main
Last change on this file since 83fb5e2 was 83fb5e2, checked in by Tome <gjorgievtome@…>, 7 months ago

fix returns and add checks

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