diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java new file mode 100644 index 0000000..011689f --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java @@ -0,0 +1,80 @@ +package ovh.herisson.Clyde.EndPoints; + +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.CourseService; +import ovh.herisson.Clyde.Services.TeacherCourseService; +import ovh.herisson.Clyde.Tables.Course; +import ovh.herisson.Clyde.Tables.Role; +import ovh.herisson.Clyde.Tables.TeacherCourse; +import ovh.herisson.Clyde.Tables.User; + +import java.util.ArrayList; +import java.util.Map; + +@RestController +@CrossOrigin(originPatterns = "*", allowCredentials = "true") +public class CourseController { + + private final CourseService courseServ; + + private final TeacherCourseService teacherCourseServ; + + private final AuthenticatorService authServ; + + public CourseController(CourseService courseServ, TeacherCourseService teacherCourseServ, AuthenticatorService authServ) { + this.courseServ = courseServ; + this.teacherCourseServ = teacherCourseServ; + this.authServ = authServ; + } + + @GetMapping("/course/{id}") + public ResponseEntity getCourse(@RequestHeader("Authorization") String token, @PathVariable long id){ + if (authServ.getUserFromToken(token) == null) + return new UnauthorizedResponse<>(null); + + return new ResponseEntity<>(courseServ.findById(id), HttpStatus.OK); + } + + + @PostMapping("/course") + public ResponseEntity postCourse(@RequestHeader("Authorization") String token, @RequestBody Course course){ + if (authServ.isNotSecretaryOrAdmin(token)) + return new UnauthorizedResponse<>(null); + + return new ResponseEntity<>(courseServ.save(course), HttpStatus.CREATED); + } + + + @PatchMapping("/course/{id}") + public ResponseEntity patchCourse(@RequestHeader("Authorization") String token, + @RequestBody Map updates, + @PathVariable long id) + { + + if (authServ.IsNotIn(new Role[]{Role.Admin,Role.Teacher,Role.Secretary}, token)){ + return new UnauthorizedResponse<>(null); + } + + return new ResponseEntity<>(courseServ.modifyData(id, updates, authServ.getUserFromToken(token).getRole()), HttpStatus.OK); + } + + @PostMapping("/course/{id}") + public ResponseEntity postTeachers(@RequestHeader("Authorization") String token, + @RequestBody Iterable teacherIds, + @PathVariable Long id) + { + if (authServ.IsNotIn(new Role[]{Role.Admin,Role.Secretary}, token)) + return new UnauthorizedResponse<>(null); + + + + teacherCourseServ.saveAll(teacherIds,courseServ.findById(id)); + return new ResponseEntity<>(HttpStatus.OK); + + } + +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/TeacherCourseRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/TeacherCourseRepository.java new file mode 100644 index 0000000..ffe654a --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/TeacherCourseRepository.java @@ -0,0 +1,8 @@ +package ovh.herisson.Clyde.Repositories; + + +import org.springframework.data.repository.CrudRepository; +import ovh.herisson.Clyde.Tables.TeacherCourse; + +public interface TeacherCourseRepository extends CrudRepository { +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java index a3301c1..63ef3c1 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java @@ -50,5 +50,18 @@ public class AuthenticatorService { return poster.getRole() != Role.Secretary || poster.getRole() != Role.Admin; } + public boolean IsNotIn(Role[] roles, String token){ + if (token == null) + return true; + + User poster = getUserFromToken(token); + if (poster == null) return true; + + for (Role r:roles){ + if (poster.getRole() == r) + return false; + } + return true; + } } 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 368dbb2..8278bbe 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/CourseService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/CourseService.java @@ -3,6 +3,10 @@ package ovh.herisson.Clyde.Services; import org.springframework.stereotype.Service; import ovh.herisson.Clyde.Repositories.CourseRepository; import ovh.herisson.Clyde.Tables.Course; +import ovh.herisson.Clyde.Tables.Role; +import ovh.herisson.Clyde.Tables.User; + +import java.util.Map; @Service public class CourseService { @@ -13,11 +17,39 @@ public class CourseService { this.courseRepo = courseRepo; } - public void save(Course course){ - courseRepo.save(course); + public Course save(Course course){ + return courseRepo.save(course); } public Course findById(long id){ return courseRepo.findById(id); } + + public Course modifyData(long id, Map updates, Role role) { + Course target = courseRepo.findById(id); + + if (role == Role.Teacher){ + for (Map.Entry entry : updates.entrySet()){ + if (entry.getKey().equals("title")){ + target.setTitle((String) entry.getValue()); + return courseRepo.save(target); + } + } + } + + for (Map.Entry entry: updates.entrySet()){ + switch (entry.getKey()){ + case "title": + target.setTitle((String) entry.getValue()); + break; + case "credits": + target.setCredits((Integer) entry.getValue()); + break; + case "owner": + target.setOwner((User) entry.getValue()); //todo check if is a teacher ! + break; + } + } + return courseRepo.save(target); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/TeacherCourseService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/TeacherCourseService.java new file mode 100644 index 0000000..0996adf --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/TeacherCourseService.java @@ -0,0 +1,39 @@ +package ovh.herisson.Clyde.Services; + +import org.springframework.stereotype.Controller; +import ovh.herisson.Clyde.Repositories.TeacherCourseRepository; +import ovh.herisson.Clyde.Repositories.UserRepository; +import ovh.herisson.Clyde.Tables.Course; +import ovh.herisson.Clyde.Tables.TeacherCourse; +import ovh.herisson.Clyde.Tables.User; + +import java.util.ArrayList; + +@Controller +public class TeacherCourseService { + private final TeacherCourseRepository teacherCourseRepo; + + private final UserRepository userRepo; + + public TeacherCourseService(TeacherCourseRepository teacherCourseRepo, UserRepository userRepo) { + this.teacherCourseRepo = teacherCourseRepo; + this.userRepo = userRepo; + } + + public boolean saveAll(Iterable teacherIds, Course course){ + + ArrayList addedIds = new ArrayList<>(); + for (Long teacherId : teacherIds){ + User teacher = userRepo.findById((long) teacherId); + if ( teacher== null){ + return false; + } + if (!addedIds.contains(teacherId)) + { + teacherCourseRepo.save(new TeacherCourse(teacher,course)); + addedIds.add(teacherId); + } + } + return true; + } +}