added POST /curriculum/{id} endopoint to post courses
This commit is contained in:
parent
c5d7ce4178
commit
474a8d3f31
@ -30,13 +30,13 @@ public class CurriculumController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/curriculum/{id}")
|
@GetMapping("/curriculum/{id}")
|
||||||
public ResponseEntity<Curriculum> findById(@PathVariable long id){
|
public ResponseEntity<Map<String,Object>> findById(@PathVariable long id){
|
||||||
Curriculum foundCurriculum = curriculumServ.findById(id);
|
Curriculum foundCurriculum = curriculumServ.findById(id);
|
||||||
|
|
||||||
if (foundCurriculum == null)
|
if (foundCurriculum == null)
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||||
|
|
||||||
return new ResponseEntity<>(foundCurriculum, HttpStatus.OK);
|
return new ResponseEntity<>(curriculumCourseServ.getDepthCurriculum(foundCurriculum), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/curriculums")
|
@GetMapping("/curriculums")
|
||||||
@ -52,4 +52,19 @@ public class CurriculumController {
|
|||||||
|
|
||||||
return new ResponseEntity<>(curriculumServ.save(curriculum),HttpStatus.CREATED);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package ovh.herisson.Clyde.Services;
|
package ovh.herisson.Clyde.Services;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import ovh.herisson.Clyde.Repositories.CourseRepository;
|
||||||
import ovh.herisson.Clyde.Repositories.CurriculumCourseRepository;
|
import ovh.herisson.Clyde.Repositories.CurriculumCourseRepository;
|
||||||
import ovh.herisson.Clyde.Tables.Course;
|
import ovh.herisson.Clyde.Tables.*;
|
||||||
import ovh.herisson.Clyde.Tables.Curriculum;
|
|
||||||
import ovh.herisson.Clyde.Tables.CurriculumCourse;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -14,9 +14,11 @@ public class CurriculumCourseService {
|
|||||||
|
|
||||||
private final CurriculumCourseRepository curriculumCourseRepo;
|
private final CurriculumCourseRepository curriculumCourseRepo;
|
||||||
|
|
||||||
|
private final CourseRepository courseRepo;
|
||||||
|
|
||||||
public CurriculumCourseService(CurriculumCourseRepository curriculumCourseRepository) {
|
public CurriculumCourseService(CurriculumCourseRepository curriculumCourseRepository, CourseRepository courseRepo) {
|
||||||
this.curriculumCourseRepo = curriculumCourseRepository;
|
this.curriculumCourseRepo = curriculumCourseRepository;
|
||||||
|
this.courseRepo = courseRepo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save(CurriculumCourse curriculumCourse){
|
public void save(CurriculumCourse curriculumCourse){
|
||||||
@ -31,7 +33,9 @@ public class CurriculumCourseService {
|
|||||||
|
|
||||||
HashMap<String ,Object> toReturn = new HashMap<>();
|
HashMap<String ,Object> toReturn = new HashMap<>();
|
||||||
ArrayList<Course> courses = new ArrayList<>();
|
ArrayList<Course> courses = new ArrayList<>();
|
||||||
for (Course c: curriculumCourseRepo.findCoursesByCurriculum(curriculum)){
|
Iterable<Course> foundCourses = curriculumCourseRepo.findCoursesByCurriculum(curriculum);
|
||||||
|
|
||||||
|
for (Course c: foundCourses){
|
||||||
courses.add(c);
|
courses.add(c);
|
||||||
}
|
}
|
||||||
toReturn.put("courses",courses);
|
toReturn.put("courses",courses);
|
||||||
@ -52,4 +56,32 @@ public class CurriculumCourseService {
|
|||||||
}
|
}
|
||||||
return toReturn;
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user