Merge branch 'Max/Backend/CourseInCurriculum' into Max/Backend/ReturnUserPasswordIssue
This commit is contained in:
		| @ -42,6 +42,15 @@ public class CourseController { | ||||
|         return new ResponseEntity<>(courseWithoutPassword(foundCourse), HttpStatus.OK); | ||||
|     } | ||||
|  | ||||
|     @GetMapping("/courses") | ||||
|     public ResponseEntity<Iterable<Course>> 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<Course> postCourse(@RequestHeader("Authorization") String token, | ||||
| @ -51,7 +60,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); | ||||
|     } | ||||
|  | ||||
|  | ||||
|  | ||||
| @ -30,13 +30,13 @@ public class CurriculumController { | ||||
|     } | ||||
|  | ||||
|     @GetMapping("/curriculum/{id}") | ||||
|     public ResponseEntity<Curriculum> findById(@PathVariable long id){ | ||||
|     public ResponseEntity<Map<String,Object>> 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<String> postCoursesToCurriculum(@RequestHeader("Authorization") String token, | ||||
|                                                           @RequestBody Iterable<Long> 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); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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<Course> findAll() { | ||||
|         return courseRepo.findAll(); | ||||
|     } | ||||
|  | ||||
|     public boolean modifyData(long id, Map<String, Object> updates, Role role) { | ||||
|         Course target = courseRepo.findById(id); | ||||
|  | ||||
| @ -62,4 +69,5 @@ public class CourseService { | ||||
|         courseRepo.save(target); | ||||
|         return true; | ||||
|     } | ||||
|  | ||||
| } | ||||
|  | ||||
| @ -1,10 +1,11 @@ | ||||
| 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.Repositories.CurriculumRepository; | ||||
| import ovh.herisson.Clyde.Tables.*; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| @ -14,9 +15,14 @@ public class CurriculumCourseService { | ||||
|  | ||||
|     private final CurriculumCourseRepository curriculumCourseRepo; | ||||
|  | ||||
|     private final CourseRepository courseRepo; | ||||
|  | ||||
|     public CurriculumCourseService(CurriculumCourseRepository curriculumCourseRepository) { | ||||
|     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){ | ||||
| @ -31,7 +37,9 @@ public class CurriculumCourseService { | ||||
|  | ||||
|         HashMap<String ,Object> toReturn = new HashMap<>(); | ||||
|         ArrayList<Course> courses = new ArrayList<>(); | ||||
|         for (Course c: curriculumCourseRepo.findCoursesByCurriculum(curriculum)){ | ||||
|         Iterable<Course> foundCourses = curriculumCourseRepo.findCoursesByCurriculum(curriculum); | ||||
|  | ||||
|         for (Course c: foundCourses){ | ||||
|             courses.add(c); | ||||
|         } | ||||
|         toReturn.put("courses",courses); | ||||
| @ -47,9 +55,40 @@ public class CurriculumCourseService { | ||||
|  | ||||
|         ArrayList<Map<String,Object>> toReturn = new ArrayList<>(); | ||||
|  | ||||
|         for (Curriculum curriculum : curriculumCourseRepo.findDistinctCurriculums()){ | ||||
|         for (Curriculum curriculum : curriculumRepo.findAll()){ | ||||
|             toReturn.add(getDepthCurriculum(curriculum)); | ||||
|         } | ||||
|  | ||||
|  | ||||
|         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<Long> coursesIds, Curriculum curriculum) { | ||||
|  | ||||
|         if (curriculum == null || coursesIds == null) | ||||
|             return false; | ||||
|  | ||||
|         ArrayList<Course> 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; | ||||
|  | ||||
|     } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user