refactor: improve code formatting and error messages in ClassService
This commit is contained in:
parent
db88a24028
commit
8e63816616
@ -8,7 +8,7 @@ import * as moment from "moment";
|
||||
|
||||
@Injectable()
|
||||
export class ClassService {
|
||||
constructor(private readonly prisma: PrismaService) { }
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async create(createClassDto: CreateClassDto) {
|
||||
return await this.prisma.class.create({ data: createClassDto });
|
||||
@ -20,7 +20,7 @@ export class ClassService {
|
||||
cursor,
|
||||
where,
|
||||
orderBy,
|
||||
include
|
||||
include,
|
||||
}: {
|
||||
skip?: number;
|
||||
take?: number;
|
||||
@ -35,7 +35,7 @@ export class ClassService {
|
||||
cursor,
|
||||
where,
|
||||
orderBy,
|
||||
include
|
||||
include,
|
||||
});
|
||||
}
|
||||
|
||||
@ -68,12 +68,11 @@ export class ClassService {
|
||||
include: { Students: true },
|
||||
});
|
||||
|
||||
if (!Class)
|
||||
throw new HttpException("Class not found", 404);
|
||||
if (!Class) throw new HttpException("Class not found", 404);
|
||||
|
||||
const studentIdsToAdd = studentIds.filter(
|
||||
(studentId) =>
|
||||
!Class.Students.some((student) => student.id === studentId)
|
||||
!Class.Students.some((student) => student.id === studentId),
|
||||
);
|
||||
|
||||
if (studentIdsToAdd.length === 0) return Class;
|
||||
@ -82,7 +81,9 @@ export class ClassService {
|
||||
where: { id: classId },
|
||||
data: {
|
||||
Students: {
|
||||
connect: studentIdsToAdd.map((studentId) => ({ id: studentId })),
|
||||
connect: studentIdsToAdd.map((studentId) => ({
|
||||
id: studentId,
|
||||
})),
|
||||
},
|
||||
},
|
||||
});
|
||||
@ -97,18 +98,41 @@ export class ClassService {
|
||||
|
||||
async createRoom(classId: string, createRoomClassDto: CreateRoomClassDto) {
|
||||
// Check if end time is greater than start time
|
||||
const invalidTime = createRoomClassDto.times.find(
|
||||
(time) => moment(time.start, "HH:mm").isAfter(moment(time.end, "HH:mm"))
|
||||
const invalidTime = createRoomClassDto.times.find((time) =>
|
||||
moment(time.start, "HH:mm").isAfter(moment(time.end, "HH:mm")),
|
||||
);
|
||||
|
||||
if (invalidTime)
|
||||
throw new HttpException("Invalid time", 400);
|
||||
throw new HttpException(
|
||||
"The end time must be greater than the start time",
|
||||
400,
|
||||
);
|
||||
|
||||
// Check if date is in the past
|
||||
const invalidDate = moment(createRoomClassDto.date).isBefore(moment());
|
||||
const date = moment(createRoomClassDto.date);
|
||||
const firstTimeDateStart = moment(date).set({
|
||||
hour: moment(createRoomClassDto.times[0].start, "HH:mm").hours(),
|
||||
minute: moment(
|
||||
createRoomClassDto.times[0].start,
|
||||
"HH:mm",
|
||||
).minutes(),
|
||||
});
|
||||
const firsTimeDateEnd = moment(date).set({
|
||||
hour: moment(createRoomClassDto.times[0].end, "HH:mm").hours(),
|
||||
minute: moment(createRoomClassDto.times[0].end, "HH:mm").minutes(),
|
||||
});
|
||||
|
||||
if (invalidDate)
|
||||
throw new HttpException("Invalid date", 400);
|
||||
// Check if start time is greater than end time
|
||||
if (firstTimeDateStart.isAfter(firsTimeDateEnd))
|
||||
throw new HttpException(
|
||||
"The end time must be greater than the start time",
|
||||
400,
|
||||
);
|
||||
|
||||
if (moment().isAfter(firsTimeDateEnd))
|
||||
throw new HttpException(
|
||||
"The end time must be greater than the current time",
|
||||
400,
|
||||
);
|
||||
|
||||
return await this.prisma.room.create({
|
||||
include: { Times: true },
|
||||
@ -132,10 +156,14 @@ export class ClassService {
|
||||
}
|
||||
|
||||
async getRooms(classId: string) {
|
||||
return await this.prisma.class.findUnique({
|
||||
where: { id: classId },
|
||||
include: { ClassRoom: { include: { Times: true, Presentator: true } } },
|
||||
}).then((class_) => class_.ClassRoom);
|
||||
return await this.prisma.class
|
||||
.findUnique({
|
||||
where: { id: classId },
|
||||
include: {
|
||||
ClassRoom: { include: { Times: true, Presentator: true } },
|
||||
},
|
||||
})
|
||||
.then((class_) => class_.ClassRoom);
|
||||
}
|
||||
|
||||
async getUserClasses(userId: string) {
|
||||
|
Loading…
Reference in New Issue
Block a user