Skip to content

Commit 31d2efa

Browse files
committed
Add new function to set a grading for a whole team.
1 parent 5ff3b83 commit 31d2efa

2 files changed

Lines changed: 42 additions & 23 deletions

File tree

server/src/module/student/grading.controller.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,7 @@ export class GradingController {
128128
@Body() dto: GradingDTO
129129
): Promise<void> {
130130
const team = await this.teamService.findById({ tutorialId, teamId });
131-
const dtoMap = new Map<Student, GradingDTO>();
132-
133-
for (const student of team.students) {
134-
dtoMap.set(student, dto);
135-
}
136131

137-
await this.gradingService.setOfMultipleStudents(dtoMap);
132+
await this.gradingService.setOfTeam(team, dto);
138133
}
139134
}

server/src/module/student/grading.service.ts

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,6 @@ export class GradingService {
112112
return await Promise.all(gradings);
113113
}
114114

115-
/**
116-
* Finds and returns the gradings for the given team and hand-in.
117-
*
118-
* If there are no grading matching both, team and hand-in, an empty array is returned.
119-
*
120-
* @param team Team to get the gradings for.
121-
* @param handIn Hand-in to get the gradings of.
122-
*
123-
* @returns Gradings for the given team and hand-in as described above.
124-
*/
125-
async findOfTeamAndHandIn(team: Team, handIn: HandIn): Promise<Grading[]> {
126-
const gradingList = await this.findOfMultipleStudents(team.getStudents().map((s) => s.id));
127-
const gradings = gradingList.getAllGradingsForHandIn(handIn);
128-
129-
return gradings.filter((g) => g.belongsToTeam);
130-
}
131-
132115
/**
133116
* Sets the grading of the given student.
134117
*
@@ -166,6 +149,47 @@ export class GradingService {
166149
}
167150
}
168151

152+
/**
153+
* Sets the grading of all students of the given team to the one from the DTO.
154+
*
155+
* @param team Team which students should get the new grading.
156+
* @param dto DTO of the new grading.
157+
*
158+
* @throws `BadRequestException` - If the students of the team have different gradings.
159+
*/
160+
async setOfTeam(team: Team, dto: GradingDTO): Promise<void> {
161+
const em = this.entityManager.fork({ clear: false });
162+
await em.begin();
163+
164+
try {
165+
const handIn = await this.getHandInFromDTO(dto);
166+
const students = team.getStudents();
167+
if (students.length > 0) {
168+
const oldGrading = await this.findOfStudentAndHandIn(students[0].id, handIn.id);
169+
if (oldGrading?.belongsToTeam === false) {
170+
// noinspection ExceptionCaughtLocallyJS
171+
throw new BadRequestException('Students have different gradings.');
172+
}
173+
const newGrading =
174+
!oldGrading || dto.createNewGrading ? new Grading({ handIn }) : oldGrading;
175+
176+
newGrading.updateFromDTO({ dto, handIn });
177+
oldGrading?.students.remove(...students);
178+
newGrading.students.add(...students);
179+
180+
if (!!oldGrading && oldGrading.students.length === 0) {
181+
em.remove(oldGrading);
182+
}
183+
184+
em.persist(newGrading);
185+
em.commit();
186+
}
187+
} catch (e) {
188+
await em.rollback();
189+
throw new BadRequestException(e);
190+
}
191+
}
192+
169193
async findAllGradingsOfStudent(student: Student): Promise<Grading[]> {
170194
return this.repository.find({ students: student.id }, { populate: true });
171195
}

0 commit comments

Comments
 (0)