Changeset 83fb5e2 for database/drizzle/queries/components.ts
- Timestamp:
- 12/25/25 01:49:51 (7 months ago)
- Branches:
- main
- Children:
- e7f1bfa
- Parents:
- d4842f4
- File:
-
- 1 edited
-
database/drizzle/queries/components.ts (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
database/drizzle/queries/components.ts
rd4842f4 r83fb5e2 1 1 import type { Database } from "../db"; 2 2 import { 3 buildComponentsTable, 3 4 buildsTable, cablesTable, caseMoboFormFactorsTable, casePsFormFactorsTable, caseStorageFormFactorsTable, 4 5 componentsTable, coolerCPUSocketsTable, coolersTable, … … 7 8 memoryTable, motherboardsTable, 8 9 networkAdaptersTable, 9 networkCardsTable, opticalDrivesTable, pcCasesTable, powerSupplyTable, soundCardsTable, storageTable 10 networkCardsTable, opticalDrivesTable, pcCasesTable, powerSupplyTable, 11 ratingBuildsTable, soundCardsTable, storageTable 10 12 } from "../schema"; 11 13 import {and, desc, eq, ilike} from "drizzle-orm"; 12 13 export async function getAllComponents(db: Database, limit?: number, q?: string, componentType?: string) { 14 export async function getAllComponents(db: Database, limit?: number, componentType?: string, q?: string) { 14 15 let queryConditions = []; 15 16 … … 59 60 60 61 if(!component) return null; 61 62 if (!component) return null;63 62 64 63 let details: any = {}; … … 247 246 numRamSlots: specificData.numRamSlots, 248 247 maxRamCapacity: specificData.maxRamCapacity, 248 pciExpressSlots: specificData.pciExpressSlots, 249 249 }); 250 250 break; … … 322 322 }); 323 323 } 324 325 export 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 345 export 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 416 export 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
Note:
See TracChangeset
for help on using the changeset viewer.
