54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
import type { Request, Response } from "express";
|
|
import { ProjectRepository } from "../../db/repositories/project.repository.js";
|
|
import {
|
|
createProjectSchema,
|
|
updateProjectSettingsSchema,
|
|
} from "../../shared/validation/consumer.schemas.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() });
|
|
}
|
|
|
|
await projectRepository.updateSettings(projectId, parsed.data);
|
|
const row = await projectRepository.findById(projectId);
|
|
if (!row) {
|
|
return res.status(404).json({ error: "Project not found" });
|
|
}
|
|
return res.json(row);
|
|
}
|