feat(api): add RoutesModule with GET/POST/DELETE /routes endpoints
Implements RoutesService and RoutesController for SyncRoute CRUD, wires GroupsModule and RoutesModule into AppModule; 11 new tests, all 31 pass.
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
|
||||
const routeInclude = {
|
||||
sourceGroup: { select: { name: true } },
|
||||
targetGroup: { select: { name: true } },
|
||||
} as const;
|
||||
|
||||
@Injectable()
|
||||
export class RoutesService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
list(sourceGroupId?: string) {
|
||||
return this.prisma.syncRoute.findMany({
|
||||
where: sourceGroupId ? { sourceGroupId } : undefined,
|
||||
include: routeInclude,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
async create(sourceGroupId: string, targetGroupId: string) {
|
||||
if (!sourceGroupId || !targetGroupId) {
|
||||
throw new BadRequestException('sourceGroupId and targetGroupId are required');
|
||||
}
|
||||
return this.prisma.syncRoute.create({
|
||||
data: { sourceGroupId, targetGroupId },
|
||||
include: routeInclude,
|
||||
});
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
try {
|
||||
await this.prisma.syncRoute.delete({ where: { id } });
|
||||
} catch (e) {
|
||||
if ((e as { code?: string })?.code === 'P2025') {
|
||||
throw new NotFoundException(`Route ${id} not found`);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user