Changeset e09fe6f


Ignore:
Timestamp:
12/28/25 03:14:12 (7 months ago)
Author:
Tome <gjorgievtome@…>
Branches:
main
Children:
67186d2
Parents:
3216215
Message:

Properly implement onAddNewBuild

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • database/drizzle/queries/builds.ts

    r3216215 re09fe6f  
    495495}
    496496
    497 export async function addNewBuild(db: Database, userId: number, name: string, description: string, componentIds: number[]) {
    498     return db.transaction(async (tx) => {
    499         const components = await tx
    500             .select({
    501                 price:  componentsTable.price
    502             })
    503             .from(componentsTable)
    504             .where(
    505                 inArray(componentsTable.id, componentIds)
    506             );
    507 
    508         const totalPrice = components.reduce((sum, c) => sum + Number(c.price), 0);
    509 
    510         const [newBuild] = await tx
    511             .insert(buildsTable)
    512             .values({
    513                 userId: userId,
    514                 name: name,
    515                 createdAt: new Date().toISOString().split('T')[0],
    516                 description: description,
    517                 totalPrice: totalPrice.toFixed(2),
    518                 isApproved: false
    519             })
    520             .returning({
    521                 id: buildsTable.id
    522             });
    523 
    524         if(components.length) {
    525             await tx.insert(buildComponentsTable)
    526                 .values(
    527                     componentIds.map(componentId => ({
    528                         buildId: newBuild.id,
    529                         componentId: componentId
    530                     }))
    531                 );
    532         }
    533 
    534         return newBuild?.id ?? null;
    535     });
     497export async function addNewBuild(db: Database, userId: number, name: string, description: string) {
     498    const [newBuild] = await db
     499        .insert(buildsTable)
     500        .values({
     501            userId: userId,
     502            name: name,
     503            createdAt: new Date().toISOString().split('T')[0],
     504            description: description,
     505            totalPrice: Number(0).toFixed(2),
     506            isApproved: false
     507        })
     508        .returning({
     509            id: buildsTable.id
     510        });
     511
     512    return newBuild?.id ?? null;
    536513}
    537514
  • pages/+Layout.telefunc.ts

    r3216215 re09fe6f  
    33import {Abort} from "telefunc";
    44import type {Database} from "../database/drizzle/db";
     5import {addNewBuild} from "../database/drizzle/queries";
    56
    67export async function onGetAuthState() {
     
    113114    return newBuild;
    114115}
     116
     117export async function onAddNewBuild({ name, description }
     118                                          : { name: string; description: string }) {
     119    const { c, userId } = requireUser()
     120
     121    const newBuildId = await drizzleQueries.addNewBuild(c.db, userId, name, description);
     122
     123    if (!newBuildId) throw Abort();
     124
     125    return { buildId: newBuildId };
     126}
Note: See TracChangeset for help on using the changeset viewer.