From 474a8d3f31f1b7da6642ce6f4cf55d3875c75e58 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sun, 17 Mar 2024 02:00:42 +0100 Subject: [PATCH 1/3] added POST /curriculum/{id} endopoint to post courses --- .../Clyde/EndPoints/CurriculumController.java | 19 ++++++++- .../Services/CurriculumCourseService.java | 42 ++++++++++++++++--- 2 files changed, 54 insertions(+), 7 deletions(-) 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 4cb9504..7a1bfe4 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CurriculumController.java @@ -30,13 +30,13 @@ public class CurriculumController { } @GetMapping("/curriculum/{id}") - public ResponseEntity findById(@PathVariable long id){ + public ResponseEntity> findById(@PathVariable long id){ Curriculum foundCurriculum = curriculumServ.findById(id); if (foundCurriculum == null) return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - return new ResponseEntity<>(foundCurriculum, HttpStatus.OK); + return new ResponseEntity<>(curriculumCourseServ.getDepthCurriculum(foundCurriculum), HttpStatus.OK); } @GetMapping("/curriculums") @@ -52,4 +52,19 @@ public class CurriculumController { return new ResponseEntity<>(curriculumServ.save(curriculum),HttpStatus.CREATED); } + + @PostMapping("/curriculum/{id}") + public ResponseEntity postCoursesToCurriculum(@RequestHeader("Authorization") String token, + @RequestBody Iterable coursesIds, + @PathVariable long id) + { + + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) + return new UnauthorizedResponse<>(null); + + if (!curriculumCourseServ.saveAll(coursesIds, curriculumServ.findById(id))) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + return new ResponseEntity<>(HttpStatus.OK); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumCourseService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumCourseService.java index 5e1992d..0173a05 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumCourseService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumCourseService.java @@ -1,10 +1,10 @@ package ovh.herisson.Clyde.Services; import org.springframework.stereotype.Service; +import ovh.herisson.Clyde.Repositories.CourseRepository; import ovh.herisson.Clyde.Repositories.CurriculumCourseRepository; -import ovh.herisson.Clyde.Tables.Course; -import ovh.herisson.Clyde.Tables.Curriculum; -import ovh.herisson.Clyde.Tables.CurriculumCourse; +import ovh.herisson.Clyde.Tables.*; + import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -14,9 +14,11 @@ public class CurriculumCourseService { private final CurriculumCourseRepository curriculumCourseRepo; + private final CourseRepository courseRepo; - public CurriculumCourseService(CurriculumCourseRepository curriculumCourseRepository) { + public CurriculumCourseService(CurriculumCourseRepository curriculumCourseRepository, CourseRepository courseRepo) { this.curriculumCourseRepo = curriculumCourseRepository; + this.courseRepo = courseRepo; } public void save(CurriculumCourse curriculumCourse){ @@ -31,7 +33,9 @@ public class CurriculumCourseService { HashMap toReturn = new HashMap<>(); ArrayList courses = new ArrayList<>(); - for (Course c: curriculumCourseRepo.findCoursesByCurriculum(curriculum)){ + Iterable foundCourses = curriculumCourseRepo.findCoursesByCurriculum(curriculum); + + for (Course c: foundCourses){ courses.add(c); } toReturn.put("courses",courses); @@ -52,4 +56,32 @@ public class CurriculumCourseService { } return toReturn; } + + /** tries to add all courses to the curriculum + * + * @param coursesIds the ids of the courses to be added + * @param curriculum the curriculum to add the courses to + * @return if the changes were made + */ + public boolean saveAll(Iterable coursesIds, Curriculum curriculum) { + + if (curriculum == null || coursesIds == null) + return false; + + ArrayList toAdd = new ArrayList<>(); + for (Long courseId : coursesIds){ + Course course = courseRepo.findById((long) courseId); + if (course == null) + return false; + + if (!toAdd.contains(course)) + toAdd.add(course); + } + + for (Course course : toAdd){ + curriculumCourseRepo.save(new CurriculumCourse(curriculum,course)); + } + return true; + + } } From 6e6bd285afb6a9c55795aee454ce470a666b220a Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sun, 17 Mar 2024 02:15:08 +0100 Subject: [PATCH 2/3] added security to the post of course and GET /courses --- .../Clyde/EndPoints/CourseController.java | 15 ++++++++++++++- .../herisson/Clyde/Services/CourseService.java | 8 ++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java index ebfa730..60e7e1e 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java @@ -40,6 +40,15 @@ public class CourseController { return new ResponseEntity<>(foundCourse, HttpStatus.OK); } + @GetMapping("/courses") + public ResponseEntity> getAllCourses(@RequestHeader("Authorization") String token){ + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) + return new UnauthorizedResponse<>(null); + + + return new ResponseEntity<>(courseServ.findAll(),HttpStatus.OK); + } + @PostMapping("/course") public ResponseEntity postCourse(@RequestHeader("Authorization") String token, @@ -49,7 +58,11 @@ public class CourseController { if (authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token)) return new UnauthorizedResponse<>(null); - return new ResponseEntity<>(courseServ.save(course), HttpStatus.CREATED); + Course createdCourse = courseServ.save(course); + if (createdCourse == null) + return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST); + + return new ResponseEntity<>(createdCourse, HttpStatus.CREATED); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/CourseService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/CourseService.java index abfa6ae..bdb9ae8 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/CourseService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/CourseService.java @@ -17,6 +17,8 @@ public class CourseService { } public Course save(Course course){ + if (course.getOwner().getRole() != Role.Teacher) + return null; return courseRepo.save(course); } @@ -24,6 +26,11 @@ public class CourseService { return courseRepo.findById(id); } + + public Iterable findAll() { + return courseRepo.findAll(); + } + public boolean modifyData(long id, Map updates, Role role) { Course target = courseRepo.findById(id); @@ -62,4 +69,5 @@ public class CourseService { courseRepo.save(target); return true; } + } From 4cf2ac1aa8c0bf8ecfd540653519b3d1c4f0205b Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sun, 17 Mar 2024 02:34:00 +0100 Subject: [PATCH 3/3] fixed an issue with the getting of curriculums --- .../Clyde/Services/CurriculumCourseService.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumCourseService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumCourseService.java index 0173a05..a32e9d6 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumCourseService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/CurriculumCourseService.java @@ -3,6 +3,7 @@ package ovh.herisson.Clyde.Services; import org.springframework.stereotype.Service; import ovh.herisson.Clyde.Repositories.CourseRepository; import ovh.herisson.Clyde.Repositories.CurriculumCourseRepository; +import ovh.herisson.Clyde.Repositories.CurriculumRepository; import ovh.herisson.Clyde.Tables.*; import java.util.ArrayList; @@ -16,9 +17,12 @@ public class CurriculumCourseService { private final CourseRepository courseRepo; - public CurriculumCourseService(CurriculumCourseRepository curriculumCourseRepository, CourseRepository courseRepo) { + private final CurriculumRepository curriculumRepo; + + public CurriculumCourseService(CurriculumCourseRepository curriculumCourseRepository, CourseRepository courseRepo, CurriculumRepository curriculumRepo) { this.curriculumCourseRepo = curriculumCourseRepository; this.courseRepo = courseRepo; + this.curriculumRepo = curriculumRepo; } public void save(CurriculumCourse curriculumCourse){ @@ -51,9 +55,11 @@ public class CurriculumCourseService { ArrayList> toReturn = new ArrayList<>(); - for (Curriculum curriculum : curriculumCourseRepo.findDistinctCurriculums()){ + for (Curriculum curriculum : curriculumRepo.findAll()){ toReturn.add(getDepthCurriculum(curriculum)); } + + return toReturn; } @@ -70,6 +76,7 @@ public class CurriculumCourseService { ArrayList toAdd = new ArrayList<>(); for (Long courseId : coursesIds){ + Course course = courseRepo.findById((long) courseId); if (course == null) return false;