source: pages/+Layout.telefunc.ts@ ae5d054

main
Last change on this file since ae5d054 was 8fe7f1a, checked in by Tome <gjorgievtome@…>, 6 months ago

Refactor queries and telefunctions

  • Property mode set to 100644
File size: 4.1 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";
5import {addNewBuild} from "../database/drizzle/queries";
6
7export async function onGetAuthState() {
8 const context = getAuthState();
9
10 return context;
11}
12
13export async function onGetAllComponents({ componentType, limit, sort, q }
14 : { componentType?: string; limit?: number; sort?: string, q?: string }) {
15 const c = ctx();
16
17 const components = await drizzleQueries.getAllComponents(c.db, limit, componentType, sort, q);
18
19 return components;
20}
21
22export async function onSuggestComponent({ link, description, componentType }
23 : { link: string; description: string; componentType: string }) {
24 const { c, userId } = requireUser()
25
26 const newSuggestionId = await drizzleQueries.addNewComponentSuggestion(c.db, userId, link, description, componentType);
27
28 if(!newSuggestionId) throw Abort();
29
30 return { success: true };
31}
32
33export async function onGetApprovedBuilds({ limit, sort, q }
34 : { limit?: number; sort?: string; q?: string }) {
35 const context = ctx();
36
37 const approvedBuilds = await drizzleQueries.getApprovedBuilds(context.db, limit, sort, q);
38
39 return approvedBuilds;
40}
41
42export async function onGetComponentDetails({ componentId }
43 : { componentId: number }) {
44 const context = ctx();
45
46 if(!Number.isInteger(componentId) || componentId <= 0) throw Abort();
47
48 const componentDetails = await drizzleQueries.getComponentDetails(context.db, componentId);
49
50 if(!componentDetails) throw Abort();
51
52 return componentDetails;
53}
54
55export async function onGetBuildDetails({ buildId }
56 : { buildId: number }) {
57 const context = ctx();
58
59 if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
60
61 const userId = parseSessionUserId(context.session?.user?.id);
62 const buildDetails = await drizzleQueries.getBuildDetails(context.db, buildId, userId);
63
64 if(!buildDetails) throw Abort();
65
66 return buildDetails;
67}
68
69export async function onToggleFavorite({ buildId }
70 : { buildId: number }) {
71 const { c, userId } = requireUser()
72
73 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
74
75 const isFavorite = await drizzleQueries.toggleFavoriteBuild(c.db, userId, buildId);
76
77 return isFavorite;
78}
79
80export async function onSetRating({ buildId, value }
81 : { buildId: number; value: number }) {
82 const { c, userId } = requireUser()
83
84 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
85
86 if (!Number.isInteger(value) || value < 1 || value > 5) throw Abort();
87
88 const result = await drizzleQueries.setBuildRating(c.db, userId, buildId, value);
89
90 return { success: true };
91}
92
93export async function onSetReview({ buildId, content }
94 : { buildId: number; content: string }) {
95 const { c, userId } = requireUser()
96
97 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
98
99 const result = await drizzleQueries.setBuildReview(c.db, userId, buildId, content);
100
101 return { success: true };
102}
103
104export async function onCloneBuild({ buildId }
105 : { buildId: number }) {
106 const { c, userId } = requireUser()
107
108 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
109
110 const clonedBuildId = await drizzleQueries.cloneBuild(c.db, userId, buildId);
111
112 if (!clonedBuildId) throw Abort();
113
114 return clonedBuildId;
115}
116
117export async function onAddNewBuild({ name, description }
118 : { name: string; description: string }) {
119 const { c, userId } = requireUser()
120
121 const newBuildId = await drizzleQueries.addNewBuild(c.db, userId, name, description);
122
123 if (!newBuildId) throw Abort();
124
125 return newBuildId;
126}
127
Note: See TracBrowser for help on using the repository browser.