@@ -133,19 +133,11 @@ export class GradingService {
133133 * @throws `BadRequestException` - If an error occurs during the setting process of _any_ student this exception is thrown.
134134 */
135135 async setOfMultipleStudents ( dtos : Map < Student , GradingDTO > ) : Promise < void > {
136- const em = this . entityManager . fork ( { clear : false } ) ;
137- await em . begin ( ) ;
138-
139- try {
140- for ( const [ student , dto ] of dtos ) {
141- const handIn = await this . getHandInFromDTO ( dto ) ;
142- await this . updateGradingOfStudent ( { student, dto, em, handIn } ) ;
143- }
144- await em . commit ( ) ;
145- } catch ( e ) {
146- await em . rollback ( ) ;
147- throw new BadRequestException ( e ) ;
136+ for ( const [ student , dto ] of dtos ) {
137+ const handIn = await this . getHandInFromDTO ( dto ) ;
138+ await this . updateGradingOfStudent ( { student, dto, handIn } ) ;
148139 }
140+ await this . entityManager . flush ( )
149141 }
150142
151143 /**
@@ -157,41 +149,31 @@ export class GradingService {
157149 * @throws `BadRequestException` - If the students of the team have different gradings.
158150 */
159151 async setOfTeam ( team : Team , dto : GradingDTO ) : Promise < void > {
160- const em = this . entityManager . fork ( { clear : false } ) ;
161- await em . begin ( ) ;
152+ const handIn = await this . getHandInFromDTO ( dto ) ;
153+ const students = team . getStudents ( ) ;
154+ if ( students . length > 0 ) {
155+ const oldGradings = await Promise . all (
156+ students . map ( ( student ) => this . findOfStudentAndHandIn ( student . id , handIn . id ) )
157+ ) ;
158+ const oldGradingIds = new Set (
159+ oldGradings . map ( ( grading ) => grading ?. id ) . filter ( ( gradingId ) => gradingId )
160+ ) ;
161+ if ( oldGradingIds . size > 1 ) {
162+ throw new BadRequestException ( 'Students have different gradings.' ) ;
163+ }
164+ const oldGrading = oldGradings [ 0 ] ;
165+ const newGrading =
166+ ! oldGrading || dto . createNewGrading ? new Grading ( { handIn } ) : oldGrading ;
162167
163- try {
164- const handIn = await this . getHandInFromDTO ( dto ) ;
165- const students = team . getStudents ( ) ;
166- if ( students . length > 0 ) {
167- const oldGradings = await Promise . all (
168- students . map ( ( student ) => this . findOfStudentAndHandIn ( student . id , handIn . id ) )
169- ) ;
170- const oldGradingIds = new Set (
171- oldGradings . map ( ( grading ) => grading ?. id ) . filter ( ( gradingId ) => gradingId )
172- ) ;
173- if ( oldGradingIds . size > 1 ) {
174- // noinspection ExceptionCaughtLocallyJS
175- throw new BadRequestException ( 'Students have different gradings.' ) ;
176- }
177- const oldGrading = oldGradings [ 0 ] ;
178- const newGrading =
179- ! oldGrading || dto . createNewGrading ? new Grading ( { handIn } ) : oldGrading ;
180-
181- newGrading . updateFromDTO ( { dto, handIn } ) ;
182- oldGrading ?. students . remove ( ...students ) ;
183- newGrading . students . add ( ...students ) ;
184-
185- if ( ! ! oldGrading && oldGrading . students . length === 0 ) {
186- em . remove ( oldGrading ) ;
187- }
188-
189- em . persist ( newGrading ) ;
190- em . commit ( ) ;
168+ newGrading . updateFromDTO ( { dto, handIn } ) ;
169+ oldGrading ?. students . remove ( ...students ) ;
170+ newGrading . students . add ( ...students ) ;
171+
172+ if ( ! ! oldGrading && oldGrading . students . length === 0 ) {
173+ this . repository . remove ( oldGrading ) ;
191174 }
192- } catch ( e ) {
193- await em . rollback ( ) ;
194- throw new BadRequestException ( e ) ;
175+
176+ await this . repository . persistAndFlush ( newGrading ) ;
195177 }
196178 }
197179
@@ -223,7 +205,6 @@ export class GradingService {
223205 student,
224206 dto,
225207 handIn,
226- em,
227208 } : UpdateGradingParams ) : Promise < void > {
228209 const oldGrading : Grading | undefined = await this . findOfStudentAndHandIn (
229210 student . id ,
@@ -238,10 +219,10 @@ export class GradingService {
238219 newGrading . students . add ( student ) ;
239220
240221 if ( ! ! oldGrading && oldGrading . students . length === 0 ) {
241- em . remove ( oldGrading ) ;
222+ this . repository . remove ( oldGrading ) ;
242223 }
243224
244- em . persist ( newGrading ) ;
225+ this . repository . persist ( newGrading ) ;
245226 }
246227
247228 /**
@@ -286,7 +267,6 @@ interface UpdateGradingParams {
286267 student : Student ;
287268 dto : GradingDTO ;
288269 handIn : HandIn ;
289- em : EntityManager ;
290270}
291271
292272export interface StudentAndGradings {
0 commit comments