source: pages/+Layout.telefunc.ts@ 6196d60

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

Modify getAuthState()

  • Property mode set to 100644
File size: 3.9 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 AuthenticationState() {
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, q, componentType);
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, q }
46 : { limit?: number; q?: string }) {
47 const context = ctx();
48
49 const approvedBuilds = await drizzleQueries.getApprovedBuilds(context.db, limit, q);
50
51 return approvedBuilds;
52}
53
54export async function onGetHighestRankedBuilds({ limit }
55 : { limit?: number }) {
56 const context = ctx();
57
58 const highestRankedBuilds = await drizzleQueries.getHighestRankedBuilds(context.db, limit);
59
60 return highestRankedBuilds;
61}
62
63// Shared
64
65export async function onGetBuildDetails({ buildId }
66 : { buildId: number }) {
67 const context = ctx();
68
69 if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
70
71 const userId = parseSessionUserId(context.session?.user?.id);
72 const buildDetails = await drizzleQueries.getBuildDetails(context.db, buildId, userId);
73
74 if(!buildDetails) throw Abort();
75
76 return buildDetails;
77}
78
79export async function onToggleFavorite({ buildId }
80 : { buildId: number }) {
81 const { c, userId } = requireUser()
82
83 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
84
85 const isFavorite = await drizzleQueries.toggleFavoriteBuild(c.db, userId, buildId);
86
87 return isFavorite.favorite;
88}
89
90export async function onSetRating({ buildId, value }
91 : { buildId: number; value: number }) {
92 const { c, userId } = requireUser()
93
94 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
95
96 if (!Number.isInteger(value) || value < 1 || value > 5) throw Abort();
97
98 const result = await drizzleQueries.setBuildRating(c.db, userId, buildId, value);
99
100 return { success: true };
101}
102
103export async function onSetReview({ buildId, content }
104 : { buildId: number; content: string }) {
105 const { c, userId } = requireUser()
106
107 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
108
109 const result = await drizzleQueries.setBuildReview(c.db, userId, buildId, content);
110
111 return { success: true };
112}
113
114export async function onCloneBuild({ buildId }
115 : { buildId: number }) {
116 const { c, userId } = requireUser()
117
118 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
119
120 const newBuild = await drizzleQueries.cloneBuild(c.db, userId, buildId);
121
122 if (!newBuild) throw Abort();
123
124 return newBuild;
125}
126
127
128
129
130
131
Note: See TracBrowser for help on using the repository browser.