Changeset 83fb5e2


Ignore:
Timestamp:
12/25/25 01:49:51 (7 months ago)
Author:
Tome <gjorgievtome@…>
Branches:
main
Children:
e7f1bfa
Parents:
d4842f4
Message:

fix returns and add checks

Files:
7 edited

Legend:

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

    rd4842f4 r83fb5e2  
    4646
    4747export async function setBuildApprovalStatus(db: Database, buildId: number, isApproved: boolean){
    48     const result = await db
     48    const [result] = await db
    4949        .update(buildsTable)
    5050        .set({
     
    6161        })
    6262
    63     return result.length;
     63    return result?.id ?? null;
    6464}
    6565
     
    135135            created_at: buildsTable.createdAt,
    136136            total_price: buildsTable.totalPrice,
    137             avgRating: sql<number>`AVG(${ratingBuildsTable.value}::float)`
     137            avgRating: sql<number>`COALESCE(AVG(${ratingBuildsTable.value}::float),0)`
    138138        })
    139139        .from(buildsTable)
     
    160160
    161161    return approvedBuildsList;
    162 }
    163 
    164 export async function getHighestRankedBuilds(db: Database, limit? : number) {
    165     const highestRankedBuildsList = await db
    166         .select({
    167             id: buildsTable.id,
    168             user_id: buildsTable.userId,
    169             name: buildsTable.name,
    170             created_at: buildsTable.createdAt,
    171             total_price: buildsTable.totalPrice,
    172             avgRating: sql<number>`AVG(${ratingBuildsTable.value}::float)`
    173         })
    174         .from(buildsTable)
    175         .innerJoin(
    176             ratingBuildsTable,
    177             eq(buildsTable.id, ratingBuildsTable.buildId)
    178         )
    179         .where(
    180             eq(buildsTable.isApproved, true)
    181         )
    182         .groupBy(buildsTable.id)
    183         .orderBy(
    184             desc(sql<number>`AVG(${ratingBuildsTable.value}::float)`)
    185         )
    186         .limit(limit || 100); // 100 placeholder
    187 
    188     return highestRankedBuildsList;
    189162}
    190163
     
    335308        .limit(1);
    336309
    337     if (existing.length) {
     310    if (existing.length > 0) {
    338311        await db
    339312            .delete(favoriteBuildsTable)
     
    345318            );
    346319
    347         return { favorite: false };
     320        return null;
    348321    }
    349322
     
    355328        });
    356329
    357     return { favorite: true };
     330    return true;
    358331}
    359332
     
    378351        })
    379352
    380     return result;
     353    return result ?? null;
    381354}
    382355
     
    404377        })
    405378
    406     return result;
     379    return result ?? null;
    407380}
    408381
     
    498471
    499472export async function deleteBuild(db: Database, userId: number, buildId: number) {
    500     const result = await db
     473    const [result] = await db
    501474        .delete(buildsTable)
    502475        .where(
     
    510483        })
    511484
    512     return result.length;
     485    return result?.id ?? null;
    513486}
    514487
     
    550523        }
    551524
    552         return newBuild.id;
     525        return newBuild?.id ?? null;
    553526    });
    554527}
  • database/drizzle/queries/components.ts

    rd4842f4 r83fb5e2  
    11import type { Database } from "../db";
    22import {
     3    buildComponentsTable,
    34    buildsTable, cablesTable, caseMoboFormFactorsTable, casePsFormFactorsTable, caseStorageFormFactorsTable,
    45    componentsTable, coolerCPUSocketsTable, coolersTable,
     
    78    memoryTable, motherboardsTable,
    89    networkAdaptersTable,
    9     networkCardsTable, opticalDrivesTable, pcCasesTable, powerSupplyTable, soundCardsTable, storageTable
     10    networkCardsTable, opticalDrivesTable, pcCasesTable, powerSupplyTable,
     11    ratingBuildsTable, soundCardsTable, storageTable
    1012} from "../schema";
    1113import {and, desc, eq, ilike} from "drizzle-orm";
    12 
    13 export async function getAllComponents(db: Database, limit?: number,  q?: string, componentType?: string) {
     14export async function getAllComponents(db: Database, limit?: number, componentType?: string, q?: string) {
    1415    let queryConditions = [];
    1516
     
    5960
    6061    if(!component) return null;
    61 
    62     if (!component) return null;
    6362
    6463    let details: any = {};
     
    247246                    numRamSlots: specificData.numRamSlots,
    248247                    maxRamCapacity: specificData.maxRamCapacity,
     248                    pciExpressSlots: specificData.pciExpressSlots,
    249249                });
    250250                break;
     
    322322    });
    323323}
     324
     325export async function getCompatibleComponents(db: Database, buildId: number, componentType: string) {
     326    return db.transaction(async (tx) => {
     327        const [build] = await tx
     328            .select({
     329                buildId: buildsTable.id,
     330                userId: buildsTable.userId,
     331            })
     332            .from(buildsTable)
     333            .where(
     334                eq(buildsTable.id, buildId)
     335            )
     336            .limit(1);
     337
     338        if(!build) return null;
     339
     340
     341    return null;
     342    });
     343}
     344
     345export async function addComponentToBuild(db: Database, buildId: number, componentId: number) {
     346    const [build] = await db
     347        .select()
     348        .from(buildsTable)
     349        .where(
     350            eq(buildsTable.id, buildId)
     351        )
     352        .limit(1);
     353
     354    if(!build) return null;
     355
     356    const [component] = await db
     357        .select()
     358        .from(componentsTable)
     359        .where(
     360            eq(componentsTable.id, componentId)
     361        )
     362        .limit(1);
     363
     364    if(!component) return null;
     365
     366    const existing = await db
     367        .select()
     368        .from(buildComponentsTable)
     369        .where(
     370            and(
     371                eq(buildComponentsTable.buildId, buildId),
     372                eq(buildComponentsTable.componentId, componentId)
     373            )
     374        )
     375        .limit(1);
     376
     377    if(existing.length > 0) return null;
     378
     379    const [result] = await db
     380        .insert(buildComponentsTable)
     381        .values({
     382            buildId,
     383            componentId
     384        })
     385        .returning({
     386            id: buildComponentsTable.buildId
     387        });
     388
     389    const buildComponents = await db
     390        .select({
     391            price:  componentsTable.price,
     392        })
     393        .from(buildComponentsTable)
     394        .innerJoin(
     395            componentsTable,
     396            eq(buildComponentsTable.componentId, componentsTable.id)
     397        )
     398        .where(
     399            eq(buildComponentsTable.buildId, buildId)
     400        );
     401
     402    const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0);
     403
     404     await db
     405        .update(buildsTable)
     406        .set({
     407            totalPrice: totalPrice.toFixed(2)
     408        })
     409        .where(
     410            eq(buildsTable.id, buildId)
     411        );
     412
     413    return result?.id ?? null;
     414}
     415
     416export async function removeComponentFromBuild(db: Database, buildId: number, componentId: number) {
     417    const [build] = await db
     418        .select()
     419        .from(buildsTable)
     420        .where(
     421            eq(buildsTable.id, buildId)
     422        )
     423        .limit(1);
     424
     425    if(!build) return null;
     426
     427    const [component] = await db
     428        .select()
     429        .from(componentsTable)
     430        .where(
     431            eq(componentsTable.id, componentId)
     432        )
     433        .limit(1);
     434
     435    if(!component) return null;
     436
     437    const result = await db
     438        .delete(buildComponentsTable)
     439        .where(
     440            and(
     441                eq(buildComponentsTable.buildId, buildId),
     442                eq(buildComponentsTable.componentId, componentId)
     443            )
     444        );
     445
     446    if(result.rowCount === 0) return null;
     447
     448    const buildComponents = await db
     449        .select({
     450            price:  componentsTable.price,
     451        })
     452        .from(buildComponentsTable)
     453        .innerJoin(
     454            componentsTable,
     455            eq(buildComponentsTable.componentId, componentsTable.id)
     456        )
     457        .where(
     458            eq(buildComponentsTable.buildId, buildId)
     459        );
     460
     461    const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0);
     462
     463    await db
     464        .update(buildsTable)
     465        .set({
     466            totalPrice: totalPrice.toFixed(2)
     467        })
     468        .where(
     469            eq(buildsTable.id, buildId)
     470        );
     471
     472    return result;
     473}
     474
  • database/drizzle/queries/users.ts

    rd4842f4 r83fb5e2  
    44
    55export async function createUser(db: Database, username: string, email: string, passwordHash: string) {
    6     await db
     6    const [newUser] = await db
    77        .insert(usersTable)
    88        .values({
     
    1010            email: email,
    1111            passwordHash: passwordHash,
     12        })
     13        .returning({
     14            id: usersTable.id,
    1215        });
     16
     17    return newUser?.id ?? null;
    1318}
    1419
     
    2934
    3035export async function isAdmin(db: Database, userId: number) {
    31     const admin = await db
     36    const [admin] = await db
    3237        .selectDistinct()
    3338        .from(adminsTable)
     
    3742        .limit(1);
    3843
    39     return admin.length ?? null;
     44    return !!admin;
    4045}
    4146
     
    5560
    5661export async function setComponentSuggestionStatus(db: Database, suggestionId: number, adminId: number, status: string, adminComment: string) {
    57     const result = await db
     62    const [result] = await db
    5863        .update(suggestionsTable)
    5964        .set({
     
    6469        .where(
    6570            eq(suggestionsTable.id, suggestionId)
    66         );
     71        )
     72        .returning({
     73            id: suggestionsTable.id
     74        });
    6775
    68     return result.rowCount ?? null;
     76    return result?.id ?? null;
    6977}
    7078
     
    7886            componentType: componentType
    7987        })
    80         .returning({ id: suggestionsTable.id });
     88        .returning({
     89            id: suggestionsTable.id
     90        });
    8191
    82     return newSuggestion.id ?? null;
     92    return newSuggestion?.id ?? null;
    8393}
  • pages/+Layout.telefunc.ts

    rd4842f4 r83fb5e2  
    2929    if(!newSuggestionId) throw Abort();
    3030
    31     return newSuggestionId;
     31    return { success: true };
    3232}
    3333
     
    7878    const isFavorite = await drizzleQueries.toggleFavoriteBuild(c.db, userId, buildId);
    7979
    80     return isFavorite.favorite;
     80    return isFavorite;
    8181}
    8282
  • pages/auth/register/register.telefunc.ts

    rd4842f4 r83fb5e2  
    1313
    1414    const passwordHash = await bcrypt.hash(password, 8);
    15     await drizzleQueries.createUser(c.db, username, email, passwordHash);
     15    const result = await drizzleQueries.createUser(c.db, username, email, passwordHash);
     16
     17    if(!result) throw Abort();
    1618
    1719    return { success: true };
  • pages/dashboard/admin/adminDashboard.telefunc.ts

    rd4842f4 r83fb5e2  
    6161    if(!newComponentId) throw Abort();
    6262
    63     return newComponentId;
     63    return { success: true };
    6464}
  • pages/dashboard/user/userDashboard.telefunc.ts

    rd4842f4 r83fb5e2  
    2828    const result = await drizzleQueries.deleteBuild(c.db, userId, buildId);
    2929
     30    if(!result) throw Abort();
     31
    3032    return { success: true };
    3133}
Note: See TracChangeset for help on using the changeset viewer.