From a2be04bfb325f2ac9ddf7969307954d43a348191 Mon Sep 17 00:00:00 2001 From: Wawilski Date: Tue, 16 Apr 2024 22:03:48 +0200 Subject: [PATCH] link backend Post Patch Delete Lesson --- .../Clyde/EndPoints/CourseController.java | 2 +- .../Clyde/EndPoints/LessonController.java | 25 ++- .../Clyde/EndPoints/MockController.java | 2 +- .../Clyde/EndPoints/ScheduleController.java | 18 +- .../ScheduleLessonRepository.java | 8 + .../Clyde/Services/LessonService.java | 47 +++- .../Clyde/Services/ScheduleLessonService.java | 8 + .../ovh/herisson/Clyde/Tables/Course.java | 7 +- .../ovh/herisson/Clyde/Tables/Lesson.java | 5 +- .../ovh/herisson/Clyde/Tables/Schedule.java | 1 + .../herisson/Clyde/Tables/ScheduleLesson.java | 2 + frontend/src/Apps/ManageCourses.vue | 4 +- frontend/src/Apps/ManageSchedule.vue | 206 +++++++++++++----- frontend/src/rest/lessonSchedule.js | 4 +- frontend/src/rest/restConsumer.js | 7 +- frontend/src/rest/scheduleRest.js | 11 +- frontend/src/scheduleFunctions.js | 4 +- 17 files changed, 282 insertions(+), 79 deletions(-) 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 40b1e0f..b11a075 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java @@ -76,7 +76,7 @@ public class CourseController { { if (authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token)) return new UnauthorizedResponse<>(null); - + System.out.println(course.getOwner().getRegNo()); Course createdCourse = courseServ.save(course); if (createdCourse == null) return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST); diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LessonController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LessonController.java index 63d7e24..8074abd 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LessonController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LessonController.java @@ -52,11 +52,14 @@ public class LessonController { @PostMapping("/lesson") - public ResponseEntity> postLesson(@RequestHeader("Authorization")String token, - @RequestBody Lesson lesson){ + public ResponseEntity> postLesson(@RequestHeader("Authorization") String token, + @RequestBody Map lessonInfos){ if(authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token)) return new UnauthorizedResponse<>(null); - Lesson createdLesson = lessonServ.save(lesson); + + + Lesson lesson = lessonServ.createLesson(lessonInfos); + Lesson createdLesson = lessonServ.save(lesson); if(createdLesson==null) return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST); return new ResponseEntity<>(ProtectionService.lessonWithoutPassword(createdLesson), HttpStatus.OK); @@ -68,8 +71,22 @@ public class LessonController { @PathVariable long id){ if(authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) return new UnauthorizedResponse<>(null); - if(!lessonServ.modifyData(id, updates, authServ.getUserFromToken(token).getRole())) + if(!lessonServ.modifyData(id, updates)){ return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } return new ResponseEntity<>(HttpStatus.OK); } + + @DeleteMapping("lesson/{id}") + public ResponseEntity deleteLesson(@RequestHeader("Authorization") String token, + @PathVariable Long id){ + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) + return new UnauthorizedResponse<>(null); + Lesson toDelete = lessonServ.findById(id); + if(toDelete == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + lessonServ.delete(toDelete); + return new ResponseEntity<>(HttpStatus.OK); + + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java index 835ddfe..5a69a82 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -107,7 +107,7 @@ public class MockController { //Schedule part - Lesson lesson_0_progra1 = new Lesson(progra1, "Mon Apr 01 2024 08:15", "Mon Apr 01 2024 10:15","rgb(0,50,100)","A0B2","Course"); + Lesson lesson_0_progra1 = new Lesson(progra1, "Mon Apr 22 2024 08:15", "Mon Apr 22 2024 10:15","rgb(0,50,100)","A0B2","Course"); Lesson lesson_0_chemistry1 = new Lesson(chemistry1, "Wed Mar 27 2024 08:15", "Wed Mar 27 2024 09:15","rgb(100,50,0)","A0B2","TP"); Lesson lesson_0_psycho1 = new Lesson(psycho1, "Sun Mar 24 2024 10:30 ","Sun Mar 24 2024 12:30 ","rgb(100,50,100)", "A0B2","TD"); Lesson lesson_1_progra1 = new Lesson(progra1, "Mon Apr 02 2024 13:30", "Mon Apr 02 2024 15:30","rgb(0,50,100)","A0B2","TP"); diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScheduleController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScheduleController.java index 6cfaa01..4c21684 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScheduleController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScheduleController.java @@ -1,5 +1,6 @@ package ovh.herisson.Clyde.EndPoints; +import ch.qos.logback.core.net.SyslogOutputStream; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -9,10 +10,7 @@ import ovh.herisson.Clyde.Services.ScheduleLessonService; import ovh.herisson.Clyde.Services.ScheduleService; import ovh.herisson.Clyde.Services.UserCurriculumService; import ovh.herisson.Clyde.Services.CurriculumService; -import ovh.herisson.Clyde.Tables.Curriculum; -import ovh.herisson.Clyde.Tables.Role; -import ovh.herisson.Clyde.Tables.Schedule; -import ovh.herisson.Clyde.Tables.ScheduleLesson; +import ovh.herisson.Clyde.Tables.*; import ovh.herisson.Clyde.Services.LessonService; import java.util.Map; @@ -94,4 +92,16 @@ public class ScheduleController { return new ResponseEntity<>(HttpStatus.OK); } + @DeleteMapping("/schedule/lesson/{id}") + public ResponseEntity deleteLessonFromSchedule(@RequestHeader("Authorization") String token, + @RequestBody Long lessonId, + @PathVariable Long id) + { + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) + return new UnauthorizedResponse<>(null); + if (!scheduleLessonServ.delete(lessonId)) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + return new ResponseEntity<>(HttpStatus.OK); + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScheduleLessonRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScheduleLessonRepository.java index 81fec17..db1b0c2 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScheduleLessonRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScheduleLessonRepository.java @@ -1,5 +1,7 @@ package ovh.herisson.Clyde.Repositories; +import jakarta.transaction.Transactional; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import ovh.herisson.Clyde.Tables.Curriculum; @@ -20,4 +22,10 @@ public interface ScheduleLessonRepository extends CrudRepository findLessonBySchedule(Schedule schedule); + + @Modifying + @Transactional + @Query("delete from ScheduleLesson sl where sl.lesson =?1") + void delete(Lesson lesson); + } 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 8637a45..e873699 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/LessonService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/LessonService.java @@ -8,6 +8,7 @@ import ovh.herisson.Clyde.Tables.Lesson; import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.User; +import java.lang.reflect.GenericSignatureFormatError; import java.util.ArrayList; import java.util.Map; @@ -38,15 +39,11 @@ public class LessonService { return toReturn; } - public boolean modifyData(long id, Map updates, Role role){ - Lesson target = lessonRepo.findById(id); + public Lesson createLesson(Map lessonInfos) { + Lesson target = new Lesson(); - if(target == null || role != Role.Secretary) - return false; - - - for (Map.Entry entry: updates.entrySet()){ - switch (entry.getKey()){ + for (Map.Entry entry : lessonInfos.entrySet()) { + switch (entry.getKey()) { case "lessonStart": target.setLessonStart((String) entry.getValue()); break; @@ -61,6 +58,40 @@ public class LessonService { case "lessonType": target.setLessonType((String) entry.getValue()); break; + case "courseId": + target.setCourse(courseRepo.findById((int) entry.getValue())); + } + + } + return target; + } + + public boolean modifyData(long id, Map updates){ + Lesson target = lessonRepo.findById(id); + System.out.println(target); + + if(target == null) + return false; + + + System.out.println("test"); + System.out.println(updates.entrySet()); + + for (Map.Entry entry: updates.entrySet()){ + System.out.println(entry); + switch (entry.getKey()){ + case "lessonStart": + target.setLessonStart((String) entry.getValue()); + break; + case "lessonEnd": + target.setLessonEnd((String) entry.getValue()); + break; + case "local": + target.setLocal((String) entry.getValue()); + break; + case "lessonType": + target.setLessonType((String) entry.getValue()); + break; } } lessonRepo.save(target); diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/ScheduleLessonService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/ScheduleLessonService.java index 05d5eb7..0fde2fe 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/ScheduleLessonService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/ScheduleLessonService.java @@ -30,6 +30,14 @@ public class ScheduleLessonService { return true; } + public boolean delete(long lessonId){ + if(lessonId == 0) + return false; + scheduleLessonRepo.delete(lessonRepo.findById(lessonId)); + return true; + } + + public Schedule getScheduleByCurriculum(Curriculum curriculum){ return scheduleLessonRepo.findScheduleByCurriculum(curriculum); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java index df0421d..af6ac77 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Course.java @@ -8,7 +8,7 @@ import org.hibernate.annotations.OnDeleteAction; public class Course { @Id @GeneratedValue(strategy = GenerationType.AUTO) - private int courseID; + private int courseId; private int credits; private String title; @@ -26,8 +26,11 @@ public class Course { public Course() {} public int getCourseID() { - return courseID; + return courseId; } + public void setCourseID(int courseId){ + this.courseId = courseId; + } public int getCredits() { return credits; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Lesson.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Lesson.java index 67465a8..af75921 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Lesson.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Lesson.java @@ -1,6 +1,8 @@ package ovh.herisson.Clyde.Tables; import jakarta.persistence.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; @Entity @@ -10,7 +12,8 @@ public class Lesson { private int lessonID; @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name= "Course") + @OnDelete(action = OnDeleteAction.SET_NULL) + @JoinColumn(name = "Course") private Course course; private String lessonStart; diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Schedule.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Schedule.java index 993c38c..c34222e 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Schedule.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Schedule.java @@ -13,6 +13,7 @@ public class Schedule { @OneToOne @JoinColumn(name = "Curriculum") + @OnDelete(action = OnDeleteAction.SET_NULL) private Curriculum curriculum; public Schedule(Curriculum curriculum){ diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScheduleLesson.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScheduleLesson.java index 3b7b6a4..895cef1 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScheduleLesson.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScheduleLesson.java @@ -14,10 +14,12 @@ public class ScheduleLesson{ @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "Schedule") + @OnDelete(action = OnDeleteAction.SET_NULL) private Schedule schedule; @ManyToOne(fetch = FetchType.EAGER) + @OnDelete(action = OnDeleteAction.SET_NULL) @JoinColumn(name = "Lesson") private Lesson lesson; diff --git a/frontend/src/Apps/ManageCourses.vue b/frontend/src/Apps/ManageCourses.vue index a9f1f66..44cd187 100644 --- a/frontend/src/Apps/ManageCourses.vue +++ b/frontend/src/Apps/ManageCourses.vue @@ -155,11 +155,11 @@
-
{{item.owner.lastName}}
- +
{{i18n("Credits")}}:{{item.credits}}
diff --git a/frontend/src/Apps/ManageSchedule.vue b/frontend/src/Apps/ManageSchedule.vue index 905e6d7..dcfa93b 100644 --- a/frontend/src/Apps/ManageSchedule.vue +++ b/frontend/src/Apps/ManageSchedule.vue @@ -1,18 +1,17 @@