From fd357ba938215fd5e367d4f1204a6f1adb20ecfa Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Mon, 18 Mar 2024 12:15:13 +0100 Subject: [PATCH] GET /curriculum returns user's curriculum --- .../Clyde/EndPoints/CurriculumController.java | 21 +++++++++++++++---- .../UserCurriculumRepository.java | 6 ++++++ .../Clyde/Services/UserCurriculumService.java | 20 ++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Services/UserCurriculumService.java diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java index dda0c63..409e269 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java @@ -5,9 +5,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import ovh.herisson.Clyde.Responses.UnauthorizedResponse; -import ovh.herisson.Clyde.Services.AuthenticatorService; -import ovh.herisson.Clyde.Services.CurriculumCourseService; -import ovh.herisson.Clyde.Services.CurriculumService; +import ovh.herisson.Clyde.Services.*; import ovh.herisson.Clyde.Tables.Curriculum; import ovh.herisson.Clyde.Tables.Role; @@ -21,11 +19,13 @@ public class CurriculumController { private final CurriculumService curriculumServ; private final AuthenticatorService authServ; + private final UserCurriculumService userCurriculumServ; private final CurriculumCourseService curriculumCourseServ; - public CurriculumController(CurriculumService curriculumServ, AuthenticatorService authServ, CurriculumCourseService curriculumCourseServ){ + public CurriculumController(CurriculumService curriculumServ, AuthenticatorService authServ, UserCurriculumService userCurriculumServ, CurriculumCourseService curriculumCourseServ){ this.curriculumServ = curriculumServ; this.authServ = authServ; + this.userCurriculumServ = userCurriculumServ; this.curriculumCourseServ = curriculumCourseServ; } @@ -39,6 +39,19 @@ public class CurriculumController { return new ResponseEntity<>(curriculumCourseServ.getDepthCurriculum(foundCurriculum), HttpStatus.OK); } + @GetMapping("/curriculum") + public ResponseEntity> findSelfCurriculum(@RequestHeader("Authorization") String token){ + if (authServ.getUserFromToken(token) == null) + return new UnauthorizedResponse<>(null); + + Curriculum curriculum = userCurriculumServ.findByUser(authServ.getUserFromToken(token)); + + if (curriculum == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + return new ResponseEntity<>(curriculumCourseServ.getDepthCurriculum(curriculum),HttpStatus.OK); + } + @GetMapping("/curriculums") public ResponseEntity>> findAllIndDepth(){ return new ResponseEntity<>(curriculumCourseServ.getAllDepthCurriculum(),HttpStatus.OK); diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java index 93cb10f..32f207a 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/UserCurriculumRepository.java @@ -1,7 +1,13 @@ package ovh.herisson.Clyde.Repositories; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; +import ovh.herisson.Clyde.Tables.Curriculum; +import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.UserCurriculum; public interface UserCurriculumRepository extends CrudRepository { + + @Query("select uc.curriculum from UserCurriculum uc where uc.user = ?1") + Curriculum findByUser(User student); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/UserCurriculumService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/UserCurriculumService.java new file mode 100644 index 0000000..6484e2b --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserCurriculumService.java @@ -0,0 +1,20 @@ +package ovh.herisson.Clyde.Services; + +import org.springframework.stereotype.Service; +import ovh.herisson.Clyde.Repositories.UserCurriculumRepository; +import ovh.herisson.Clyde.Tables.Curriculum; +import ovh.herisson.Clyde.Tables.User; + +@Service +public class UserCurriculumService { + + private final UserCurriculumRepository userCurriculumRepository; + + public UserCurriculumService(UserCurriculumRepository userCurriculumRepository) { + this.userCurriculumRepository = userCurriculumRepository; + } + + public Curriculum findByUser(User student){ + return userCurriculumRepository.findByUser(student); + } +}