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

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

implement components query and filtering

  • 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, 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
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
43export async function onGetComponentDetails({ componentId }
44 : { componentId: number }) {
45 const context = ctx();
46
47 if(!Number.isInteger(componentId) || componentId <= 0) throw Abort();
48
49 const componentDetails = await drizzleQueries.getComponentDetails(context.db, componentId);
50
51 if(!componentDetails) throw Abort();
52
53 return componentDetails;
54}
55
56export async function onGetBuildDetails({ buildId }
57 : { buildId: number }) {
58 const context = ctx();
59
60 if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
61
62 const userId = parseSessionUserId(context.session?.user?.id);
63 const buildDetails = await drizzleQueries.getBuildDetails(context.db, buildId, userId);
64
65 if(!buildDetails) throw Abort();
66
67 return buildDetails;
68}
69
70export async function onToggleFavorite({ buildId }
71 : { buildId: number }) {
72 const { c, userId } = requireUser()
73
74 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
75
76 const isFavorite = await drizzleQueries.toggleFavoriteBuild(c.db, userId, buildId);
77
78 return isFavorite;
79}
80
81export async function onSetRating({ buildId, value }
82 : { buildId: number; value: number }) {
83 const { c, userId } = requireUser()
84
85 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
86
87 if (!Number.isInteger(value) || value < 1 || value > 5) throw Abort();
88
89 const result = await drizzleQueries.setBuildRating(c.db, userId, buildId, value);
90
91 return { success: true };
92}
93
94export async function onSetReview({ buildId, content }
95 : { buildId: number; content: string }) {
96 const { c, userId } = requireUser()
97
98 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
99
100 const result = await drizzleQueries.setBuildReview(c.db, userId, buildId, content);
101
102 return { success: true };
103}
104
105export async function onCloneBuild({ buildId }
106 : { buildId: number }) {
107 const { c, userId } = requireUser()
108
109 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
110
111 const newBuild = await drizzleQueries.cloneBuild(c.db, userId, buildId);
112
113 if (!newBuild) throw Abort();
114
115 return newBuild;
116}
Note: See TracBrowser for help on using the repository browser.