Files
rack-planner/server/services/connectionService.ts

42 lines
1.5 KiB
TypeScript

import { prisma } from '../lib/prisma';
import { AppError } from '../types/index';
export async function createConnection(data: { fromPortId: string; toPortId: string; color?: string; label?: string; edgeType?: string }) {
// Check if both ports exist
const [from, to] = await Promise.all([
prisma.port.findUnique({ where: { id: data.fromPortId } }),
prisma.port.findUnique({ where: { id: data.toPortId } }),
]);
if (!from || !to) throw new AppError('One or both ports not found', 404, 'NOT_FOUND');
if (from.id === to.id) throw new AppError('Cannot connect a port to itself', 400, 'BAD_REQUEST');
// Check if ports are already occupied?
// (In real life, a port can only have one cable, but we might allow one source and one target per port if we want to be flexible, but better to prevent simple loops)
// Create connection (if it already exists, use upsert or just throw error; @@unique already handles it)
return prisma.connection.create({ data });
}
export async function deleteConnection(id: string) {
return prisma.connection.delete({ where: { id } });
}
export async function updateConnection(id: string, data: Partial<{ color: string; label: string; edgeType: string }>) {
return prisma.connection.update({
where: { id },
data,
});
}
export async function deleteByPorts(portId1: string, portId2: string) {
return prisma.connection.deleteMany({
where: {
OR: [
{ fromPortId: portId1, toPortId: portId2 },
{ fromPortId: portId2, toPortId: portId1 },
],
},
});
}