81 lines
2.7 KiB
TypeScript
81 lines
2.7 KiB
TypeScript
import type { Request, Response } from "express";
|
|
import { CircuitWriteService } from "../../domain/services/circuit-write.service.js";
|
|
import {
|
|
createCircuitDeviceRowSchema,
|
|
moveCircuitDeviceRowSchema,
|
|
updateCircuitDeviceRowSchema,
|
|
} from "../../shared/validation/circuit.schemas.js";
|
|
|
|
const circuitWriteService = new CircuitWriteService();
|
|
|
|
export async function createCircuitDeviceRow(req: Request, res: Response) {
|
|
const { circuitId } = req.params;
|
|
if (typeof circuitId !== "string") {
|
|
return res.status(400).json({ error: "Invalid circuitId" });
|
|
}
|
|
|
|
const parsed = createCircuitDeviceRowSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ error: parsed.error.flatten() });
|
|
}
|
|
|
|
try {
|
|
const created = await circuitWriteService.createDeviceRow(circuitId, parsed.data);
|
|
return res.status(201).json(created);
|
|
} catch (error) {
|
|
return res.status(400).json({ error: error instanceof Error ? error.message : "Failed to create device row." });
|
|
}
|
|
}
|
|
|
|
export async function updateCircuitDeviceRow(req: Request, res: Response) {
|
|
const { rowId } = req.params;
|
|
if (typeof rowId !== "string") {
|
|
return res.status(400).json({ error: "Invalid rowId" });
|
|
}
|
|
|
|
const parsed = updateCircuitDeviceRowSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ error: parsed.error.flatten() });
|
|
}
|
|
|
|
try {
|
|
const updated = await circuitWriteService.updateDeviceRow(rowId, parsed.data);
|
|
return res.json(updated);
|
|
} catch (error) {
|
|
return res.status(400).json({ error: error instanceof Error ? error.message : "Failed to update device row." });
|
|
}
|
|
}
|
|
|
|
export async function deleteCircuitDeviceRow(req: Request, res: Response) {
|
|
const { rowId } = req.params;
|
|
if (typeof rowId !== "string") {
|
|
return res.status(400).json({ error: "Invalid rowId" });
|
|
}
|
|
|
|
try {
|
|
await circuitWriteService.deleteDeviceRow(rowId);
|
|
return res.status(204).send();
|
|
} catch (error) {
|
|
return res.status(400).json({ error: error instanceof Error ? error.message : "Failed to delete device row." });
|
|
}
|
|
}
|
|
|
|
export async function moveCircuitDeviceRow(req: Request, res: Response) {
|
|
const { rowId } = req.params;
|
|
if (typeof rowId !== "string") {
|
|
return res.status(400).json({ error: "Invalid rowId" });
|
|
}
|
|
|
|
const parsed = moveCircuitDeviceRowSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ error: parsed.error.flatten() });
|
|
}
|
|
|
|
try {
|
|
const moved = await circuitWriteService.moveDeviceRow(rowId, parsed.data);
|
|
return res.json(moved);
|
|
} catch (error) {
|
|
return res.status(400).json({ error: error instanceof Error ? error.message : "Failed to move device row." });
|
|
}
|
|
}
|