feat(rack-planner): implement port-to-port connections (patch cables) with dynamic SVG visualization layer
This commit is contained in:
34
server/services/connectionService.ts
Normal file
34
server/services/connectionService.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { prisma } from '../lib/prisma';
|
||||
import { AppError } from '../types/index';
|
||||
|
||||
export async function createConnection(data: { fromPortId: string; toPortId: string; color?: string; label?: 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 deleteByPorts(portId1: string, portId2: string) {
|
||||
return prisma.connection.deleteMany({
|
||||
where: {
|
||||
OR: [
|
||||
{ fromPortId: portId1, toPortId: portId2 },
|
||||
{ fromPortId: portId2, toPortId: portId1 },
|
||||
],
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user