import type { Request, Response } from "express"; import { createProjectFloorDeleteProjectCommand, createProjectFloorInsertProjectCommand, createProjectFloorSnapshot, createProjectFloorUpdateProjectCommand, } from "../../domain/models/project-location-structure-project-command.model.js"; import { createFloorSchema, deleteProjectLocationSchema, updateFloorSchema, } from "../../shared/validation/project-structure.schemas.js"; import { projectCommandService } from "../composition/project-command-stores.js"; import { floorRepository } from "../composition/application-repositories.js"; import { respondWithProjectCommandError } from "./project-command.controller.js"; export async function listFloorsByProject(req: Request, res: Response) { const { projectId } = req.params; if (typeof projectId !== "string") { return res.status(400).json({ error: "Invalid projectId" }); } const result = await floorRepository.listByProject(projectId); return res.json(result); } export async function createFloor(req: Request, res: Response) { const { projectId } = req.params; if (typeof projectId !== "string") { return res.status(400).json({ error: "Invalid projectId" }); } const parsed = createFloorSchema.safeParse(req.body); if (!parsed.success) { return res.status(400).json({ error: parsed.error.flatten() }); } const existingFloors = await floorRepository.listByProject(projectId); const nextSortOrder = existingFloors.length ? Math.max(...existingFloors.map((floor) => floor.sortOrder)) + 1 : 0; const floor = createProjectFloorSnapshot( projectId, parsed.data.name, nextSortOrder ); try { const result = projectCommandService.executeUser({ projectId, expectedRevision: parsed.data.expectedRevision, description: "Geschoss anlegen", command: createProjectFloorInsertProjectCommand(floor), }); return res.status(201).json({ ...result, floor }); } catch (error) { return respondWithProjectCommandError(error, res); } } export async function updateFloor(req: Request, res: Response) { const { projectId, floorId } = req.params; if (typeof projectId !== "string" || typeof floorId !== "string") { return res.status(400).json({ error: "Invalid parameters" }); } const parsed = updateFloorSchema.safeParse(req.body); if (!parsed.success) { return res.status(400).json({ error: parsed.error.flatten() }); } const current = await floorRepository.findById(projectId, floorId); if (!current) { return res.status(404).json({ error: "Floor not found" }); } const target = { ...current, name: parsed.data.name }; try { const result = projectCommandService.executeUser({ projectId, expectedRevision: parsed.data.expectedRevision, description: "Geschoss bearbeiten", command: createProjectFloorUpdateProjectCommand(current, target), }); return res.json({ ...result, floor: target }); } catch (error) { return respondWithProjectCommandError(error, res); } } export async function deleteFloor(req: Request, res: Response) { const { projectId, floorId } = req.params; if (typeof projectId !== "string" || typeof floorId !== "string") { return res.status(400).json({ error: "Invalid parameters" }); } const parsed = deleteProjectLocationSchema.safeParse(req.body); if (!parsed.success) { return res.status(400).json({ error: parsed.error.flatten() }); } const current = await floorRepository.findById(projectId, floorId); if (!current) { return res.status(404).json({ error: "Floor not found" }); } try { const result = projectCommandService.executeUser({ projectId, expectedRevision: parsed.data.expectedRevision, description: "Geschoss löschen", command: createProjectFloorDeleteProjectCommand(current), }); return res.json(result); } catch (error) { return respondWithProjectCommandError(error, res); } }