Add editable project floors and rooms

This commit is contained in:
Julian Appel 2026-07-31 14:46:06 +02:00
parent 1e3c721cfa
commit 1eed19ef6b
15 changed files with 743 additions and 80 deletions

View file

@ -1,9 +1,15 @@
import type { Request, Response } from "express";
import {
createProjectFloorDeleteProjectCommand,
createProjectFloorInsertProjectCommand,
createProjectFloorSnapshot,
createProjectFloorUpdateProjectCommand,
} from "../../domain/models/project-location-structure-project-command.model.js";
import { createFloorSchema } from "../../shared/validation/project-structure.schemas.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";
@ -50,3 +56,56 @@ export async function createFloor(req: Request, res: Response) {
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);
}
}

View file

@ -1,9 +1,15 @@
import type { Request, Response } from "express";
import {
createProjectRoomDeleteProjectCommand,
createProjectRoomInsertProjectCommand,
createProjectRoomSnapshot,
createProjectRoomUpdateProjectCommand,
} from "../../domain/models/project-location-structure-project-command.model.js";
import { createRoomSchema } from "../../shared/validation/project-structure.schemas.js";
import {
createRoomSchema,
deleteProjectLocationSchema,
updateRoomSchema,
} from "../../shared/validation/project-structure.schemas.js";
import { projectCommandService } from "../composition/project-command-stores.js";
import { roomRepository } from "../composition/application-repositories.js";
import { respondWithProjectCommandError } from "./project-command.controller.js";
@ -42,3 +48,61 @@ export async function createRoom(req: Request, res: Response) {
return respondWithProjectCommandError(error, res);
}
}
export async function updateRoom(req: Request, res: Response) {
const { projectId, roomId } = req.params;
if (typeof projectId !== "string" || typeof roomId !== "string") {
return res.status(400).json({ error: "Invalid parameters" });
}
const parsed = updateRoomSchema.safeParse(req.body);
if (!parsed.success) {
return res.status(400).json({ error: parsed.error.flatten() });
}
const current = await roomRepository.findById(projectId, roomId);
if (!current) {
return res.status(404).json({ error: "Room not found" });
}
const target = {
...current,
floorId: parsed.data.floorId,
roomNumber: parsed.data.roomNumber,
roomName: parsed.data.roomName,
};
try {
const result = projectCommandService.executeUser({
projectId,
expectedRevision: parsed.data.expectedRevision,
description: "Raum bearbeiten",
command: createProjectRoomUpdateProjectCommand(current, target),
});
return res.json({ ...result, room: target });
} catch (error) {
return respondWithProjectCommandError(error, res);
}
}
export async function deleteRoom(req: Request, res: Response) {
const { projectId, roomId } = req.params;
if (typeof projectId !== "string" || typeof roomId !== "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 roomRepository.findById(projectId, roomId);
if (!current) {
return res.status(404).json({ error: "Room not found" });
}
try {
const result = projectCommandService.executeUser({
projectId,
expectedRevision: parsed.data.expectedRevision,
description: "Raum löschen",
command: createProjectRoomDeleteProjectCommand(current),
});
return res.json(result);
} catch (error) {
return respondWithProjectCommandError(error, res);
}
}