forked from jappel/leistungsbilanz-ts
39 lines
1.4 KiB
TypeScript
39 lines
1.4 KiB
TypeScript
import type { Request, Response } from "express";
|
|
import { FloorRepository } from "../../db/repositories/floor.repository.js";
|
|
import { RoomRepository } from "../../db/repositories/room.repository.js";
|
|
import { createRoomSchema } from "../../shared/validation/project-structure.schemas.js";
|
|
|
|
const floorRepository = new FloorRepository();
|
|
const roomRepository = new RoomRepository();
|
|
|
|
export async function listRoomsByProject(req: Request, res: Response) {
|
|
const { projectId } = req.params;
|
|
if (typeof projectId !== "string") {
|
|
return res.status(400).json({ error: "Invalid projectId" });
|
|
}
|
|
|
|
const result = await roomRepository.listByProject(projectId);
|
|
return res.json(result);
|
|
}
|
|
|
|
export async function createRoom(req: Request, res: Response) {
|
|
const { projectId } = req.params;
|
|
if (typeof projectId !== "string") {
|
|
return res.status(400).json({ error: "Invalid projectId" });
|
|
}
|
|
|
|
const parsed = createRoomSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ error: parsed.error.flatten() });
|
|
}
|
|
|
|
if (parsed.data.floorId) {
|
|
const hasValidFloor = await floorRepository.existsInProject(projectId, parsed.data.floorId);
|
|
if (!hasValidFloor) {
|
|
return res.status(400).json({ error: "Floor does not belong to the provided project." });
|
|
}
|
|
}
|
|
|
|
const room = await roomRepository.create(projectId, parsed.data);
|
|
return res.status(201).json(room);
|
|
}
|