Skip to content

Commit

Permalink
Add CompetencyRepositoryService with default methods of CompetencyRep…
Browse files Browse the repository at this point in the history
…ository
  • Loading branch information
ole-ve committed Nov 17, 2024
1 parent 59bc157 commit 5f4c963
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ public interface CompetencyRepository extends ArtemisJpaRepository<Competency, L
""")
Optional<Competency> findByIdWithLectureUnitsAndExercises(@Param("competencyId") long competencyId);

default Competency findByIdWithLectureUnitsAndExercisesElseThrow(long competencyId) {
return getValueElseThrow(findByIdWithLectureUnitsAndExercises(competencyId), competencyId);
}

default Competency findByIdWithLectureUnitsElseThrow(long competencyId) {
return getValueElseThrow(findByIdWithLectureUnits(competencyId), competencyId);
}

long countByCourse(Course course);

List<Competency> findByCourseIdOrderById(long courseId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.tum.cit.aet.artemis.atlas.service.competency;

import static de.tum.cit.aet.artemis.core.config.Constants.PROFILE_CORE;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import de.tum.cit.aet.artemis.atlas.domain.competency.Competency;
import de.tum.cit.aet.artemis.atlas.repository.CompetencyRepository;
import de.tum.cit.aet.artemis.core.service.AbstractRepositoryService;

@Profile(PROFILE_CORE)
@Service
public class CompetencyRepositoryService extends AbstractRepositoryService {

private final CompetencyRepository competencyRepository;

public CompetencyRepositoryService(CompetencyRepository competencyRepository) {
this.competencyRepository = competencyRepository;
}

public Competency findByIdWithLectureUnitsAndExercisesElseThrow(long competencyId) {
return getValueElseThrow(competencyRepository.findByIdWithLectureUnitsAndExercises(competencyId), competencyId);
}

public Competency findByIdWithLectureUnitsElseThrow(long competencyId) {
return getValueElseThrow(competencyRepository.findByIdWithLectureUnits(competencyId), competencyId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@ public class CompetencyService extends CourseCompetencyService {

private final CompetencyRepository competencyRepository;

private final CompetencyRepositoryService competencyRepositoryService;

public CompetencyService(CompetencyRepository competencyRepository, AuthorizationCheckService authCheckService, CompetencyRelationRepository competencyRelationRepository,
LearningPathService learningPathService, CompetencyProgressService competencyProgressService, LectureUnitService lectureUnitService,
CompetencyProgressRepository competencyProgressRepository, LectureUnitCompletionRepository lectureUnitCompletionRepository,
StandardizedCompetencyRepository standardizedCompetencyRepository, CourseCompetencyRepository courseCompetencyRepository, ExerciseService exerciseService,
LearningObjectImportService learningObjectImportService, CompetencyLectureUnitLinkRepository competencyLectureUnitLinkRepository, CourseRepository courseRepository) {
LearningObjectImportService learningObjectImportService, CompetencyLectureUnitLinkRepository competencyLectureUnitLinkRepository, CourseRepository courseRepository,
CompetencyRepositoryService competencyRepositoryService) {
super(competencyProgressRepository, courseCompetencyRepository, competencyRelationRepository, competencyProgressService, exerciseService, lectureUnitService,
learningPathService, authCheckService, standardizedCompetencyRepository, lectureUnitCompletionRepository, learningObjectImportService,
competencyLectureUnitLinkRepository, courseRepository);
this.competencyRepository = competencyRepository;
this.competencyRepositoryService = competencyRepositoryService;
}

/**
Expand Down Expand Up @@ -104,7 +108,7 @@ public List<Competency> createCompetencies(List<Competency> competencies, Course
* @return The found competency
*/
public Competency findCompetencyWithExercisesAndLectureUnitsAndProgressForUser(Long competencyId, Long userId) {
Competency competency = competencyRepository.findByIdWithLectureUnitsAndExercisesElseThrow(competencyId);
Competency competency = competencyRepositoryService.findByIdWithLectureUnitsAndExercisesElseThrow(competencyId);
return findProgressAndLectureUnitCompletionsForUser(competency, userId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import de.tum.cit.aet.artemis.atlas.dto.CompetencyWithTailRelationDTO;
import de.tum.cit.aet.artemis.atlas.repository.CompetencyRepository;
import de.tum.cit.aet.artemis.atlas.repository.CourseCompetencyRepository;
import de.tum.cit.aet.artemis.atlas.service.competency.CompetencyRepositoryService;
import de.tum.cit.aet.artemis.atlas.service.competency.CompetencyService;
import de.tum.cit.aet.artemis.atlas.service.competency.CourseCompetencyService;
import de.tum.cit.aet.artemis.core.domain.Course;
Expand Down Expand Up @@ -70,16 +71,19 @@ public class CompetencyResource {

private final CourseCompetencyService courseCompetencyService;

private final CompetencyRepositoryService competencyRepositoryService;

public CompetencyResource(CourseRepository courseRepository, AuthorizationCheckService authorizationCheckService, UserRepository userRepository,
CompetencyRepository competencyRepository, CompetencyService competencyService, CourseCompetencyRepository courseCompetencyRepository,
CourseCompetencyService courseCompetencyService) {
CourseCompetencyService courseCompetencyService, CompetencyRepositoryService competencyRepositoryService) {
this.courseRepository = courseRepository;
this.authorizationCheckService = authorizationCheckService;
this.userRepository = userRepository;
this.competencyRepository = competencyRepository;
this.competencyService = competencyService;
this.courseCompetencyRepository = courseCompetencyRepository;
this.courseCompetencyService = courseCompetencyService;
this.competencyRepositoryService = competencyRepositoryService;
}

/**
Expand Down Expand Up @@ -293,7 +297,7 @@ public ResponseEntity<Competency> updateCompetency(@PathVariable long courseId,
checkCompetencyAttributesForUpdate(competency);

var course = courseRepository.findByIdElseThrow(courseId);
var existingCompetency = competencyRepository.findByIdWithLectureUnitsElseThrow(competency.getId());
var existingCompetency = competencyRepositoryService.findByIdWithLectureUnitsElseThrow(competency.getId());
checkCourseForCompetency(course, existingCompetency);

var persistedCompetency = competencyService.updateCourseCompetency(existingCompetency, competency);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.tum.cit.aet.artemis.core.service;

import java.util.Optional;

import de.tum.cit.aet.artemis.core.exception.EntityNotFoundException;

public abstract class AbstractRepositoryService {

public <U> U getValueElseThrow(Optional<U> optional) {
if (optional.isEmpty()) {
throw new EntityNotFoundException(optional.getClass().getName()); // ToDo: Check validity of optional anme
}

return optional.get();
}

public <U> U getValueElseThrow(Optional<U> optional, long id) {
if (optional.isEmpty()) {
throw new EntityNotFoundException(optional.getClass().getName(), id); // ToDo: Check validity of optional anme
}

return optional.get();
}
}

0 comments on commit 5f4c963

Please sign in to comment.