Changes in / [a744c90:87b79bc]


Ignore:
Location:
database
Files:
1 added
1 deleted
5 edited

Legend:

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

    ra744c90 r87b79bc  
    200200            .select({
    201201                componentId: buildComponentsTable.componentId,
    202                 component: componentsTable,
    203                 quantity: buildComponentsTable.numComponents
     202                component: componentsTable
    204203            })
    205204            .from(buildComponentsTable)
     
    293292        return {
    294293            ...buildDetails,
    295             components: components.map(c => ({
    296                     ...c.component,
    297                     quantity: c.quantity
    298             })),
     294            components: components.map(c => c.component),
    299295            reviews: reviews.map(r => ({
    300296                username: r.username,
     
    423419
    424420        const existing = await tx
    425             .select({
    426                 componentId: buildComponentsTable.componentId,
    427                 numComponents: buildComponentsTable.numComponents
    428             })
     421            .select({ componentId: buildComponentsTable.componentId })
    429422            .from(buildComponentsTable)
    430423            .where(eq(buildComponentsTable.buildId, buildId));
     
    435428                    buildId: newBuild.id,
    436429                    componentId: r.componentId,
    437                     numComponents: r.numComponents
    438430                })),
    439431            );
     
    553545        const components = await tx
    554546            .select({
    555                 componentId: buildComponentsTable.componentId,
    556                 quantity: buildComponentsTable.numComponents
     547                componentId: buildComponentsTable.componentId
    557548            })
    558549            .from(buildComponentsTable)
     
    563554        return {
    564555            build,
    565             components: components.map(c => ({
    566                 id: c.componentId,
    567                 quantity: c.quantity
    568             }))
     556            componentIds: components.map(c => c.componentId)
    569557        };
    570558    });
  • database/drizzle/queries/components.ts

    ra744c90 r87b79bc  
    196196            id: componentsTable.id,
    197197            type: componentsTable.type,
    198             quantity: buildComponentsTable.numComponents
    199198        })
    200199        .from(buildComponentsTable)
     
    232231        componentsDetails.push({
    233232            ...comp,
    234             quantity: comp.quantity,
    235233            details: details
    236234        });
     
    258256
    259257    if(!existingComponents) return null;
     258
     259
    260260
    261261    const existing = {
     
    278278        ...existing.networkAdapters,
    279279        ...existing.soundCards
    280     ].reduce((sum: number, c: any) => {
    281         if (!c) return sum;
    282         return sum + (c.quantity || 1);
    283     }, 0);
     280    ].filter(Boolean).length;
    284281
    285282    const existingTDP = existingComponents.reduce((sum, c) => {
    286283        const tdp = c.details?.tdp ? Number(c.details.tdp) : 0;
    287         return sum + (tdp * (c.quantity || 1));
     284        return sum + tdp;
    288285    }, 0);
    289286
     
    459456        if (existing.memory.length > 0) {
    460457            const totalModules = existing.memory.reduce((sum: number, m: any) =>
    461                 sum + (Number(m.details.modules) * m.quantity), 0
     458                sum + Number(m.details.modules), 0
    462459            );
    463460            const totalCapacity = existing.memory.reduce((sum: number, m: any) =>
    464                 sum + (Number(m.details.capacity) * m.quantity), 0
     461                sum + Number(m.details.capacity), 0
    465462            );
    466463
     
    707704            if (!caseStorageFormFactor) return false;
    708705
    709             const usedSlots = existing.storage
    710                 .filter((s: any) => s.details.formFactor === stor.formFactor)
    711                 .reduce((sum: number, s: any) => sum + s.quantity, 0);
     706            const usedSlots = existing.storage.filter(
     707                (s: any) => s.details.formFactor === stor.formFactor
     708            ).length;
    712709
    713710            return usedSlots < Number(caseStorageFormFactor.numSlots);
     
    824821                if (!storageFF) return false;
    825822
    826                 const usedSlots = existing.storage
    827                     .filter((s: any) => s.details.formFactor === stor.details.formFactor)
    828                     .reduce((sum: number, s: any) => sum + s.quantity, 0);;
     823                const usedSlots = existing.storage.filter(
     824                    (s: any) => s.details.formFactor === stor.details.formFactor
     825                ).length;
    829826
    830827                return usedSlots <= Number(storageFF.numSlots);
     
    966963            .values({
    967964                buildId,
    968                 componentId,
    969                 numComponents: 1
    970             })
    971             .onConflictDoUpdate({
    972                 target: [buildComponentsTable.buildId, buildComponentsTable.componentId],
    973                 set: {
    974                     numComponents: sql`${buildComponentsTable.numComponents} + 1`
    975                 }
     965                componentId
    976966            });
    977967
     
    979969            .select({
    980970                price:  componentsTable.price,
    981                 quantity: buildComponentsTable.numComponents
    982971            })
    983972            .from(buildComponentsTable)
     
    990979            );
    991980
    992         const totalPrice = buildComponents.reduce((sum, c) =>
    993             sum + (Number(c.price) * c.quantity), 0
    994         );
     981        const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0);
    995982
    996983        await tx
     
    10221009        if(!build || build.isApproved) return null;
    10231010
    1024         const [existing] = await tx
    1025             .select({
    1026                 quantity: buildComponentsTable.numComponents
    1027             })
    1028             .from(buildComponentsTable)
     1011        const result = await tx
     1012            .delete(buildComponentsTable)
    10291013            .where(
    10301014                and(
     
    10321016                    eq(buildComponentsTable.componentId, componentId)
    10331017                )
    1034             )
    1035             .limit(1);
    1036 
    1037         if (!existing) return null;
    1038 
    1039         if (existing.quantity > 1) {
    1040             await tx
    1041                 .update(buildComponentsTable)
    1042                 .set({
    1043                     numComponents: sql`${buildComponentsTable.numComponents} - 1`
    1044                 })
    1045                 .where(
    1046                     and(
    1047                         eq(buildComponentsTable.buildId, buildId),
    1048                         eq(buildComponentsTable.componentId, componentId)
    1049                     )
    1050                 );
    1051         } else {
    1052             await tx
    1053                 .delete(buildComponentsTable)
    1054                 .where(
    1055                     and(
    1056                         eq(buildComponentsTable.buildId, buildId),
    1057                         eq(buildComponentsTable.componentId, componentId)
    1058                     )
    1059                 );
    1060         }
     1018            );
     1019
     1020        if(result.rowCount === 0) return null;
    10611021
    10621022        const buildComponents = await tx
    10631023            .select({
    10641024                price:  componentsTable.price,
    1065                 quantity: buildComponentsTable.numComponents
    10661025            })
    10671026            .from(buildComponentsTable)
     
    10741033            );
    10751034
    1076         const totalPrice = buildComponents.reduce((sum, c) =>
    1077             sum + (Number(c.price) * c.quantity), 0
    1078         );
     1035        const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0);
    10791036
    10801037        await tx
     
    10871044            );
    10881045
    1089         return componentId;
     1046        return result;
    10901047    })
    10911048}
  • database/drizzle/schema/builds.ts

    ra744c90 r87b79bc  
    1717
    1818export const buildComponentsTable = pgTable("build_component", {
     19        id: serial("id").primaryKey(),
    1920        buildId: integer("build_id")
    2021            .notNull()
     
    2324            .notNull()
    2425            .references(() => componentsTable.id, { onDelete: "cascade", onUpdate: "cascade" }),
    25         numComponents: integer("num_components")
    26             .notNull()
    27             .default(1),
    2826    },
    29     (t) => ({
    30         pk: primaryKey({ columns: [t.buildId, t.componentId] }),
    31     }),
    3227);
    3328
  • database/migrations/meta/0000_snapshot.json

    ra744c90 r87b79bc  
    11{
    2   "id": "d1ab4ce8-e387-4d74-8d5b-511fdbe0a032",
     2  "id": "779a3882-651b-446d-8967-3728aeea2888",
    33  "prevId": "00000000-0000-0000-0000-000000000000",
    44  "version": "7",
     
    99      "schema": "",
    1010      "columns": {
     11        "id": {
     12          "name": "id",
     13          "type": "serial",
     14          "primaryKey": true,
     15          "notNull": true
     16        },
    1117        "build_id": {
    1218          "name": "build_id",
     
    2026          "primaryKey": false,
    2127          "notNull": true
    22         },
    23         "num_components": {
    24           "name": "num_components",
    25           "type": "integer",
    26           "primaryKey": false,
    27           "notNull": true,
    28           "default": 1
    2928        }
    3029      },
     
    5857        }
    5958      },
    60       "compositePrimaryKeys": {
    61         "build_component_build_id_component_id_pk": {
    62           "name": "build_component_build_id_component_id_pk",
    63           "columns": [
    64             "build_id",
    65             "component_id"
    66           ]
    67         }
    68       },
     59      "compositePrimaryKeys": {},
    6960      "uniqueConstraints": {},
    7061      "policies": {},
  • database/migrations/meta/_journal.json

    ra744c90 r87b79bc  
    66      "idx": 0,
    77      "version": "7",
    8       "when": 1769598141213,
    9       "tag": "0000_shallow_darkhawk",
     8      "when": 1769188258136,
     9      "tag": "0000_regular_kulan_gath",
    1010      "breakpoints": true
    1111    }
Note: See TracChangeset for help on using the changeset viewer.