Files

38 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

import { prisma } from '../lib/prisma';
import { AppError } from '../types/index';
export async function listVlans() {
return prisma.vlan.findMany({ orderBy: { vlanId: 'asc' } });
}
export async function createVlan(data: {
vlanId: number;
name: string;
description?: string;
color?: string;
}) {
const existing = await prisma.vlan.findUnique({ where: { vlanId: data.vlanId } });
if (existing) throw new AppError(`VLAN ID ${data.vlanId} already exists`, 409, 'DUPLICATE');
if (data.vlanId < 1 || data.vlanId > 4094) {
throw new AppError('VLAN ID must be between 1 and 4094', 400, 'INVALID_VLAN_ID');
}
return prisma.vlan.create({ data });
}
export async function updateVlan(
id: string,
data: Partial<{ name: string; description: string; color: string }>
) {
const existing = await prisma.vlan.findUnique({ where: { id } });
if (!existing) throw new AppError('VLAN not found', 404, 'NOT_FOUND');
return prisma.vlan.update({ where: { id }, data });
}
export async function deleteVlan(id: string) {
const existing = await prisma.vlan.findUnique({ where: { id } });
if (!existing) throw new AppError('VLAN not found', 404, 'NOT_FOUND');
return prisma.vlan.delete({ where: { id } });
}