diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LessonRequestsController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LessonRequestsController.java index 2facbc0..ab2e72f 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LessonRequestsController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LessonRequestsController.java @@ -59,10 +59,13 @@ public class LessonRequestsController { } @PostMapping("/requests/lessonRequest") - public ResponseEntity> makeRequest(@RequestBody LessonChangesRequest lessonRequest){ - System.out.println(lessonRequest.getLesson()); - System.out.println(lessonRequest.getLessonEnd()); - LessonChangesRequest lessonChangesRequest = lessonRequestServ.save(lessonRequest); + public ResponseEntity> makeRequest(@RequestHeader("Authorization") String token, @RequestBody Map lessonRequestInfos){ + if(authServ.isNotIn(new Role[]{Role.Admin,Role.Teacher},token)) + return new UnauthorizedResponse<>(null); + LessonChangesRequest lessonChangesRequest = lessonRequestServ.createLessonRequest(lessonRequestInfos); + LessonChangesRequest createdRequest = lessonRequestServ.save(lessonChangesRequest); + if(createdRequest == null) + return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); return new ResponseEntity<>(ProtectionService.lessonRequestWithoutPassword(lessonChangesRequest),HttpStatus.OK); } @@ -98,13 +101,13 @@ public class LessonRequestsController { infos.put("lessonStart", lessonRequest.getLessonStart()); infos.put("lessonEnd", lessonRequest.getLessonEnd()); infos.put("lessonType",lessonRequest.getLessonType()); - if(!lessonRequestServ.modifyChangeRequestState(infos,lessonRequest.getLesson(),state)) + if(!lessonRequestServ.modifyChangeRequestState(infos,lessonRequest.getLessonId(),state)) return new ResponseEntity<>(HttpStatus.BAD_REQUEST); lessonRequest.setState(state); } else{ - lessonRequestServ.modifyDeleleRequest(lessonRequest, state); + lessonRequestServ.modifyDeleteRequest(lessonRequest, state); } lessonRequestServ.save(lessonRequest); return new ResponseEntity<>(HttpStatus.OK); diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/LessonRequestService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/LessonRequestService.java index 24c0a7c..63802fb 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/LessonRequestService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/LessonRequestService.java @@ -4,7 +4,6 @@ import org.springframework.stereotype.Service; import ovh.herisson.Clyde.Repositories.*; import ovh.herisson.Clyde.Tables.*; -import java.sql.SQLOutput; import java.util.Map; @Service @@ -23,10 +22,14 @@ public class LessonRequestService { private final ScheduleLessonService scheduleLessonService; + private final UserService userServ; + private final CourseRepository courseRepository; public LessonRequestService(LessonChangesRequestRepository lessonChangesRepo, UserRepository userRepo, LessonRepository lessonRepo, - LessonService lessonServ, ScheduleLessonRepository scheduleLessonRepo, ScheduleRepository scheduleRepository, ScheduleLessonService scheduleLessonService, CourseRepository courseRepository) { + LessonService lessonServ, ScheduleLessonRepository scheduleLessonRepo, + ScheduleRepository scheduleRepository, ScheduleLessonService scheduleLessonService, + UserService userServ, CourseRepository courseRepository) { this.lessonChangesRepo = lessonChangesRepo; this.userRepo = userRepo; this.lessonRepo = lessonRepo; @@ -34,6 +37,7 @@ public class LessonRequestService { this.scheduleLessonRepo = scheduleLessonRepo; this.scheduleRepository = scheduleRepository; this.scheduleLessonService = scheduleLessonService; + this.userServ = userServ; this.courseRepository = courseRepository; } public Iterable findOwnRequests(User user){ @@ -54,13 +58,14 @@ public class LessonRequestService { Course course = courseRepository.findById(lessonRequest.getCourse().getCourseID()); if(courseRepository.findById(lessonRequest.getCourse().getCourseID())==null|| local == null){ return false;} - Lesson lesson = new Lesson( - course, - lessonRequest.getLessonStart(), - lessonRequest.getLessonEnd(), - lessonRequest.getColor(), - local, - lessonRequest.getLessonType()); + Lesson lesson = new Lesson(); + lesson.setCourse(course); + lesson.setLessonStart(lessonRequest.getLessonStart()); + lesson.setLessonEnd(lessonRequest.getLessonEnd()); + lesson.setColor(lessonRequest.getColor()); + lesson.setLocal(local); + lesson.setLessonType(lessonRequest.getLessonType()); + lesson = lessonRepo.save(lesson); scheduleLessonService.saveToAllSchedule(lesson); } @@ -71,7 +76,7 @@ public class LessonRequestService { public boolean modifyChangeRequestState(Map updates, long lessonId,RequestState state){ if(state == RequestState.Accepted){ - System.out.println(updates.toString()); + Lesson lesson = lessonServ.findById(lessonId); return lessonServ.modifyData(lesson.getLessonID(),updates); @@ -79,12 +84,52 @@ public class LessonRequestService { return true; } - public void modifyDeleleRequest(LessonChangesRequest lessonChangesRequest, RequestState state){ + public void modifyDeleteRequest(LessonChangesRequest lessonChangesRequest, RequestState state){ if(state == RequestState.Accepted){ - lessonServ.delete(lessonServ.findById(lessonChangesRequest.getLesson())); + lessonServ.delete(lessonServ.findById(lessonChangesRequest.getLessonId())); lessonChangesRequest.setState(state);} } + public LessonChangesRequest createLessonRequest(Map lessonInfos) { + LessonChangesRequest target = new LessonChangesRequest(); + + for (Map.Entry entry : lessonInfos.entrySet()) { + System.out.println(entry.toString()); + if(entry.getValue() != null){ + switch (entry.getKey()) { + case "requestType": + target.setRequestType((int) entry.getValue()); + break; + case "lessonStart": + target.setLessonStart((String) entry.getValue()); + break; + case "lessonEnd": + target.setLessonEnd((String) entry.getValue()); + break; + case "color": + target.setColor((String) entry.getValue()); + break; + case "user": + target.setUser(userServ.getUserById((int) entry.getValue())); + break; + case "lessonType": + target.setLessonType((String) entry.getValue()); + break; + case "course": + target.setCourse(courseRepository.findById((int) entry.getValue())); + break; + case "lessonId": + target.setLessonId((int) entry.getValue()); + break; + + } + } + } + target.setState(RequestState.Pending); + return target; + } + + public void delete (LessonChangesRequest toDelete) { diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/LessonService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/LessonService.java index 3436e2c..7a0fa12 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/LessonService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/LessonService.java @@ -60,6 +60,7 @@ public class LessonService { break; case "courseId": target.setCourse(courseRepo.findById((int) entry.getValue())); + break; } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java index 3b8bca2..34d4173 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java @@ -128,7 +128,7 @@ public class ProtectionService { toReturn.put("user", userWithoutPassword(lessonRequest.getUser())); toReturn.put("requestType", lessonRequest.getRequestType()); toReturn.put("state", lessonRequest.getState()); - toReturn.put("lessonId",lessonRequest.getLesson()); + toReturn.put("lessonId",lessonRequest.getLessonId()); return toReturn; } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/LessonChangesRequest.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/LessonChangesRequest.java index 752421b..6258f2b 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/LessonChangesRequest.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/LessonChangesRequest.java @@ -25,7 +25,7 @@ public class LessonChangesRequest { private String lessonEnd; private String color; - @OneToOne + @ManyToOne @JoinColumn(name ="Course") private Course course; @@ -64,7 +64,7 @@ public class LessonChangesRequest { return user; } - public long getLesson(){ + public long getLessonId(){ return lessonId; } @@ -123,6 +123,9 @@ public class LessonChangesRequest { } + public void setLessonId(long lessonId) { + this.lessonId = lessonId; + } public void setCourse(Course course){ this.course = course; diff --git a/frontend/src/Apps/LessonRequests.vue b/frontend/src/Apps/LessonRequests.vue index 2a8270d..9540e89 100644 --- a/frontend/src/Apps/LessonRequests.vue +++ b/frontend/src/Apps/LessonRequests.vue @@ -83,7 +83,7 @@ async function setMoreInfos(item){ -