diff --git a/src/main/java/de/tum/in/www1/artemis/repository/ResultRepository.java b/src/main/java/de/tum/in/www1/artemis/repository/ResultRepository.java index 321bc682165c..96160b7cf6e2 100644 --- a/src/main/java/de/tum/in/www1/artemis/repository/ResultRepository.java +++ b/src/main/java/de/tum/in/www1/artemis/repository/ResultRepository.java @@ -57,8 +57,14 @@ public interface ResultRepository extends JpaRepository { @Query("select r from Result r left join fetch r.feedbacks left join fetch r.assessor where r.id = :resultId") Optional findByIdWithEagerFeedbacksAndAssessor(@Param("resultId") Long id); - @Query("select r from Result r left join fetch r.submission left join fetch r.feedbacks left join fetch r.assessor where r.id = :resultId") - Optional findByIdWithEagerSubmissionAndFeedbacksAndAssessor(@Param("resultId") Long id); + /** + * Load a result from the database by its id together with the associated submission, the list of feedback items and the assessor. + * + * @param resultId the id of the result to load from the database + * @return an optional containing the result with submission, feedback list and assessor, or an empty optional if no result could be found for the given id + */ + @EntityGraph(attributePaths = { "submission", "feedbacks", "assessor" }) + Optional findWithEagerSubmissionAndFeedbackAndAssessorById(Long resultId); /** * This SQL query is used for inserting results if only one unrated result should exist per participation. This prevents multiple (concurrent) inserts with the same diff --git a/src/main/java/de/tum/in/www1/artemis/service/ModelAssessmentConflictService.java b/src/main/java/de/tum/in/www1/artemis/service/ModelAssessmentConflictService.java index ffe4d9ea7acc..a04eb0123fc4 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/ModelAssessmentConflictService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/ModelAssessmentConflictService.java @@ -109,9 +109,9 @@ void deleteAllConflictsForParticipation(Participation participation) { private void loadSubmissionsAndFeedbacksAndAssessorOfConflictingResults(List conflicts) { conflicts.forEach(conflict -> { conflict.getCausingConflictingResult() - .setResult(resultRepository.findByIdWithEagerSubmissionAndFeedbacksAndAssessor(conflict.getCausingConflictingResult().getResult().getId()).get()); + .setResult(resultRepository.findWithEagerSubmissionAndFeedbackAndAssessorById(conflict.getCausingConflictingResult().getResult().getId()).get()); conflict.getResultsInConflict().forEach(conflictingResult -> conflictingResult - .setResult(resultRepository.findByIdWithEagerSubmissionAndFeedbacksAndAssessor(conflictingResult.getResult().getId()).get())); + .setResult(resultRepository.findWithEagerSubmissionAndFeedbackAndAssessorById(conflictingResult.getResult().getId()).get())); }); } diff --git a/src/main/java/de/tum/in/www1/artemis/service/ModelingAssessmentService.java b/src/main/java/de/tum/in/www1/artemis/service/ModelingAssessmentService.java index 7b150b93ef94..6e6e8e16da1e 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/ModelingAssessmentService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/ModelingAssessmentService.java @@ -47,23 +47,24 @@ public ModelingAssessmentService(UserService userService, ComplaintResponseServi } /** - * This function is used for submitting a manual assessment/result. It updates the completion date, sets the assessment type to MANUAL and sets the assessor attribute. - * Furthermore, it saves the result in the database. + * This function is used for submitting a manual assessment/result. It gets the result that belongs to the given resultId, updates the completion date, sets the assessment type + * to MANUAL and sets the assessor attribute. Afterwards, it saves the update result in the database again. * - * @param result the result the assessment belongs to + * @param resultId the id of the result that should be submitted * @param exercise the exercise the assessment belongs to * @param submissionDate the date manual assessment was submitted * @return the ResponseEntity with result as body */ @Transactional - public Result submitManualAssessment(Result result, ModelingExercise exercise, ZonedDateTime submissionDate) { + public Result submitManualAssessment(long resultId, ModelingExercise exercise, ZonedDateTime submissionDate) { // TODO CZ: use AssessmentService#submitResult() function instead + Result result = resultRepository.findWithEagerSubmissionAndFeedbackAndAssessorById(resultId) + .orElseThrow(() -> new EntityNotFoundException("No result for the given resultId could be found")); result.setRatedIfNotExceeded(exercise.getDueDate(), submissionDate); result.setCompletionDate(ZonedDateTime.now()); - result.evaluateFeedback(exercise.getMaxScore()); // TODO CZ: move to AssessmentService class, as it's the same for - // modeling and text exercises (i.e. total score is sum of feedback credits) - resultRepository.save(result); - return result; + result.evaluateFeedback(exercise.getMaxScore()); // TODO CZ: move to AssessmentService class, as it's the same for modeling and text exercises (i.e. total score is sum of + // feedback credits) + return resultRepository.save(result); } /** @@ -101,8 +102,7 @@ public Result saveManualAssessment(ModelingSubmission modelingSubmission, List saveModelingAssessment(@PathVariable Long submissi // return ResponseEntity.status(HttpStatus.CONFLICT).body(conflicts); // } // else { - modelingAssessmentService.submitManualAssessment(result, modelingExercise, modelingSubmission.getSubmissionDate()); + result = modelingAssessmentService.submitManualAssessment(result.getId(), modelingExercise, modelingSubmission.getSubmissionDate()); if (compassService.isSupported(modelingExercise.getDiagramType())) { compassService.addAssessment(exerciseId, submissionId, result.getFeedbacks()); } diff --git a/src/test/java/de/tum/in/www1/artemis/util/DatabaseUtilService.java b/src/test/java/de/tum/in/www1/artemis/util/DatabaseUtilService.java index ed6528926e9b..bc491690d5c1 100644 --- a/src/test/java/de/tum/in/www1/artemis/util/DatabaseUtilService.java +++ b/src/test/java/de/tum/in/www1/artemis/util/DatabaseUtilService.java @@ -585,10 +585,11 @@ public Result addModelingAssessmentForSubmission(ModelingExercise exercise, Mode Result result = modelingAssessmentService.saveManualAssessment(submission, assessment, exercise); result.setParticipation(submission.getParticipation().results(null)); result.setAssessor(getUserByLogin(login)); + resultRepo.save(result); if (submit) { - result = modelingAssessmentService.submitManualAssessment(result, exercise, submission.getSubmissionDate()); + modelingAssessmentService.submitManualAssessment(result.getId(), exercise, submission.getSubmissionDate()); } - return result; + return resultRepo.findWithEagerSubmissionAndFeedbackAndAssessorById(result.getId()).get(); } public ExampleSubmission addExampleSubmission(ExampleSubmission exampleSubmission, String login) {