| [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 |
|
|---|
| 7 | export async function onSaveNewBuild({ name, description, componentIds }
|
|---|
| 8 | : { name: string; description: string; componentIds: number[] }) {
|
|---|
| 9 | const { c, userId } = requireUser()
|
|---|
| 10 |
|
|---|
| 11 | const newBuildId = await drizzleQueries.addNewBuild(c.db, userId, name, description, componentIds);
|
|---|
| 12 |
|
|---|
| 13 | if(!newBuildId) throw Abort();
|
|---|
| 14 |
|
|---|
| 15 | return { success: true };
|
|---|
| [e7f1bfa] | 16 | }
|
|---|
| 17 |
|
|---|
| 18 | export async function onRemoveComponentFromBuild({ buildId, componentId }
|
|---|
| 19 | : { buildId: number, componentId: number }) {
|
|---|
| 20 | const { c, userId } = requireUser()
|
|---|
| 21 |
|
|---|
| 22 | const result = await drizzleQueries.removeComponentFromBuild(c.db, buildId, componentId);
|
|---|
| 23 |
|
|---|
| 24 | if(!result) throw Abort();
|
|---|
| 25 |
|
|---|
| 26 | return { success: true };
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | export async function onDeleteBuild({ buildId }: { buildId: number }) {
|
|---|
| 30 | const { c, userId } = requireUser()
|
|---|
| 31 |
|
|---|
| 32 | const result = await drizzleQueries.deleteBuild(c.db, userId, buildId);
|
|---|
| 33 | if(!result) throw Abort();
|
|---|
| 34 |
|
|---|
| 35 | return { success: true };
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | export async function onGetBuildComponents({ buildId }
|
|---|
| 39 | : { buildId: number }) {
|
|---|
| 40 | const { c, userId } = requireUser()
|
|---|
| 41 |
|
|---|
| 42 | if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 43 |
|
|---|
| 44 | const buildComponents = await drizzleQueries.getBuildComponents(c.db, buildId);
|
|---|
| 45 |
|
|---|
| 46 | if(!buildComponents) throw Abort();
|
|---|
| 47 |
|
|---|
| 48 | return buildComponents;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | export async function onAddComponentToBuild({ buildId, componentId }: { buildId: number, componentId: number }) {
|
|---|
| 52 | const { c, userId } = requireUser()
|
|---|
| 53 |
|
|---|
| 54 | if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 55 | if(!Number.isInteger(componentId) || componentId <= 0) throw Abort();
|
|---|
| 56 |
|
|---|
| 57 | const result = await drizzleQueries.addComponentToBuild(c.db, buildId, componentId);
|
|---|
| 58 |
|
|---|
| 59 | if(!result) throw Abort();
|
|---|
| 60 |
|
|---|
| 61 | return { success: true };
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | export async function onGetCompatibleComponents({ buildId, componentType }: { buildId: number, componentType: string }) {
|
|---|
| 65 | const { c, userId } = requireUser()
|
|---|
| 66 |
|
|---|
| 67 | if(!Number.isInteger(buildId) || buildId <= 0) throw Abort();
|
|---|
| 68 |
|
|---|
| 69 | const compatibleComponents = await drizzleQueries.getCompatibleComponents(c.db, buildId, componentType);
|
|---|
| 70 |
|
|---|
| 71 | if(!compatibleComponents) throw Abort();
|
|---|
| 72 |
|
|---|
| 73 | return compatibleComponents;
|
|---|
| [226fbbb] | 74 | } |
|---|