diff --git a/replicate.ts b/replicate.ts index a4b9d84..0121c8f 100644 --- a/replicate.ts +++ b/replicate.ts @@ -2,7 +2,6 @@ import { db } from "./src/db/index"; import { IUnit } from "./src/types/IUnit"; import { unitsTable } from "./src/db/schema/units"; import got from "got"; -// import { and, eq, exists } from "drizzle-orm"; const { units: unitsOfMarasiDrive } = await got .get( diff --git a/src/controllers/units.ts b/src/controllers/units.ts index 737f53e..b3ec3d9 100644 --- a/src/controllers/units.ts +++ b/src/controllers/units.ts @@ -413,4 +413,53 @@ export const unitsController = new Elysia({ prefix: "/units" }) project: t.String(), }), } + ) + .get( + "/:unitNumber", + async ({ params: { unitNumber }, query: { project } }) => { + try { + return await db.query.unitsTable.findFirst({ + where: and( + eq(unitsTable.unitNo, unitNumber), + eq(unitsTable.project, decodeURIComponent(project)) + ), + }); + } catch (error) { + console.log((error as Error).message); + return status(500, "Internal server error"); + } + }, + { + query: t.Object({ + project: t.String(), + }), + params: t.Object({ + unitNumber: t.String(), + }), + } + ) + .get( + "/on-floor", + async ({ query: { floor, project, wing } }) => { + try { + return await db.query.unitsTable.findMany({ + where: and( + eq(unitsTable.project, decodeURIComponent(project)), + eq(unitsTable.floor, floor), + wing ? eq(unitsTable.wing, wing) : undefined + ), + orderBy: [asc(unitsTable.number)], + }); + } catch (error) { + console.log((error as Error).message); + return status(500, "Internal server error"); + } + }, + { + query: t.Object({ + project: t.String(), + floor: t.Number(), + wing: t.Optional(t.Union([t.Literal("East"), t.Literal("West")])), + }), + } );