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

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

Fix some issues

  • Property mode set to 100644
File size: 3.7 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
[67dfe57]6export async function onGetAuthState() {
[5ce5904]7 const context = getAuthState();
[b69759d]8
[5ce5904]9 return context;
[b69759d]10}
11
[9854393]12export async function onGetAllComponents({ componentType, limit, sort, q }
13 : { componentType?: string; limit?: number; sort?: string, q?: string }) {
[b69759d]14 const c = ctx();
15
[9854393]16 const components = await drizzleQueries.getAllComponents(c.db, limit, componentType, sort, q);
[5ce5904]17
18 return components;
19}
20
[226fbbb]21export async function onSuggestComponent({ link, description, componentType }
[5ce5904]22 : { link: string; description: string; componentType: string }) {
23 const { c, userId } = requireUser()
24
[226fbbb]25 const newSuggestionId = await drizzleQueries.addNewComponentSuggestion(c.db, userId, link, description, componentType);
[5ce5904]26
27 if(!newSuggestionId) throw Abort();
28
[83fb5e2]29 return { success: true };
[b69759d]30}
31
[59f8f5a]32export async function onGetApprovedBuilds({ limit, sort, q }
[2c6d75a]33 : { limit?: number; sort?: string; q?: string }) {
[b69759d]34 const context = ctx();
35
[59f8f5a]36 const approvedBuilds = await drizzleQueries.getApprovedBuilds(context.db, limit, sort, q);
[b69759d]37
38 return approvedBuilds;
39}
40
[f14dc71]41export async function onGetComponentDetails({ componentId }
42 : { componentId: number }) {
43 const context = ctx();
44
45 if(!Number.isInteger(componentId) || componentId <= 0) throw Abort();
46
47 const componentDetails = await drizzleQueries.getComponentDetails(context.db, componentId);
48
49 if(!componentDetails) throw Abort();
50
51 return componentDetails;
52}
53
[226fbbb]54export async function onGetBuildDetails({ buildId }
[b69759d]55 : { buildId: number }) {
[6196d60]56 const context = ctx();
[b69759d]57
58 if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
59
[6196d60]60 const userId = parseSessionUserId(context.session?.user?.id);
61 const buildDetails = await drizzleQueries.getBuildDetails(context.db, buildId, userId);
[b69759d]62
63 if(!buildDetails) throw Abort();
64
65 return buildDetails;
66}
67
[226fbbb]68export async function onToggleFavorite({ buildId }
[b69759d]69 : { buildId: number }) {
70 const { c, userId } = requireUser()
71
72 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
73
74 const isFavorite = await drizzleQueries.toggleFavoriteBuild(c.db, userId, buildId);
75
[83fb5e2]76 return isFavorite;
[b69759d]77}
78
[226fbbb]79export async function onSetRating({ buildId, value }
[b69759d]80 : { buildId: number; value: number }) {
81 const { c, userId } = requireUser()
82
83 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
84
[5ce5904]85 if (!Number.isInteger(value) || value < 1 || value > 5) throw Abort();
86
[b69759d]87 const result = await drizzleQueries.setBuildRating(c.db, userId, buildId, value);
88
89 return { success: true };
90}
91
[226fbbb]92export async function onSetReview({ buildId, content }
[b69759d]93 : { buildId: number; content: string }) {
94 const { c, userId } = requireUser()
95
96 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
97
98 const result = await drizzleQueries.setBuildReview(c.db, userId, buildId, content);
99
100 return { success: true };
101}
102
[226fbbb]103export async function onCloneBuild({ buildId }
[b69759d]104 : { buildId: number }) {
105 const { c, userId } = requireUser()
106
107 if (!Number.isInteger(buildId) || buildId <= 0) throw Abort();
108
[226fbbb]109 const newBuild = await drizzleQueries.cloneBuild(c.db, userId, buildId);
[b69759d]110
[226fbbb]111 if (!newBuild) throw Abort();
[b69759d]112
[226fbbb]113 return newBuild;
[3fca46c]114}
Note: See TracBrowser for help on using the repository browser.