source: pages/+Layout.telefunc.ts@ 2c6d75a

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

add optional param

  • 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
21export 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
34export 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
45export async function onGetApprovedBuilds({ limit, sort, q }
46 : { limit?: number; sort?: string; q?: string }) {
47 const context = ctx();
48
49 const approvedBuilds = await drizzleQueries.getApprovedBuilds(context.db, limit, sort, q);
50
51 return approvedBuilds;
52}
53
54// Shared
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.favorite;
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.