mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 01:20:55 +00:00
47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
import { Injectable, NotFoundException, ConflictException } from '@nestjs/common';
|
|
import { PrismaService } from '../../common/prisma.service';
|
|
import { CreateSeatClassDto, UpdateSeatClassDto } from './seat-classes.dto';
|
|
|
|
@Injectable()
|
|
export class SeatClassesService {
|
|
constructor(private prisma: PrismaService) {}
|
|
|
|
private readonly coachInclude = {
|
|
coaches: {
|
|
select: { id: true, coachNumber: true, label: true, mode: true, totalUnits: true, _count: { select: { seats: true } } },
|
|
orderBy: { label: 'asc' as const },
|
|
},
|
|
};
|
|
|
|
listSeatClasses() {
|
|
return this.prisma.seatClass.findMany({ orderBy: { createdAt: 'asc' }, include: this.coachInclude });
|
|
}
|
|
|
|
async getSeatClass(id: string) {
|
|
const sc = await this.prisma.seatClass.findUnique({ where: { id }, include: this.coachInclude });
|
|
if (!sc) throw new NotFoundException('SeatClass not found');
|
|
return sc;
|
|
}
|
|
|
|
async createSeatClass(dto: CreateSeatClassDto) {
|
|
try {
|
|
return await this.prisma.seatClass.create({ data: dto, include: this.coachInclude });
|
|
} catch (e: any) {
|
|
if (e.code === 'P2002') throw new ConflictException(`Seat class "${dto.name}" already exists`);
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
async updateSeatClass(id: string, dto: UpdateSeatClassDto) {
|
|
const sc = await this.prisma.seatClass.findUnique({ where: { id } });
|
|
if (!sc) throw new NotFoundException('SeatClass not found');
|
|
return this.prisma.seatClass.update({ where: { id }, data: dto, include: this.coachInclude });
|
|
}
|
|
|
|
async deleteSeatClass(id: string) {
|
|
const sc = await this.prisma.seatClass.findUnique({ where: { id } });
|
|
if (!sc) throw new NotFoundException('SeatClass not found');
|
|
return this.prisma.seatClass.delete({ where: { id } });
|
|
}
|
|
}
|