Persist project locations

This commit is contained in:
2026-07-26 10:56:29 +02:00
parent 1cf26a932e
commit ac465a1cc0
23 changed files with 1313 additions and 101 deletions
+26 -2
View File
@@ -1,6 +1,12 @@
import type { Request, Response } from "express";
import { FloorRepository } from "../../db/repositories/floor.repository.js";
import {
createProjectFloorInsertProjectCommand,
createProjectFloorSnapshot,
} from "../../domain/models/project-location-structure-project-command.model.js";
import { createFloorSchema } from "../../shared/validation/project-structure.schemas.js";
import { projectCommandService } from "../composition/project-command-stores.js";
import { respondWithProjectCommandError } from "./project-command.controller.js";
const floorRepository = new FloorRepository();
@@ -25,6 +31,24 @@ export async function createFloor(req: Request, res: Response) {
return res.status(400).json({ error: parsed.error.flatten() });
}
const floor = await floorRepository.create(projectId, parsed.data.name);
return res.status(201).json(floor);
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);
}
}
+17 -10
View File
@@ -1,9 +1,13 @@
import type { Request, Response } from "express";
import { FloorRepository } from "../../db/repositories/floor.repository.js";
import { RoomRepository } from "../../db/repositories/room.repository.js";
import {
createProjectRoomInsertProjectCommand,
createProjectRoomSnapshot,
} from "../../domain/models/project-location-structure-project-command.model.js";
import { createRoomSchema } from "../../shared/validation/project-structure.schemas.js";
import { projectCommandService } from "../composition/project-command-stores.js";
import { respondWithProjectCommandError } from "./project-command.controller.js";
const floorRepository = new FloorRepository();
const roomRepository = new RoomRepository();
export async function listRoomsByProject(req: Request, res: Response) {
@@ -27,13 +31,16 @@ export async function createRoom(req: Request, res: Response) {
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 = createProjectRoomSnapshot(projectId, parsed.data);
try {
const result = projectCommandService.executeUser({
projectId,
expectedRevision: parsed.data.expectedRevision,
description: "Raum anlegen",
command: createProjectRoomInsertProjectCommand(room),
});
return res.status(201).json({ ...result, room });
} catch (error) {
return respondWithProjectCommandError(error, res);
}
const room = await roomRepository.create(projectId, parsed.data);
return res.status(201).json(room);
}