source: pages/+Layout.telefunc.ts@ 226fbbb

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

Update telefunctions

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[b69759d]1import * as drizzleQueries from "../database/drizzle/queries";
2import {ctx, getAuthState, requireUser} from "../server/telefunc/ctx";
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 }) {
[5ce5904]67 const context = getAuthState();
[b69759d]68
69 if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
70
[5ce5904]71 const buildDetails = await drizzleQueries.getBuildDetails(context.db, buildId, context.userId ?? undefined);
[b69759d]72
73 if(!buildDetails) throw Abort();
74
75 return buildDetails;
76}
77
[226fbbb]78export async function onToggleFavorite({ buildId }
[b69759d]79 : { buildId: number }) {
80 const { c, userId } = requireUser()
81
82 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
83
84 const isFavorite = await drizzleQueries.toggleFavoriteBuild(c.db, userId, buildId);
85
86 return isFavorite.favorite;
87}
88
[226fbbb]89export async function onSetRating({ buildId, value }
[b69759d]90 : { buildId: number; value: number }) {
91 const { c, userId } = requireUser()
92
93 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
94
[5ce5904]95 if (!Number.isInteger(value) || value < 1 || value > 5) throw Abort();
96
[b69759d]97 const result = await drizzleQueries.setBuildRating(c.db, userId, buildId, value);
98
99 return { success: true };
100}
101
[226fbbb]102export async function onSetReview({ buildId, content }
[b69759d]103 : { buildId: number; content: string }) {
104 const { c, userId } = requireUser()
105
106 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
107
108 const result = await drizzleQueries.setBuildReview(c.db, userId, buildId, content);
109
110 return { success: true };
111}
112
[226fbbb]113export async function onCloneBuild({ buildId }
[b69759d]114 : { buildId: number }) {
115 const { c, userId } = requireUser()
116
117 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
118
[226fbbb]119 const newBuild = await drizzleQueries.cloneBuild(c.db, userId, buildId);
[b69759d]120
[226fbbb]121 if (!newBuild) throw Abort();
[b69759d]122
[226fbbb]123 return newBuild;
[b69759d]124}
[226fbbb]125
126
127
128
129
130
Note: See TracBrowser for help on using the repository browser.