@@ -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