From 17ad3302fd26f212b58bb002640434e1b1d02345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20St=C3=B6hr?= Date: Thu, 4 Jul 2024 12:13:21 +0200 Subject: [PATCH] Fix and test issue --- .../repository/CompetencyRelationRepository.java | 9 +++++++++ .../in/www1/artemis/service/CourseService.java | 15 ++++++++++++--- .../lecture/CompetencyIntegrationTest.java | 10 +++++++--- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/tum/in/www1/artemis/repository/CompetencyRelationRepository.java b/src/main/java/de/tum/in/www1/artemis/repository/CompetencyRelationRepository.java index fca433daa2bb..8ccc837904a2 100644 --- a/src/main/java/de/tum/in/www1/artemis/repository/CompetencyRelationRepository.java +++ b/src/main/java/de/tum/in/www1/artemis/repository/CompetencyRelationRepository.java @@ -99,4 +99,13 @@ WITH RECURSIVE transitive_closure(id) AS Set getMatchingCompetenciesByCompetencyId(@Param("competencyId") long competencyId); Set findAllByHeadCompetencyIdInAndTailCompetencyIdIn(Set headCompetencyIds, Set tailCompetencyIds); + + @Transactional // ok because of delete + @Modifying + @Query(""" + DELETE FROM CompetencyRelation cr + WHERE cr.headCompetency.course.id = :courseId + OR cr.tailCompetency.course.id = :courseId + """) + void deleteAllByCourseId(@Param("courseId") long courseId); } diff --git a/src/main/java/de/tum/in/www1/artemis/service/CourseService.java b/src/main/java/de/tum/in/www1/artemis/service/CourseService.java index bf6a44d66142..a054e697199d 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/CourseService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/CourseService.java @@ -52,6 +52,7 @@ import de.tum.in.www1.artemis.domain.ProgrammingExercise; import de.tum.in.www1.artemis.domain.User; import de.tum.in.www1.artemis.domain.competency.Competency; +import de.tum.in.www1.artemis.domain.competency.Prerequisite; import de.tum.in.www1.artemis.domain.enumeration.IncludedInOverallScore; import de.tum.in.www1.artemis.domain.enumeration.NotificationType; import de.tum.in.www1.artemis.domain.exam.Exam; @@ -59,6 +60,7 @@ import de.tum.in.www1.artemis.domain.notification.GroupNotification; import de.tum.in.www1.artemis.domain.plagiarism.PlagiarismCase; import de.tum.in.www1.artemis.domain.statistics.StatisticsEntry; +import de.tum.in.www1.artemis.repository.CompetencyRelationRepository; import de.tum.in.www1.artemis.repository.CompetencyRepository; import de.tum.in.www1.artemis.repository.ComplaintRepository; import de.tum.in.www1.artemis.repository.ComplaintResponseRepository; @@ -109,12 +111,14 @@ @Service public class CourseService { - private final TutorialGroupChannelManagementService tutorialGroupChannelManagementService; + private static final Logger log = LoggerFactory.getLogger(CourseService.class); @Value("${artemis.course-archives-path}") private Path courseArchivesDirPath; - private static final Logger log = LoggerFactory.getLogger(CourseService.class); + private final TutorialGroupChannelManagementService tutorialGroupChannelManagementService; + + private final CompetencyRelationRepository competencyRelationRepository; private final ExerciseService exerciseService; @@ -202,7 +206,7 @@ public CourseService(CourseRepository courseRepository, ExerciseService exercise TutorialGroupRepository tutorialGroupRepository, PlagiarismCaseRepository plagiarismCaseRepository, ConversationRepository conversationRepository, LearningPathService learningPathService, Optional irisSettingsService, LectureRepository lectureRepository, TutorialGroupNotificationRepository tutorialGroupNotificationRepository, TutorialGroupChannelManagementService tutorialGroupChannelManagementService, - PrerequisiteRepository prerequisiteRepository) { + PrerequisiteRepository prerequisiteRepository, CompetencyRelationRepository competencyRelationRepository) { this.courseRepository = courseRepository; this.exerciseService = exerciseService; this.exerciseDeletionService = exerciseDeletionService; @@ -241,6 +245,7 @@ public CourseService(CourseRepository courseRepository, ExerciseService exercise this.tutorialGroupNotificationRepository = tutorialGroupNotificationRepository; this.tutorialGroupChannelManagementService = tutorialGroupChannelManagementService; this.prerequisiteRepository = prerequisiteRepository; + this.competencyRelationRepository = competencyRelationRepository; } /** @@ -528,6 +533,10 @@ private void deleteExercisesOfCourse(Course course) { } private void deleteCompetenciesOfCourse(Course course) { + competencyRelationRepository.deleteAllByCourseId(course.getId()); + for (Prerequisite prerequisite : course.getPrerequisites()) { + prerequisiteRepository.deleteById(prerequisite.getId()); + } for (Competency competency : course.getCompetencies()) { competencyRepository.deleteById(competency.getId()); } diff --git a/src/test/java/de/tum/in/www1/artemis/lecture/CompetencyIntegrationTest.java b/src/test/java/de/tum/in/www1/artemis/lecture/CompetencyIntegrationTest.java index f9d1ad5c5737..0ae0eb82c534 100644 --- a/src/test/java/de/tum/in/www1/artemis/lecture/CompetencyIntegrationTest.java +++ b/src/test/java/de/tum/in/www1/artemis/lecture/CompetencyIntegrationTest.java @@ -506,11 +506,15 @@ void shouldReturnForbiddenForInstructorOfOtherCourse() throws Exception { @Test @WithMockUser(username = "admin", roles = "ADMIN") - void deleteCourseShouldAlsoDeleteCompetency() throws Exception { + void deleteCourseShouldAlsoDeleteCompetencyAndRelations() throws Exception { + Competency competency2 = createCompetency(course); + CompetencyRelation relation = createRelation(competency, competency2, RelationType.EXTENDS); + request.delete("/api/admin/courses/" + course.getId(), HttpStatus.OK); - boolean competencyExists = competencyRepository.existsById(competency.getId()); - assertThat(competencyExists).isFalse(); + assertThat(competencyRepository.existsById(competency.getId())).isFalse(); + assertThat(competencyRepository.existsById(competency2.getId())).isFalse(); + assertThat(competencyRelationRepository.existsById(relation.getId())).isFalse(); } @Nested