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
RevLine 
[b69759d]1import * as drizzleQueries from "../database/drizzle/queries";
[6196d60]2import {ctx, getAuthState, parseSessionUserId, requireUser} from "../server/telefunc/ctx";
[b69759d]3import {Abort} from "telefunc";
[226fbbb]4import type {Database} from "../database/drizzle/db";
[b69759d]5
[226fbbb]6export async function AuthenticationState() {
[5ce5904]7 const context = getAuthState();
[b69759d]8
[5ce5904]9 return context;
[b69759d]10}
11
[226fbbb]12export async function onGetAllComponents({ componentType, limit, q }
[5ce5904]13 : { componentType?: string; limit?: number; q?: string }) {
[b69759d]14 const c = ctx();
15
[5ce5904]16 const components = await drizzleQueries.getAllComponents(c.db, limit, q, componentType);
17
18 return components;
19}
20
[226fbbb]21export async function onGetComponentDetails({ componentId }
[5ce5904]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
[226fbbb]34export async function onSuggestComponent({ link, description, componentType }
[5ce5904]35 : { link: string; description: string; componentType: string }) {
36 const { c, userId } = requireUser()
37
[226fbbb]38 const newSuggestionId = await drizzleQueries.addNewComponentSuggestion(c.db, userId, link, description, componentType);
[5ce5904]39
40 if(!newSuggestionId) throw Abort();
41
42 return newSuggestionId;
[b69759d]43}
44
[226fbbb]45export async function onGetApprovedBuilds({ limit, q }
[b69759d]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
[226fbbb]54export async function onGetHighestRankedBuilds({ limit }
[b69759d]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
[226fbbb]65export async function onGetBuildDetails({ buildId }
[b69759d]66 : { buildId: number }) {
[6196d60]67 const context = ctx();
[b69759d]68
69 if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
70
[6196d60]71 const userId = parseSessionUserId(context.session?.user?.id);
72 const buildDetails = await drizzleQueries.getBuildDetails(context.db, buildId, userId);
[b69759d]73
74 if(!buildDetails) throw Abort();
75
76 return buildDetails;
77}
78
[226fbbb]79export async function onToggleFavorite({ buildId }
[b69759d]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
[226fbbb]90export async function onSetRating({ buildId, value }
[b69759d]91 : { buildId: number; value: number }) {
92 const { c, userId } = requireUser()
93
94 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
95
[5ce5904]96 if (!Number.isInteger(value) || value < 1 || value > 5) throw Abort();
97
[b69759d]98 const result = await drizzleQueries.setBuildRating(c.db, userId, buildId, value);
99
100 return { success: true };
101}
102
[226fbbb]103export async function onSetReview({ buildId, content }
[b69759d]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
[226fbbb]114export async function onCloneBuild({ buildId }
[b69759d]115 : { buildId: number }) {
116 const { c, userId } = requireUser()
117
118 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
119
[226fbbb]120 const newBuild = await drizzleQueries.cloneBuild(c.db, userId, buildId);
[b69759d]121
[226fbbb]122 if (!newBuild) throw Abort();
[b69759d]123
[226fbbb]124 return newBuild;
[b69759d]125}
[226fbbb]126
127
128
129
130
131
Note: See TracBrowser for help on using the repository browser.