| [226fbbb] | 1 | import * as drizzleQueries from "../../database/drizzle/queries";
|
|---|
| 2 | import {ctx, getAuthState, requireUser} from "../../server/telefunc/ctx";
|
|---|
| 3 | import {Abort} from "telefunc";
|
|---|
| 4 | import type {Database} from "../../database/drizzle/db";
|
|---|
| [e7f1bfa] | 5 | import {removeComponentFromBuild} from "../../database/drizzle/queries";
|
|---|
| [226fbbb] | 6 |
|
|---|
| [8fe7f1a] | 7 | export async function onRemoveComponentFromBuild({ buildId, componentId }
|
|---|
| 8 | : { buildId: number, componentId: number }) {
|
|---|
| [226fbbb] | 9 | const { c, userId } = requireUser()
|
|---|
| 10 |
|
|---|
| [8fe7f1a] | 11 | const result = await drizzleQueries.removeComponentFromBuild(c.db, userId, buildId, componentId);
|
|---|
| [226fbbb] | 12 |
|
|---|
| [8fe7f1a] | 13 | if(!result) throw Abort();
|
|---|
| [226fbbb] | 14 |
|
|---|
| 15 | return { success: true };
|
|---|
| [e7f1bfa] | 16 | }
|
|---|
| 17 |
|
|---|
| [8fe7f1a] | 18 | export async function onAddComponentToBuild({ buildId, componentId }: { buildId: number, componentId: number }) {
|
|---|
| [e7f1bfa] | 19 | const { c, userId } = requireUser()
|
|---|
| 20 |
|
|---|
| [8fe7f1a] | 21 | if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 22 | if(!Number.isInteger(componentId) || componentId <= 0) throw Abort();
|
|---|
| 23 |
|
|---|
| 24 | const result = await drizzleQueries.addComponentToBuild(c.db, userId, buildId, componentId);
|
|---|
| [e7f1bfa] | 25 |
|
|---|
| 26 | if(!result) throw Abort();
|
|---|
| 27 |
|
|---|
| 28 | return { success: true };
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | export async function onDeleteBuild({ buildId }: { buildId: number }) {
|
|---|
| 32 | const { c, userId } = requireUser()
|
|---|
| 33 |
|
|---|
| 34 | const result = await drizzleQueries.deleteBuild(c.db, userId, buildId);
|
|---|
| 35 | if(!result) throw Abort();
|
|---|
| 36 |
|
|---|
| 37 | return { success: true };
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | export async function onGetBuildComponents({ buildId }
|
|---|
| 41 | : { buildId: number }) {
|
|---|
| 42 | const { c, userId } = requireUser()
|
|---|
| 43 |
|
|---|
| 44 | if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 45 |
|
|---|
| 46 | const buildComponents = await drizzleQueries.getBuildComponents(c.db, buildId);
|
|---|
| 47 |
|
|---|
| 48 | if(!buildComponents) throw Abort();
|
|---|
| 49 |
|
|---|
| 50 | return buildComponents;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| [8fe7f1a] | 53 | export async function onGetCompatibleComponents({ buildId, componentType, limit, sort }
|
|---|
| 54 | : { buildId: number, componentType: string, limit?: number, sort?: string }) {
|
|---|
| [e7f1bfa] | 55 | const { c, userId } = requireUser()
|
|---|
| 56 |
|
|---|
| 57 | if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 58 |
|
|---|
| [8fe7f1a] | 59 | const compatibleComponents = await drizzleQueries.getCompatibleComponents(c.db, buildId, componentType, limit, sort);
|
|---|
| [e7f1bfa] | 60 |
|
|---|
| [8fe7f1a] | 61 | if(!compatibleComponents) throw Abort();
|
|---|
| [e7f1bfa] | 62 |
|
|---|
| [8fe7f1a] | 63 | return compatibleComponents;
|
|---|
| [e7f1bfa] | 64 | }
|
|---|
| 65 |
|
|---|
| [8fe7f1a] | 66 | export async function onGetBuildState({ db, buildId}
|
|---|
| 67 | :{ db: Database, buildId: number }) {
|
|---|
| [e7f1bfa] | 68 | const { c, userId } = requireUser()
|
|---|
| 69 |
|
|---|
| 70 | if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 71 |
|
|---|
| [8fe7f1a] | 72 | const buildState = await drizzleQueries.getBuildState(c.db, userId, buildId);
|
|---|
| [e7f1bfa] | 73 |
|
|---|
| [8fe7f1a] | 74 | if(!buildState) throw Abort();
|
|---|
| [e7f1bfa] | 75 |
|
|---|
| [8fe7f1a] | 76 | return buildState;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | export async function saveBuildState({ db, buildId, name, description }
|
|---|
| 80 | :{ db: Database, buildId: number, name: string, description: string }) {
|
|---|
| 81 | const { c, userId } = requireUser()
|
|---|
| 82 |
|
|---|
| 83 | if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 84 |
|
|---|
| 85 | const result = await drizzleQueries.saveBuildState(c.db, userId, buildId, name, description);
|
|---|
| 86 |
|
|---|
| 87 | if(!result) throw Abort();
|
|---|
| 88 |
|
|---|
| 89 | return { success: true };
|
|---|
| [226fbbb] | 90 | } |
|---|