forked from jappel/leistungsbilanz-ts
36 lines
967 B
TypeScript
36 lines
967 B
TypeScript
import crypto from "node:crypto";
|
|
import { and, asc, eq } from "drizzle-orm";
|
|
import { db } from "../client.js";
|
|
import { floors } from "../schema/floors.js";
|
|
|
|
export class FloorRepository {
|
|
async listByProject(projectId: string) {
|
|
return db
|
|
.select()
|
|
.from(floors)
|
|
.where(eq(floors.projectId, projectId))
|
|
.orderBy(asc(floors.sortOrder), asc(floors.name));
|
|
}
|
|
|
|
async create(projectId: string, name: string) {
|
|
const id = crypto.randomUUID();
|
|
const existing = await this.listByProject(projectId);
|
|
const floor = {
|
|
id,
|
|
projectId,
|
|
name,
|
|
sortOrder: existing.length,
|
|
};
|
|
await db.insert(floors).values(floor);
|
|
return floor;
|
|
}
|
|
|
|
async existsInProject(projectId: string, floorId: string) {
|
|
const [row] = await db
|
|
.select({ id: floors.id })
|
|
.from(floors)
|
|
.where(and(eq(floors.projectId, projectId), eq(floors.id, floorId)))
|
|
.limit(1);
|
|
return Boolean(row);
|
|
}
|
|
}
|