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
Line 
1import * as drizzleQueries from "../database/drizzle/queries";
2import {ctx, getAuthState, parseSessionUserId, requireUser} from "../server/telefunc/ctx";
3import {Abort} from "telefunc";
4import type {Database} from "../database/drizzle/db";
5
6export async function onGetAuthState() {
7 const context = getAuthState();
8
9 return context;
10}
11
12export 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, componentType, q);
17
18 return components;
19}
20
21
22
23export async function onSuggestComponent({ link, description, componentType }
24 : { link: string; description: string; componentType: string }) {
25 const { c, userId } = requireUser()
26
27 const newSuggestionId = await drizzleQueries.addNewComponentSuggestion(c.db, userId, link, description, componentType);
28
29 if(!newSuggestionId) throw Abort();
30
31 return { success: true };
32}
33
34export async function onGetApprovedBuilds({ limit, sort, q }
35 : { limit?: number; sort?: string; q?: string }) {
36 const context = ctx();
37
38 const approvedBuilds = await drizzleQueries.getApprovedBuilds(context.db, limit, sort, q);
39
40 return approvedBuilds;
41}
42
43// Shared
44
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
58export async function onGetBuildDetails({ buildId }
59 : { buildId: number }) {
60 const context = ctx();
61
62 if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
63
64 const userId = parseSessionUserId(context.session?.user?.id);
65 const buildDetails = await drizzleQueries.getBuildDetails(context.db, buildId, userId);
66
67 if(!buildDetails) throw Abort();
68
69 return buildDetails;
70}
71
72export async function onToggleFavorite({ buildId }
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
80 return isFavorite;
81}
82
83export async function onSetRating({ buildId, value }
84 : { buildId: number; value: number }) {
85 const { c, userId } = requireUser()
86
87 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
88
89 if (!Number.isInteger(value) || value < 1 || value > 5) throw Abort();
90
91 const result = await drizzleQueries.setBuildRating(c.db, userId, buildId, value);
92
93 return { success: true };
94}
95
96export async function onSetReview({ buildId, content }
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
107export async function onCloneBuild({ buildId }
108 : { buildId: number }) {
109 const { c, userId } = requireUser()
110
111 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
112
113 const newBuild = await drizzleQueries.cloneBuild(c.db, userId, buildId);
114
115 if (!newBuild) throw Abort();
116
117 return newBuild;
118}
Note: See TracBrowser for help on using the repository browser.