forked from jappel/leistungsbilanz-ts
66 lines
2.3 KiB
TypeScript
66 lines
2.3 KiB
TypeScript
import type { Request, Response } from "express";
|
|
import { ProjectRepository } from "../../db/repositories/project.repository.js";
|
|
import {
|
|
createProjectSchema,
|
|
updateProjectSettingsSchema,
|
|
} from "../../shared/validation/project-structure.schemas.js";
|
|
import { createProjectSettingsUpdateProjectCommand } from "../../domain/models/project-settings-project-command.model.js";
|
|
import { projectCommandService } from "../composition/project-command-stores.js";
|
|
import { respondWithProjectCommandError } from "./project-command.controller.js";
|
|
|
|
const projectRepository = new ProjectRepository();
|
|
|
|
export async function listProjects(_req: Request, res: Response) {
|
|
const result = await projectRepository.list();
|
|
res.json(result);
|
|
}
|
|
|
|
export async function createProject(req: Request, res: Response) {
|
|
const parsed = createProjectSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ error: parsed.error.flatten() });
|
|
}
|
|
const project = await projectRepository.create(parsed.data);
|
|
return res.status(201).json(project);
|
|
}
|
|
|
|
export async function getProject(req: Request, res: Response) {
|
|
const { projectId } = req.params;
|
|
if (typeof projectId !== "string") {
|
|
return res.status(400).json({ error: "Invalid projectId" });
|
|
}
|
|
const row = await projectRepository.findById(projectId);
|
|
if (!row) {
|
|
return res.status(404).json({ error: "Project not found" });
|
|
}
|
|
return res.json(row);
|
|
}
|
|
|
|
export async function updateProjectSettings(req: Request, res: Response) {
|
|
const { projectId } = req.params;
|
|
if (typeof projectId !== "string") {
|
|
return res.status(400).json({ error: "Invalid projectId" });
|
|
}
|
|
|
|
const parsed = updateProjectSettingsSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ error: parsed.error.flatten() });
|
|
}
|
|
|
|
const { expectedRevision, ...settings } = parsed.data;
|
|
try {
|
|
const result = projectCommandService.executeUser({
|
|
projectId,
|
|
expectedRevision,
|
|
description: "Projekteinstellungen bearbeiten",
|
|
command: createProjectSettingsUpdateProjectCommand(settings),
|
|
});
|
|
const project = await projectRepository.findById(projectId);
|
|
if (!project) {
|
|
throw new Error("Updated project could not be loaded.");
|
|
}
|
|
return res.json({ ...result, project });
|
|
} catch (error) {
|
|
return respondWithProjectCommandError(error, res);
|
|
}
|
|
}
|