Index: database/drizzle/queries/components.ts
===================================================================
--- database/drizzle/queries/components.ts	(revision ea09e98c1561d7948b3e6612bff5f2269a697451)
+++ database/drizzle/queries/components.ts	(revision 83fb5e20e4a24449997dabfce4e3017efabd0f4a)
@@ -1,4 +1,5 @@
 import type { Database } from "../db";
 import {
+    buildComponentsTable,
     buildsTable, cablesTable, caseMoboFormFactorsTable, casePsFormFactorsTable, caseStorageFormFactorsTable,
     componentsTable, coolerCPUSocketsTable, coolersTable,
@@ -7,9 +8,9 @@
     memoryTable, motherboardsTable,
     networkAdaptersTable,
-    networkCardsTable, opticalDrivesTable, pcCasesTable, powerSupplyTable, soundCardsTable, storageTable
+    networkCardsTable, opticalDrivesTable, pcCasesTable, powerSupplyTable,
+    ratingBuildsTable, soundCardsTable, storageTable
 } from "../schema";
 import {and, desc, eq, ilike} from "drizzle-orm";
-
-export async function getAllComponents(db: Database, limit?: number,  q?: string, componentType?: string) {
+export async function getAllComponents(db: Database, limit?: number, componentType?: string, q?: string) {
     let queryConditions = [];
 
@@ -59,6 +60,4 @@
 
     if(!component) return null;
-
-    if (!component) return null;
 
     let details: any = {};
@@ -247,4 +246,5 @@
                     numRamSlots: specificData.numRamSlots,
                     maxRamCapacity: specificData.maxRamCapacity,
+                    pciExpressSlots: specificData.pciExpressSlots,
                 });
                 break;
@@ -322,2 +322,153 @@
     });
 }
+
+export async function getCompatibleComponents(db: Database, buildId: number, componentType: string) {
+    return db.transaction(async (tx) => {
+        const [build] = await tx
+            .select({
+                buildId: buildsTable.id,
+                userId: buildsTable.userId,
+            })
+            .from(buildsTable)
+            .where(
+                eq(buildsTable.id, buildId)
+            )
+            .limit(1);
+
+        if(!build) return null;
+
+
+    return null;
+    });
+}
+
+export async function addComponentToBuild(db: Database, buildId: number, componentId: number) {
+    const [build] = await db
+        .select()
+        .from(buildsTable)
+        .where(
+            eq(buildsTable.id, buildId)
+        )
+        .limit(1);
+
+    if(!build) return null;
+
+    const [component] = await db
+        .select()
+        .from(componentsTable)
+        .where(
+            eq(componentsTable.id, componentId)
+        )
+        .limit(1);
+
+    if(!component) return null;
+
+    const existing = await db
+        .select()
+        .from(buildComponentsTable)
+        .where(
+            and(
+                eq(buildComponentsTable.buildId, buildId),
+                eq(buildComponentsTable.componentId, componentId)
+            )
+        )
+        .limit(1);
+
+    if(existing.length > 0) return null;
+
+    const [result] = await db
+        .insert(buildComponentsTable)
+        .values({
+            buildId,
+            componentId
+        })
+        .returning({
+            id: buildComponentsTable.buildId
+        });
+
+    const buildComponents = await db
+        .select({
+            price:  componentsTable.price,
+        })
+        .from(buildComponentsTable)
+        .innerJoin(
+            componentsTable,
+            eq(buildComponentsTable.componentId, componentsTable.id)
+        )
+        .where(
+            eq(buildComponentsTable.buildId, buildId)
+        );
+
+    const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0);
+
+     await db
+        .update(buildsTable)
+        .set({
+            totalPrice: totalPrice.toFixed(2)
+        })
+        .where(
+            eq(buildsTable.id, buildId)
+        );
+
+    return result?.id ?? null;
+}
+
+export async function removeComponentFromBuild(db: Database, buildId: number, componentId: number) {
+    const [build] = await db
+        .select()
+        .from(buildsTable)
+        .where(
+            eq(buildsTable.id, buildId)
+        )
+        .limit(1);
+
+    if(!build) return null;
+
+    const [component] = await db
+        .select()
+        .from(componentsTable)
+        .where(
+            eq(componentsTable.id, componentId)
+        )
+        .limit(1);
+
+    if(!component) return null;
+
+    const result = await db
+        .delete(buildComponentsTable)
+        .where(
+            and(
+                eq(buildComponentsTable.buildId, buildId),
+                eq(buildComponentsTable.componentId, componentId)
+            )
+        );
+
+    if(result.rowCount === 0) return null;
+
+    const buildComponents = await db
+        .select({
+            price:  componentsTable.price,
+        })
+        .from(buildComponentsTable)
+        .innerJoin(
+            componentsTable,
+            eq(buildComponentsTable.componentId, componentsTable.id)
+        )
+        .where(
+            eq(buildComponentsTable.buildId, buildId)
+        );
+
+    const totalPrice = buildComponents.reduce((sum, c) => sum + Number(c.price), 0);
+
+    await db
+        .update(buildsTable)
+        .set({
+            totalPrice: totalPrice.toFixed(2)
+        })
+        .where(
+            eq(buildsTable.id, buildId)
+        );
+
+    return result;
+}
+
