diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java index b2dcd50..74f76fa 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ApplicationsController.java @@ -47,6 +47,7 @@ public class ApplicationsController { //if unAuthed authorizedApps.add(Applications.Login); + authorizedApps.add(Applications.Schedule); User user = authServ.getUserFromToken(token); if(user == null) @@ -60,6 +61,8 @@ public class ApplicationsController { authorizedApps.add(Applications.Rdv); } + if(!authServ.isNotIn(new Role[]{Role.Teacher,Role.Admin},token)) + authorizedApps.add(Applications.ManageOwnedLessons); //if Teacher or Secretary or Admin add ManageCourses App if (!authServ.isNotIn(new Role[]{Role.Teacher,Role.Secretary,Role.Admin},token)) authorizedApps.add(Applications.ManageCourses); @@ -69,7 +72,9 @@ public class ApplicationsController { authorizedApps.add(Applications.StudentsList);} if (!authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token)){ - authorizedApps.add(Applications.UsersList);} + authorizedApps.add(Applications.UsersList); + authorizedApps.add(Applications.ManageSchedules); + authorizedApps.add(Applications.LessonRequests);} if (!authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin, Role.InscriptionService},token)){ authorizedApps.add(Applications.Payments);} 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 c021e0e..f85980c 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/CourseController.java @@ -86,11 +86,8 @@ public class CourseController { public ResponseEntity> postCourse(@RequestHeader("Authorization") String token, @RequestBody Course course) { - System.out.println(course); - System.out.println(token); if (authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token)) return new UnauthorizedResponse<>(null); - 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 new file mode 100644 index 0000000..72a05b1 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LessonController.java @@ -0,0 +1,129 @@ +package ovh.herisson.Clyde.EndPoints; + +/****************************************************** + * @file LessonController.java + * @author William Karpinski + * @scope Extension Horaire + * + * Controller of Lessons API + ******************************************************/ + +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.LessonService; +import ovh.herisson.Clyde.Services.ProtectionService; +import ovh.herisson.Clyde.Services.ScheduleLessonService; +import ovh.herisson.Clyde.Tables.Lesson; +import ovh.herisson.Clyde.Tables.Role; + +import java.util.HashMap; +import java.util.Map; + +@RestController +@CrossOrigin(originPatterns = "*", allowCredentials = "true") +public class LessonController { + private final LessonService lessonServ; + + private final ScheduleLessonService scheduleLessonServ; + private final AuthenticatorService authServ; + + public LessonController(LessonService lessonServ, ScheduleLessonService scheduleLessonService, AuthenticatorService authServ) { + this.lessonServ = lessonServ; + this.scheduleLessonServ = scheduleLessonService; + this.authServ = authServ; + } + + /** + * Return a lesson via its id + */ + @GetMapping("/lesson/{id}") + public ResponseEntity> getLesson(@PathVariable long id){ + Lesson lesson = lessonServ.findById(id); + + if(lesson == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + return new ResponseEntity<>(ProtectionService.lessonWithoutPassword(lesson),HttpStatus.OK); + } + + /** + * Return all the lessons + */ + @GetMapping("/lessons") + public ResponseEntity>> getAllLessons(@RequestHeader("Authorization") String token){ + if(authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) + return new UnauthorizedResponse<>(null); + return new ResponseEntity<>(ProtectionService.lessonsWithoutPassword(lessonServ.findAll()),HttpStatus.OK); + } + + /** + * Return all the lessons of a teacher's courses + */ + @GetMapping("/lessons/owned") + public ResponseEntity>> getOwnedLessons(@RequestHeader("Authorization") String token){ + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Teacher},token)){ + return new UnauthorizedResponse<>(null);} + return new ResponseEntity<>(ProtectionService.lessonsWithoutPassword(lessonServ.findAllOwnedLesson(authServ.getUserFromToken(token))),HttpStatus.OK); + } + + /** + ⋅* Return all the lessons of a student + */ + @GetMapping("/lessons/OwnCurriculum") + public ResponseEntity>> getOnesLessons(@RequestHeader("Authorization") String token){ + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Student},token)){ + return new UnauthorizedResponse<>(null);} + return new ResponseEntity<>(ProtectionService.lessonsWithoutPassword(lessonServ.findOnesLessons(authServ.getUserFromToken(token))),HttpStatus.OK); + } + + /** + * Post a new lesson + */ + @PostMapping("/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 lesson = lessonServ.createLesson(lessonInfos); + Lesson createdLesson = lessonServ.save(lesson); + scheduleLessonServ.saveToAllSchedule(lesson); + + if(createdLesson==null) + return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(ProtectionService.lessonWithoutPassword(createdLesson), HttpStatus.OK); + } + /** + * Modify a lesson + */ + @PatchMapping("/lesson/{id}") + public ResponseEntity patchLesson(@RequestHeader("Authorization") String token, + @RequestBody Map updates, + @PathVariable long id){ + if(authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) + return new UnauthorizedResponse<>(null); + if(!lessonServ.modifyData(id, updates)){ + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + return new ResponseEntity<>(HttpStatus.OK); + } + /** + * Delete a lesson + */ + @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/LessonRequestsController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LessonRequestsController.java new file mode 100644 index 0000000..68eeeeb --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/LessonRequestsController.java @@ -0,0 +1,150 @@ +package ovh.herisson.Clyde.EndPoints; + +/****************************************************** + * @file LessonRequestsController.java + * @author William Karpinski + * @scope Extension Horaire + * + * Controller of lesson requests API + ******************************************************/ + +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.LessonRequestService; +import ovh.herisson.Clyde.Services.LessonService; +import ovh.herisson.Clyde.Services.ProtectionService; +import ovh.herisson.Clyde.Tables.LessonChangesRequest; +import ovh.herisson.Clyde.Tables.RequestState; +import ovh.herisson.Clyde.Tables.Role; +import ovh.herisson.Clyde.Tables.User; + +import java.util.Map; + +@RestController +@CrossOrigin(originPatterns = "*", allowCredentials = "true") +public class LessonRequestsController { + private final LessonRequestService lessonRequestServ; + private final AuthenticatorService authServ; + + private final LessonService lessonServ; + public LessonRequestsController(LessonRequestService lessonRequestServer, AuthenticatorService authServ, LessonService lessonServ) { + this.lessonRequestServ = lessonRequestServer; + this.authServ = authServ; + this.lessonServ = lessonServ; + } + /** + * Return a lesson request via its id + */ + @GetMapping("/requests/lessonRequest/{id}") + public ResponseEntity> getById(@RequestHeader("Authorization") String token, @PathVariable long id){ + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) + return new UnauthorizedResponse<>(null); + + LessonChangesRequest lessonRequest= lessonRequestServ.findById(id); + + return new ResponseEntity<>(ProtectionService.lessonRequestWithoutPassword(lessonRequest), HttpStatus.OK); + } + /** + * return all the requests made by a user + */ + @GetMapping("/requests/lessonRequests/owned") + public ResponseEntity>> getOwnedRequests(@RequestHeader("Authorization") String token){ + if(authServ.isNotIn(new Role[]{Role.Admin,Role.Teacher},token)) + return new UnauthorizedResponse<>(null); + User user = authServ.getUserFromToken(token); + Iterable lessonChangesRequests = lessonRequestServ.findOwnRequests(user); + return new ResponseEntity<>(ProtectionService.lessonRequestsWithoutPassword(lessonChangesRequests),HttpStatus.OK); + + } + /** + * Return all the lesson requests + */ + @GetMapping("/requests/lessonRequests") + public ResponseEntity>> getAllRequests(@RequestHeader("Authorization") String token){ + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) + return new UnauthorizedResponse<>(null); + + Iterable lessonRequests= lessonRequestServ.getAll(); + + return new ResponseEntity<>(ProtectionService.lessonRequestsWithoutPassword(lessonRequests), HttpStatus.OK); + } + /** + * Post a lesson request + */ + @PostMapping("/requests/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); + } + /** + * Modify a lesson Request + */ + @PatchMapping("/requests/lessonRequest/{id}") + public ResponseEntity changeRequestState(@PathVariable long id, + @RequestHeader("Authorization") String token, + @RequestBody Map infos){ + if(authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token)) + return new UnauthorizedResponse<>(null); + + LessonChangesRequest lessonRequest = lessonRequestServ.findById(id); + String local = ""; + RequestState state = null; + for (Map.Entry entry : infos.entrySet()) { + switch (entry.getKey()) { + case "local": + local = (String) entry.getValue(); + break; + case "state": + state = RequestState.valueOf((String)entry.getValue()); + } + } + + if (state == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + if(lessonRequest.getRequestType() == 0 ) { + if (!lessonRequestServ.modifyCreateRequestState(lessonRequest, state, local)) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + else if(lessonRequest.getRequestType() == 1){ + infos.put("lessonStart", lessonRequest.getLessonStart()); + infos.put("lessonEnd", lessonRequest.getLessonEnd()); + infos.put("lessonType",lessonRequest.getLessonType()); + if(!lessonRequestServ.modifyChangeRequestState(infos,lessonRequest.getLessonId(),state)) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + lessonRequest.setState(state); + } + + else{ + lessonRequestServ.modifyDeleteRequest(lessonRequest, state); + lessonRequest.setState(state); + + } + lessonRequestServ.save(lessonRequest); + return new ResponseEntity<>(HttpStatus.OK); + + } + + /** + * Delete a lesson request + */ + @DeleteMapping("/requests/lessonRequest/{id}") + public ResponseEntity deleteRequest(@RequestHeader("Authorization") String token, @PathVariable long id){ + if(authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary,Role.Teacher},token)) + return new UnauthorizedResponse<>(null); + LessonChangesRequest lessonChangesRequest = lessonRequestServ.findById(id); + if (lessonChangesRequest == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + lessonRequestServ.delete(lessonChangesRequest); + 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 ee215c9..3b80212 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -30,6 +30,12 @@ public class MockController { public final CourseService courseService; public final ExternalCurriculumRepository externalCurriculumRepository; public final InscriptionService inscriptionService; + + public final LessonService lessonService; + public final ScheduleService scheduleService; + public final ScheduleLessonService scheduleLessonService; + + public final LessonRequestService lessonRequestService; ArrayList mockUsers; public final UserCurriculumRepository ucr; @@ -39,7 +45,7 @@ public class MockController { public final ScholarshipRequestRepository scholarshipRequestRepository; public final UnregisterRequestRepository uninscriptionRequestRepository; - public MockController(UserService userService, UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CurriculumCourseService CurriculumCourseService, CurriculumService curriculumService, CourseService courseService, ExternalCurriculumRepository externalCurriculumRepository, InscriptionService inscriptionService, UserCurriculumRepository ucr, MinervalRepository minervalRepository, ScholarshipRequestRepository scholarshipRequestRepository, UnregisterRequestRepository unregisterRequestRepository){ + public MockController(UserService userService, UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CurriculumCourseService CurriculumCourseService, CurriculumService curriculumService, CourseService courseService, ExternalCurriculumRepository externalCurriculumRepository, InscriptionService inscriptionService, UserCurriculumRepository ucr, MinervalRepository minervalRepository, ScholarshipRequestRepository scholarshipRequestRepository, UnregisterRequestRepository unregisterRequestRepository, LessonService lessonService, ScheduleService scheduleService, ScheduleLessonService scheduleLessonService, LessonRequestService lessonRequestService){ this.userService = userService; this.tokenRepo = tokenRepo; this.userRepo = userRepo; @@ -49,6 +55,10 @@ public class MockController { this.courseService = courseService; this.externalCurriculumRepository = externalCurriculumRepository; this.inscriptionService = inscriptionService; + this.lessonService = lessonService; + this.scheduleService = scheduleService; + this.scheduleLessonService = scheduleLessonService; + this.lessonRequestService = lessonRequestService; this.ucr = ucr; this.minervalRepository = minervalRepository; this.scholarshipRequestRepository = scholarshipRequestRepository; @@ -135,6 +145,50 @@ public class MockController { inscriptionService.save(inscriptionRequest); + + //Schedule part + + 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"); + Lesson lesson_0_commun = new Lesson(commun, "Mon Apr 01 2024 10:30", "Mon Apr 01 2024 12:30","rgb(0,50,100)","A0B2","Course"); + + LessonChangesRequest request1 = new LessonChangesRequest(joke,RequestState.Pending,null,null,null,null,2,null,1); + LessonChangesRequest request2 = new LessonChangesRequest(joke,RequestState.Pending,"Fri Apr 19 2024 10:30 ","Fri Apr 19 2024 12:30 ",null,null,1,null,2); + LessonChangesRequest request3 = new LessonChangesRequest(joke,RequestState.Pending,"Fri Apr 19 2024 13:30 ","Fri Apr 19 2024 15:30 ","Course",progra1,0,"rgb(27,49,100)",4); + + + Schedule infoBab1Schedule = new Schedule(infoBab1); + Schedule chemistryBab1Schedule = new Schedule(chemistryBab1); + Schedule psychoBab1Schedule = new Schedule(psychologyBab1); + + + scheduleService.save(infoBab1Schedule); + scheduleService.save(chemistryBab1Schedule); + scheduleService.save(psychoBab1Schedule); + + lessonService.save(lesson_0_progra1); + lessonService.save(lesson_0_chemistry1); + lessonService.save(lesson_0_commun); + lessonService.save(lesson_0_psycho1); + lessonService.save(lesson_1_progra1); + + scheduleLessonService.save(new ScheduleLesson(infoBab1Schedule,lesson_0_progra1)); + scheduleLessonService.save(new ScheduleLesson(infoBab1Schedule,lesson_1_progra1)); + scheduleLessonService.save(new ScheduleLesson(infoBab1Schedule,lesson_0_commun)); + + scheduleLessonService.save(new ScheduleLesson(chemistryBab1Schedule,lesson_0_chemistry1)); + scheduleLessonService.save(new ScheduleLesson(chemistryBab1Schedule,lesson_0_commun)); + + scheduleLessonService.save(new ScheduleLesson(psychoBab1Schedule,lesson_0_psycho1)); + scheduleLessonService.save(new ScheduleLesson(psychoBab1Schedule,lesson_0_commun)); + + + lessonRequestService.save(request1); + lessonRequestService.save(request2); + lessonRequestService.save(request3); + UnregisterRequest unregisterRequest = new UnregisterRequest(RequestState.Pending, "je veux partir", new Date(), joe.getRegNo(), joe.getFirstName(), joe.getLastName(), joe.getEmail(), null); uninscriptionRequestRepository.save(unregisterRequest); diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScheduleController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScheduleController.java new file mode 100644 index 0000000..113b513 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScheduleController.java @@ -0,0 +1,116 @@ +package ovh.herisson.Clyde.EndPoints; + +/****************************************************** + * @file ScheduleController.java + * @author William Karpinski + * @scope Extension Horaire + * + * Controller of Schedule API + ******************************************************/ + + +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.ScheduleLessonService; +import ovh.herisson.Clyde.Services.ScheduleService; +import ovh.herisson.Clyde.Services.UserCurriculumService; +import ovh.herisson.Clyde.Services.CurriculumService; +import ovh.herisson.Clyde.Tables.*; +import ovh.herisson.Clyde.Services.LessonService; + +import java.util.Map; + +@RestController +@CrossOrigin(originPatterns = "*", allowCredentials = "true") +public class ScheduleController { + + private final ScheduleService scheduleServ; + private final LessonService lessonServ; + + private final CurriculumService curriculumServ; + private final AuthenticatorService authServ; + + private final ScheduleLessonService scheduleLessonServ; + + public ScheduleController(ScheduleService scheduleServ, AuthenticatorService authServ, ScheduleLessonService scheduleLessonServ, CurriculumService curriculumServ,LessonService lessonServ) { + this.scheduleServ = scheduleServ; + this.authServ = authServ; + this.scheduleLessonServ = scheduleLessonServ; + this.curriculumServ = curriculumServ; + this.lessonServ = lessonServ; + } + /** + * Return schedule via its id + */ + @GetMapping("/schedule/{id}") + public ResponseEntity> findById(@PathVariable long id){ + Schedule schedule = scheduleServ.findById(id); + + if(schedule == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(scheduleLessonServ.getDepthScheduleBySchedule(schedule),HttpStatus.OK); + } + + /** + * Return a schedule via its curriculum id + */ + @GetMapping("/schedule/curriculum/{id}") + public ResponseEntity> findCurriculumSchedule(@PathVariable Long id){ + Schedule schedule = scheduleLessonServ.getScheduleByCurriculum(curriculumServ.findById(id)); + if(schedule == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(scheduleLessonServ.getDepthScheduleBySchedule(schedule),HttpStatus.OK); + } + /** + * Return all schedules + */ + @GetMapping("/schedules") + public ResponseEntity>> findAllSchedule(){ + return new ResponseEntity<>(scheduleLessonServ.getAllSchedule(),HttpStatus.OK); + } + /** + * Post a new schedule + */ + @PostMapping("/schedule") + public ResponseEntity postSchedule(@RequestHeader("Authorization") String token, + @RequestBody Schedule schedule){ + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token)) + return new UnauthorizedResponse<>(null); + return new ResponseEntity<>(scheduleServ.save(schedule),HttpStatus.OK); + } + /** + * Post a lesson to a schedule + */ + @PostMapping("/schedule/{id}") + public ResponseEntity postLessonToSchedule(@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.save(new ScheduleLesson( scheduleServ.findById(id), lessonServ.findById(lessonId)))) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + return new ResponseEntity<>(HttpStatus.OK); + } + /** + * Delete a lesson from a schedule + */ + @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/EndPoints/UserController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java index 20ca9ee..2835b60 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -103,9 +103,6 @@ public class UserController { @GetMapping("/teachers") public ResponseEntity>> getAllTeachers(@RequestHeader("Authorization") String token){ - if (authServ.getUserFromToken(token) == null) - return new UnauthorizedResponse<>(null); - Iterable teachers = userService.getAllTeachers(); return new ResponseEntity<>(ProtectionService.usersWithoutPasswords(teachers), HttpStatus.OK); diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/LessonChangesRequestRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/LessonChangesRequestRepository.java new file mode 100644 index 0000000..4f732d6 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/LessonChangesRequestRepository.java @@ -0,0 +1,23 @@ +package ovh.herisson.Clyde.Repositories; + +/****************************************************** + * @file LessonChangesRequestRepository.java + * @author William Karpinski + * @scope Extension Horaire + ******************************************************/ + +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import ovh.herisson.Clyde.Tables.LessonChangesRequest; +import ovh.herisson.Clyde.Tables.User; + +public interface LessonChangesRequestRepository extends CrudRepository { + LessonChangesRequest findById(long id); + + + @Query("select lr from LessonChangesRequest lr where lr.user = ?1") + Iterable findOwnRequests(User user); + + @Query("select lr from LessonChangesRequest lr where lr.lessonId = ?1") + Iterable findRequestByLessonId(long id); +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/LessonRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/LessonRepository.java new file mode 100644 index 0000000..6d80ef8 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/LessonRepository.java @@ -0,0 +1,20 @@ +package ovh.herisson.Clyde.Repositories; + +/****************************************************** + * @file LessonRepository.java + * @author William Karpinski + * @scope Extension Horaire + ******************************************************/ + +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import ovh.herisson.Clyde.Tables.Course; +import ovh.herisson.Clyde.Tables.Lesson; + +public interface LessonRepository extends CrudRepository { + + Lesson findById(long id); + + @Query("select l from Lesson l where l.course = ?1") + Iterable findLessonByCourse(Course course); +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScheduleLessonRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScheduleLessonRepository.java new file mode 100644 index 0000000..97e69ca --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScheduleLessonRepository.java @@ -0,0 +1,30 @@ +package ovh.herisson.Clyde.Repositories; + +/****************************************************** + * @file ScheduleLessonRepository.java + * @author William Karpinski + * @scope Extension Horaire + ******************************************************/ +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; +import ovh.herisson.Clyde.Tables.Lesson; +import ovh.herisson.Clyde.Tables.Schedule; +import ovh.herisson.Clyde.Tables.ScheduleLesson; + +public interface ScheduleLessonRepository extends CrudRepository { + + @Query("select distinct sl.lesson from ScheduleLesson sl where sl.schedule.curriculum = ?1") + Iterable findLessonByCurriculum(Curriculum curriculum); + + @Query("select distinct sl.schedule from ScheduleLesson sl where sl.schedule.curriculum = ?1") + Schedule findScheduleByCurriculum(Curriculum curriculum); + + @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/Repositories/ScheduleRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScheduleRepository.java new file mode 100644 index 0000000..04f3cde --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScheduleRepository.java @@ -0,0 +1,22 @@ +package ovh.herisson.Clyde.Repositories; + +/****************************************************** + * @file ScheduleRepository.java + * @author William Karpinski + * @scope Extension Horaire + ******************************************************/ + +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import ovh.herisson.Clyde.Tables.Course; +import ovh.herisson.Clyde.Tables.Lesson; +import ovh.herisson.Clyde.Tables.Schedule; + +public interface ScheduleRepository extends CrudRepository { + + Schedule getById(long id); + + + @Query("select distinct sl from Schedule sl where EXISTS (select c.curriculum from CurriculumCourse c where (sl.curriculum = c.curriculum) AND (c.course = ?1))") + Iterable findAllLessonSchedule(Course course); +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/LessonRequestService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/LessonRequestService.java new file mode 100644 index 0000000..21a6732 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/LessonRequestService.java @@ -0,0 +1,177 @@ +package ovh.herisson.Clyde.Services; + +/****************************************************** + * @file LeessonRequestService.java + * @author William Karpinski + * @scope Extension Horaire + ******************************************************/ +import org.springframework.stereotype.Service; +import ovh.herisson.Clyde.Repositories.*; +import ovh.herisson.Clyde.Tables.*; + +import java.util.Map; + +@Service +public class LessonRequestService { + private final LessonChangesRequestRepository lessonChangesRepo; + + private final UserRepository userRepo; + + private final LessonRepository lessonRepo; + + private final LessonService lessonServ; + + private final ScheduleLessonRepository scheduleLessonRepo; + + private final ScheduleRepository scheduleRepository; + + 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, + UserService userServ, CourseRepository courseRepository) { + this.lessonChangesRepo = lessonChangesRepo; + this.userRepo = userRepo; + this.lessonRepo = lessonRepo; + this.lessonServ = lessonServ; + this.scheduleLessonRepo = scheduleLessonRepo; + this.scheduleRepository = scheduleRepository; + this.scheduleLessonService = scheduleLessonService; + this.userServ = userServ; + this.courseRepository = courseRepository; + } + + /** + * Create a new lesson request + */ + public LessonChangesRequest save(LessonChangesRequest lessonRequest){ + return lessonChangesRepo.save(lessonRequest); + } + + /** + * Find all the requests made by a user + */ + public Iterable findOwnRequests(User user){ + return lessonChangesRepo.findOwnRequests(user); + } + public LessonChangesRequest findById(long id){ + return lessonChangesRepo.findById(id); + } + /** + * Return all the requests + */ + public Iterable getAll(){return lessonChangesRepo.findAll();} + + /** + * Create a lesson if a request is accepted + */ + public boolean modifyCreateRequestState(LessonChangesRequest lessonRequest, RequestState state, String local ){ + if(lessonRequest == null || state == lessonRequest.getState() || state == null){ + return false;} + if (state == RequestState.Accepted){ + Course course = courseRepository.findById(lessonRequest.getCourse().getCourseID()); + if(courseRepository.findById(lessonRequest.getCourse().getCourseID())==null|| local == null){ + return false;} + 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); + } + lessonRequest.setState(state); + save(lessonRequest); + return true; + } + + public Iterable findRequestByLessonId(long id){ + return lessonChangesRepo.findRequestByLessonId(id); + } + + /** + * Refuse all the lesson request that depends on a certain lesson + * Used after the deletion of the lesson + */ + public void refuseAllByLessonId(long id){ + Iterable toRefuse = findRequestByLessonId(id); + for(LessonChangesRequest element : toRefuse) + element.setState(RequestState.Refused); + + } + + /** + * Modify a lesson if a request is accepted + */ + public boolean modifyChangeRequestState(Map updates, long lessonId,RequestState state){ + if(state == RequestState.Accepted){ + Lesson lesson = lessonServ.findById(lessonId); + return lessonServ.modifyData(lesson.getLessonID(),updates); + } + return true; + } + + /** + * Delete a lesson if a request is accepted + */ + public void modifyDeleteRequest(LessonChangesRequest lessonChangesRequest, RequestState state){ + if(state == RequestState.Accepted){ + lessonServ.delete(lessonServ.findById(lessonChangesRequest.getLessonId())); + refuseAllByLessonId(lessonChangesRequest.getLessonId()); + } + } + + /** + * Construct a lesson request + */ + 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) { + lessonChangesRepo.delete(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 new file mode 100644 index 0000000..292a6a6 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/LessonService.java @@ -0,0 +1,150 @@ +package ovh.herisson.Clyde.Services; + + +/****************************************************** + * @file LessonService.java + * @author William Karpinski + * @scope Extension Horaire + ******************************************************/ +import org.springframework.stereotype.Service; +import ovh.herisson.Clyde.Repositories.CourseRepository; +import ovh.herisson.Clyde.Repositories.CurriculumCourseRepository; +import ovh.herisson.Clyde.Repositories.LessonRepository; +import ovh.herisson.Clyde.Repositories.UserCurriculumRepository; +import ovh.herisson.Clyde.Tables.*; + +import java.util.ArrayList; +import java.util.Map; + +@Service +public class LessonService { + private final LessonRepository lessonRepo; + private final UserCurriculumRepository userCurriculumRepo; + private final CourseRepository courseRepo; + private final CurriculumCourseRepository curriculumCourseRepo; + public LessonService(LessonRepository lessonRepo, UserCurriculumRepository userCurriculumRepo, CourseRepository courseRepo, CurriculumCourseRepository curriculumCourseRepo){ + this.lessonRepo = lessonRepo; + this.userCurriculumRepo = userCurriculumRepo; + this.courseRepo = courseRepo; + this.curriculumCourseRepo = curriculumCourseRepo; + } + + /** + * Create a lesson + */ + public Lesson save(Lesson lesson){ + return lessonRepo.save(lesson); + } + /** + * Find a lesson by its id + */ + public Lesson findById(long id){ + return lessonRepo.findById(id); + } + /** + * Return all the lessons + */ + public Iterable findAll(){return lessonRepo.findAll();} + /** + * Return all a teacher's lessons + */ + public Iterable findAllOwnedLesson(User teacher){ + ArrayList toReturn = new ArrayList<>(); + ArrayList coursesOwned = (ArrayList) courseRepo.findAllOwnedCoures(teacher); + for (Course element : coursesOwned) { + for(Lesson lesson : lessonRepo.findLessonByCourse(element)) + toReturn.add(lesson); + } + return toReturn; + } + /** + * Return all a student's lessons + */ + + public Iterable findOnesLessons(User student){ + ArrayList toReturn = new ArrayList<>(); + ArrayList courses = new ArrayList<>(); + ArrayList userCurricula = userCurriculumRepo.findByUserAndActual(student, true); + for (UserCurriculum userCurriculum : userCurricula) { + curriculumCourseRepo.findCoursesByCurriculum(userCurriculum.getCurriculum()).forEach((item) -> { + //We need this to eliminate clones because a course can belong to several curriculums + if (!courses.contains(item)) { + System.out.println(item.getTitle()); + courses.add(item); + } + }); + } + for (Course element : courses) { + for(Lesson lesson : lessonRepo.findLessonByCourse(element)) + toReturn.add(lesson); + } + + return toReturn; + } + /** + * Construct a new lesson + */ + public Lesson createLesson(Map lessonInfos) { + Lesson target = new Lesson(); + + for (Map.Entry entry : lessonInfos.entrySet()) { + switch (entry.getKey()) { + case "lessonStart": + target.setLessonStart((String) entry.getValue()); + break; + case "lessonEnd": + target.setLessonEnd((String) entry.getValue()); + case "color": + target.setColor((String) entry.getValue()); + break; + case "local": + target.setLocal((String) entry.getValue()); + break; + case "lessonType": + target.setLessonType((String) entry.getValue()); + break; + case "courseID": + target.setCourse(courseRepo.findById((int) entry.getValue())); + break; + } + + } + return target; + } + /** + * Modify a lesson + */ + public boolean modifyData(long id, Map updates){ + Lesson target = lessonRepo.findById(id); + + if(target == null) + return false; + + for (Map.Entry entry: updates.entrySet()){ + 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); + return true; + } + /** + * Delete a lesson + */ + public void delete(Lesson lesson){ + lessonRepo.delete(lesson); + } + + +} 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 ef52d5e..d08951e 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/ProtectionService.java @@ -1,8 +1,7 @@ package ovh.herisson.Clyde.Services; -import ovh.herisson.Clyde.Tables.Course; +import ovh.herisson.Clyde.Tables.*; import ovh.herisson.Clyde.Tables.Inscription.InscriptionRequest; -import ovh.herisson.Clyde.Tables.User; import java.util.ArrayList; import java.util.HashMap; @@ -70,6 +69,33 @@ public class ProtectionService { } + public static HashMap lessonWithoutPassword(Lesson lesson){ + if(lesson == null) + return null; + + HashMap toReturn = new HashMap<>(); + + toReturn.put("lessonID", lesson.getLessonID()); + toReturn.put("lessonStart", lesson.getLessonStart()); + toReturn.put("lessonEnd", lesson.getLessonEnd()); + toReturn.put("course",courseWithoutPassword(lesson.getCourse())); + toReturn.put("local",lesson.getLocal()); + toReturn.put("color", lesson.getColor()); + toReturn.put("lessonType",lesson.getLessonType()); + return toReturn; + } + + public static Iterable> lessonsWithoutPassword(Iterable lessons){ + ArrayList> toReturn = new ArrayList<>(); + + for (Lesson l: lessons){ + toReturn.add(ProtectionService.lessonWithoutPassword(l)); + } + + return toReturn; + + } + public static Map requestWithoutPassword(InscriptionRequest inscriptionRequest) { @@ -94,6 +120,32 @@ public class ProtectionService { return toReturn; } + public static Map lessonRequestWithoutPassword(LessonChangesRequest lessonRequest){ + if (lessonRequest == null) + return null; + Map toReturn = new HashMap<>(); + toReturn.put("id", lessonRequest.getId()); + toReturn.put("lessonStart", lessonRequest.getLessonStart()); + toReturn.put("lessonEnd", lessonRequest.getLessonEnd()); + toReturn.put("lessonType",lessonRequest.getLessonType()); + toReturn.put("course", courseWithoutPassword(lessonRequest.getCourse())); + toReturn.put("user", userWithoutPassword(lessonRequest.getUser())); + toReturn.put("requestType", lessonRequest.getRequestType()); + toReturn.put("state", lessonRequest.getState()); + toReturn.put("lessonId",lessonRequest.getLessonId()); + + return toReturn; + } + + public static Iterable> lessonRequestsWithoutPassword(Iterable lessonChangesRequests){ + ArrayList> toReturn = new ArrayList<>(); + + for(LessonChangesRequest lessonChangeRequest: lessonChangesRequests){ + toReturn.add(lessonRequestWithoutPassword(lessonChangeRequest)); + } + return toReturn; + + } public static Iterable> requestsWithoutPasswords(Iterable inscriptionRequests){ ArrayList> toReturn = new ArrayList<>(); diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/ScheduleLessonService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/ScheduleLessonService.java new file mode 100644 index 0000000..d89fa01 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/ScheduleLessonService.java @@ -0,0 +1,93 @@ +package ovh.herisson.Clyde.Services; + +/****************************************************** + * @file ScheduleLessonService.java + * @author William Karpinski + * @scope Extension Horaire + ******************************************************/ +import org.springframework.stereotype.Service; +import ovh.herisson.Clyde.Repositories.LessonRepository; +import ovh.herisson.Clyde.Repositories.ScheduleLessonRepository; +import ovh.herisson.Clyde.Repositories.ScheduleRepository; +import ovh.herisson.Clyde.Tables.*; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +@Service +public class ScheduleLessonService { + + private final ScheduleLessonRepository scheduleLessonRepo; + + private final LessonRepository lessonRepo; + private final ScheduleRepository scheduleRepo; + public ScheduleLessonService(ScheduleLessonRepository scheduleLessonRepo, LessonRepository lessonRepo, ScheduleRepository scheduleRepo) { + this.scheduleLessonRepo = scheduleLessonRepo; + this.lessonRepo = lessonRepo; + this.scheduleRepo = scheduleRepo; + } + public boolean save(ScheduleLesson scheduleLesson){ + if(scheduleLesson == null) + return false; + scheduleLessonRepo.save(scheduleLesson); + return true; + } + /** + * Save a lesson to all the schedule it is linked + */ + public boolean saveToAllSchedule(Lesson lesson){ + Iterable schedules = scheduleRepo.findAllLessonSchedule(lesson.getCourse()); + if(schedules == null) + return false; + for (Schedule schedule : schedules){ + save(new ScheduleLesson(schedule, lesson)); + } + return true; + } + /** + * Delete a scheduleLesson via its lesson + */ + 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); + } + /** + * Return a schedule and the list of lessons that corresponds + */ + public Map getDepthScheduleBySchedule(Schedule schedule){ + if(schedule == null) + return null; + + HashMap toReturn = new HashMap<>(); + ArrayList> lessons = new ArrayList<>(); + Iterable foundLessons = scheduleLessonRepo.findLessonByCurriculum(schedule.getCurriculum()); + + for (Lesson l: foundLessons){ + lessons.add(ProtectionService.lessonWithoutPassword(l)); + } + toReturn.put("lessons",lessons); + toReturn.put("scheduleId" , schedule.getScheduleID()); + toReturn.put("curriculum", schedule.getCurriculum()); + return toReturn; + } + /** + * Return all the schedules + */ + public Iterable> getAllSchedule(){ + ArrayList> toReturn = new ArrayList<>(); + + for (Schedule schedule: scheduleRepo.findAll()){ + toReturn.add(getDepthScheduleBySchedule(schedule)); + } + return toReturn; + } + +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/ScheduleService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/ScheduleService.java new file mode 100644 index 0000000..0d73cfc --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/ScheduleService.java @@ -0,0 +1,24 @@ +package ovh.herisson.Clyde.Services; + +import org.springframework.stereotype.Service; +import ovh.herisson.Clyde.Repositories.ScheduleRepository; +import ovh.herisson.Clyde.Tables.Schedule; + +@Service +public class ScheduleService { + private final ScheduleRepository scheduleRepo; + public ScheduleService(ScheduleRepository scheduleRepo) { + this.scheduleRepo = scheduleRepo; + } + public Schedule save(Schedule schedule){ + return scheduleRepo.save(schedule); + } + public Schedule findById(long id){ + return scheduleRepo.getById(id); + } + public void delete(Schedule schedule){ + scheduleRepo.delete(schedule); + } + +} + diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java index 77a7074..741de69 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Applications.java @@ -3,6 +3,7 @@ package ovh.herisson.Clyde.Tables; public enum Applications { // without any token Login, + Schedule, // with any token Profile, @@ -12,10 +13,17 @@ public enum Applications { Msg, Forum, Rdv, + // teachers authorization + + ManageOwnedLessons, // teachers and Secretary authorization ManageCourses, UsersList, + + //Secretary authorization + ManageSchedules, + LessonRequests, // InscriptionService authorization Requests, diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Lesson.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Lesson.java new file mode 100644 index 0000000..673e532 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Lesson.java @@ -0,0 +1,101 @@ +package ovh.herisson.Clyde.Tables; + + +/****************************************************** + * @file Lesson.java + * @author William Karpinski + * @scope Extension Horaire + * + * Represent a course in a schedule + ******************************************************/ + +import jakarta.persistence.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; + + +@Entity +public class Lesson { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int lessonID; + + @ManyToOne(fetch = FetchType.EAGER) + @OnDelete(action = OnDeleteAction.SET_NULL) + @JoinColumn(name = "Course") + private Course course; + + private String lessonStart; + + private String lessonEnd; + + private String color; + + private String lessonType; + + private String local; + + public Lesson(Course course,String start, String end ,String color,String local,String lessonType){ + this.lessonEnd = end; + this.course = course; + this.lessonStart = start; + this.color = color; + this.local = local; + this.lessonType = lessonType; + } + + public Lesson() { + } + + public int getLessonID(){ + return lessonID; + } + + public void setCourse(Course course) { + this.course = course; + } + + public Course getCourse(){ + return course; + } + + public String getLessonStart(){ + return lessonStart; + } + + public String getLessonEnd() { + return lessonEnd; + } + + public String getColor(){ + return color; + } + + public String getLocal() { + return local; + } + + public String getLessonType(){ + return lessonType; + } + + public void setLessonStart(String start){ + this.lessonStart = start; + } + + public void setLessonEnd(String lessonEnd) { + this.lessonEnd = lessonEnd; + } + + public void setColor(String color){ + this.color = color; + } + + public void setLocal(String local){ + this.local = local; + } + + public void setLessonType(String lessonType){ + this.lessonType = lessonType; + } +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/LessonChangesRequest.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/LessonChangesRequest.java new file mode 100644 index 0000000..c969548 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/LessonChangesRequest.java @@ -0,0 +1,148 @@ +package ovh.herisson.Clyde.Tables; + + +/****************************************************** + * @file LessonChangesRequest.java + * @author William Karpinski + * @scope Extension Horaire + * + * Represent a request about changes on schedules and lessons + ******************************************************/ + +import jakarta.persistence.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; + +@Entity +public class LessonChangesRequest { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + @ManyToOne + @JoinColumn(name = "Users") + @OnDelete(action = OnDeleteAction.CASCADE) + private User user; + + private long lessonId; + + private RequestState state; + + private String lessonStart; + + private String lessonEnd; + + private String color; + @ManyToOne + @JoinColumn(name ="Course") + private Course course; + + private String lessonType; + + /** + * Can take 3 values: + * 0 : Request to CREATE a lesson + * 1 : Request to MODIFY an existing lesson + * 2 : Request to DELETE a lesson + */ + private int requestType; + + public LessonChangesRequest(User user, RequestState state, String lessonStart, + String lessonEnd, String lessonType, Course course, + int requestType, String color,long lessonId){ + this.user = user; + this.state = state; + this.requestType = requestType; + this.lessonType = lessonType; + this.lessonStart = lessonStart; + this.lessonEnd= lessonEnd; + this.color = color; + this.course = course; + this.lessonId = lessonId; + } + + public LessonChangesRequest() { + + } + + public int getId() { + return id; + } + + + public RequestState getState() { + return state; + } + + public User getUser() { + return user; + } + + public long getLessonId(){ + return lessonId; + } + + public String getLessonStart() { + return lessonStart; + } + + public String getLessonEnd() { + return lessonEnd; + } + + + + public int getRequestType() { + return requestType; + } + + public String getLessonType() { + return lessonType; + } + + public String getColor() { + return color; + } + + public Course getCourse() { + return course; + } + + public void setState(RequestState state) { + this.state = state; + } + + public void setUser(User user) { + this.user = user; + } + + public void setLessonStart(String lessonStart) { + this.lessonStart = lessonStart; + } + + public void setLessonType(String lessonType) { + this.lessonType = lessonType; + } + + public void setLessonEnd(String lessonEnd) { + this.lessonEnd = lessonEnd; + } + + public void setColor(String color) { + this.color = color; + } + + public void setRequestType(int requestType) { + this.requestType = requestType; + } + + + public void setLessonId(long lessonId) { + this.lessonId = lessonId; + } + + public void setCourse(Course course){ + this.course = course; + } +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Schedule.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Schedule.java new file mode 100644 index 0000000..e2f1982 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Schedule.java @@ -0,0 +1,42 @@ + +package ovh.herisson.Clyde.Tables; + + +/****************************************************** + * @file Schedule.java + * @author William Karpinski + * @scope Extension Horaire + * + * Represent a schedule linked to a curriculum + ******************************************************/ +import jakarta.persistence.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; + +@Entity +public class Schedule { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int scheduleID; + + @OneToOne + @JoinColumn(name = "Curriculum") + @OnDelete(action = OnDeleteAction.SET_NULL) + private Curriculum curriculum; + + public Schedule(Curriculum curriculum){ + this.curriculum = curriculum; + } + + public Schedule() {} + + + public int getScheduleID(){ + return scheduleID; +} + + public Curriculum getCurriculum(){ + return 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 new file mode 100644 index 0000000..9dca028 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScheduleLesson.java @@ -0,0 +1,63 @@ +package ovh.herisson.Clyde.Tables; + + +/****************************************************** + * @file ScheduleLesson.java + * @author William Karpinski + * @scope Extension Horaire + * + * Used to link schedules and lessons to each others + ******************************************************/ + +import jakarta.persistence.*; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; + + +@Entity +public class ScheduleLesson{ + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + @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; + + public ScheduleLesson(Schedule schedule,Lesson lesson){ + this.schedule = schedule; + this.lesson = lesson; + } + + public ScheduleLesson() { + } + + public int getID(){ + return id; + } + + public Lesson getLesson(){ + return lesson; + } + + public Schedule getSchedule(){ + return schedule; + } + + public void setLesson(Lesson lesson){ + this.lesson = lesson; + } + + public void setSchedule(Schedule schedule){ + this.schedule = schedule; + } + + +} diff --git a/frontend/public/i18n/EN.txt b/frontend/public/i18n/EN.txt index 7fa49b0..6598207 100644 --- a/frontend/public/i18n/EN.txt +++ b/frontend/public/i18n/EN.txt @@ -29,16 +29,76 @@ app.settings=Settings app.messages=Messages app.forum=Forum app.schedules=Schedules +app.manageSchedules=Manage Schedules app.inscription.requests=Inscription Requests app.manage.courses=Manage Courses app.language=Language app.manage.profile=Manage profile app.studentList=Students List app.users=Users +app.manageOwnLessons=Manage Owned Courses Schedule +app.lessonRequests=Schedule Requests app.payments=Payments request.moreInfos=More Infos request.accept=Accept request.refuse=Refuse +Pending=Pending +Delete=Delete +Modify=Modify +Create=Créer +requestType=Request Type +day=Day +start=Start +end=End +monday=Monday +tuesday=Tuesday +wednesday=Wednesday +thursday=Thursday +friday=Friday +saturday=Saturday +sunday=Sunday +january=January +february=February +march=March +april=April +may=May +june=June +july=July +august=August +september=September +october=October +november=November +december=December +Grid=Grid +Week=Week +Month=Month +List=List +Type=Type +Teacher=Teacher +Course=Course +TP=TP +TD=TD +Exam=Exam +OwnSchedule=Own Schedule +SwitchToJSON=Switch to JSON FILE +schedule.previous=Previous +schedule.next=Next +schedule.current=Current +schedule.settings=Settings +schedule.courses=Courses +schedule.teachers=Teacher(s) +schedule.askChanges=Ask Changes +schedule.askCreate=Ask to create course +schedule.askDeletion=Ask Deletion +schedule.createSchedule=Create Schedule +schedule.createLesson=Create course +schedule.deleteMod=Unable deletion +schedule.noDeleteMod=Disable deletion +schedule=Schedule +old_day=Precedent day +old_start=Precedent start +old_end=Precedent end +old_type=Precedent type courses.createCourse=Create course courses.deleteCourse=Delete course courses.modify=Modify @@ -146,4 +206,4 @@ newcurr=New curriculum cursusprereq=The cursus you selected has some prerequisites ensure that your external curriculum data is updated in your profile imposecurriculum=Impose a curriculum impose=Impose -gotimposed=The selected curriculum has been imposed \ No newline at end of file +gotimposed=The selected curriculum has been imposed diff --git a/frontend/public/i18n/FR.txt b/frontend/public/i18n/FR.txt index 0557a83..a09c338 100644 --- a/frontend/public/i18n/FR.txt +++ b/frontend/public/i18n/FR.txt @@ -29,16 +29,76 @@ app.settings=Options app.messages=Messages app.forum=Forum app.schedules=Horaires +app.manageSchedules=Gérer les horaires app.inscription.requests=Demandes d'Inscription app.manage.courses=Gérer les cours app.language=Langue app.manage.profile=Gérer le profil app.studentList=Liste des étudiants app.users=Utilisateurs +app.manageOwnLessons=Gérer ses horaires de cours +app.lessonRequests=Requêtes d'horaire app.payments=Payements request.moreInfos=Plus d'Infos request.accept=Accepter request.refuse=Refuser +Pending=En attente +Delete=Supprimer +Modify=Modifier +Create=Créer +requestType=Type de Requête +day=Jour +start=Début +end=Fin +monday=Lundi +tuesday=Mardi +wednesday=Mercredi +thursday=Jeudi +friday=Vendredi +saturday=Samedi +sunday=Dimanche +january=Janvier +february=Février +march=Mars +april=Avril +may=Mai +june=Juin +july=Juillet +august=Août +september=Septembre +october=Octobre +november=Novembre +december=Decembre +Grid=Grille +Week=Semaine +Month=Mois +List=Liste +Type=Type +Teacher=Professeur +Course=Cours +TP=TP +TD=TD +Exam=Exam +OwnSchedule=Mon Horaire +SwitchToJSON=Afficher le JSON +schedule.previous=Précédent +schedule.next=Prochain +schedule.current=Actuel +schedule.settings=Options +schedule.courses=Cours +schedule.teachers=Professeurs(s) +schedule.askChanges=Demander Changement +schedule.askCreate=Demander de créer un cours +schedule.askDeletion=Demander suppression +schedule.createSchedule=Créer Horaire +schedule.createLesson=Créer cours +schedule.deleteMod=Activer suppression +schedule.noDeleteMod=Désactiver suppression +schedule=Horaire +old_day=Ancien Jour +old_start=Ancien Début +old_end=Ancienne Fin +old_type=Ancien Type courses.createCourse=Créer un cours courses.deleteCourse=Supprimer un cours courses.modify=Modifier @@ -146,4 +206,4 @@ newcurr=Nouveau cursus cursusprereq=Le cursus que vous avez selectionné a des prérequis assurez vous que votre dossier de parcours est a jour dans votre profil imposecurriculum=Imposer un cursusgotimposed impose=Imposer -gotimposed=Le cursus selectionné a été imposé \ No newline at end of file +gotimposed=Le cursus selectionné a été imposé diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 0ab750a..daf1064 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -231,6 +231,7 @@ window.addEventListener('hashchange', () => { .text { right: 0%; width: 0%; + visibility: collapse; opacity: 0; color: white; font-size: 1.2em; @@ -239,7 +240,8 @@ window.addEventListener('hashchange', () => { } ul.vertical:hover .text { - opacity: 1; + opacity:1; + visibility:visible; width: 60%; transition-duration: .3s; padding-left: 15px; diff --git a/frontend/src/Apps/LessonRequests.vue b/frontend/src/Apps/LessonRequests.vue new file mode 100644 index 0000000..d84696f --- /dev/null +++ b/frontend/src/Apps/LessonRequests.vue @@ -0,0 +1,218 @@ + + + + + + + + + + + diff --git a/frontend/src/Apps/ManageCourses.vue b/frontend/src/Apps/ManageCourses.vue index 96bd35b..387dfba 100644 --- a/frontend/src/Apps/ManageCourses.vue +++ b/frontend/src/Apps/ManageCourses.vue @@ -35,7 +35,6 @@ let isnull= false; for(const [key, value] of Object.entries(toAdd)){ - console.log(toAdd.owner); if(value === null){ isnull=true; } @@ -64,17 +63,14 @@ async function patchCourse(course){ for (let element in toModify){ - console.log(toModify,1) - console.log(toModify[element],2) - if (element =="owner" && (toModify[element].regNo != course.owner.regNo)){ - await alterCourse(course.courseId,{owner:toModify[element].regNo}); + await alterCourse(course.courseID,{owner:toModify[element].regNo}); } else if(element == "title" && (toModify[element] != course.title)){ - await alterCourse(course.courseId,{title:toModify[element]}); + await alterCourse(course.courseID,{title:toModify[element]}); } else if(element == "credits" && (parseInt(toModify[element]) != course.credits)){ - await alterCourse(course.courseId,{credits:parseInt(toModify[element])}); + await alterCourse(course.courseID,{credits:parseInt(toModify[element])}); } } toModify= Object.assign({},pattern); @@ -159,11 +155,11 @@
-
{{item.owner.lastName}}
- +
{{i18n("Credits")}}:{{item.credits}}
@@ -177,17 +173,6 @@ margin-top:3.5%; } -.infosContainer { - min-width:350px; - padding-bottom:50px; - border:2px solid black; - font-size:25px; - color:white; - padding:20px; - background-color:rgb(50,50,50); - border-radius:20px; -} - .containerElement{ justify-content:center; display:grid; diff --git a/frontend/src/Apps/ManageOwnLessons.vue b/frontend/src/Apps/ManageOwnLessons.vue new file mode 100644 index 0000000..ba762ef --- /dev/null +++ b/frontend/src/Apps/ManageOwnLessons.vue @@ -0,0 +1,277 @@ + + + + + + + diff --git a/frontend/src/Apps/ManageSchedule.vue b/frontend/src/Apps/ManageSchedule.vue new file mode 100644 index 0000000..b34e4a5 --- /dev/null +++ b/frontend/src/Apps/ManageSchedule.vue @@ -0,0 +1,506 @@ + + + + + + diff --git a/frontend/src/Apps/Schedule.vue b/frontend/src/Apps/Schedule.vue new file mode 100644 index 0000000..1ee30fd --- /dev/null +++ b/frontend/src/Apps/Schedule.vue @@ -0,0 +1,802 @@ + + + + + + diff --git a/frontend/src/rest/LessonRequests.js b/frontend/src/rest/LessonRequests.js new file mode 100644 index 0000000..8586710 --- /dev/null +++ b/frontend/src/rest/LessonRequests.js @@ -0,0 +1,23 @@ +import { restGet, restPost, restDelete, restPatch } from './restConsumer.js' + +export async function getLessonRequest(id){ + return restGet('/requests/lessonRequest/' + id); +} + +export async function getAllRequests(){ + return restGet("/requests/lessonRequests"); +} +export async function getOwnedRequests(){ + return restGet("/requests/lessonRequests/owned"); +} +export async function createRequest(request){ + return restPost("/requests/lessonRequest", request); +} + +export async function changeRequestState(id, infos){ + return restPatch("/requests/lessonRequest/" + id, infos); +} + +export async function deleteRequest(id){ + return restDelete("/requests/lessonRequest/"+id); +} diff --git a/frontend/src/rest/apps.js b/frontend/src/rest/apps.js index a7b33a3..784685c 100644 --- a/frontend/src/rest/apps.js +++ b/frontend/src/rest/apps.js @@ -8,18 +8,26 @@ import Profil from "@/Apps/Profil.vue" import Courses from "@/Apps/ManageCourses.vue" import Users from "@/Apps/UsersList.vue" import Students from "@/Apps/StudentsList.vue" +import Schedule from "@/Apps/Schedule.vue" +import ManageSchedule from "@/Apps/ManageSchedule.vue" +import ManageOwnedLessons from "@/Apps/ManageOwnLessons.vue"; +import LessonRequests from "@/Apps/LessonRequests.vue"; import Msg from "@/Apps/Msg.vue" import Forums from '@/Apps/Forums.vue' import Payments from "@/Apps/Inscription/PaymentInfo.vue"; import ManageRequests from "@/Apps/Inscription/ManageRequests.vue"; const apps = { + '/schedule': Schedule, + '/manage-schedule': ManageSchedule, '/login': LoginPage, '/requests': ManageRequests, '/profil': Profil, '/manage-courses' : Courses, '/users-list' : Users, '/students-list' : Students, + '/manage-owned-lessons': ManageOwnedLessons, + '/manage-schedule-requests' : LessonRequests, '/msg' : Msg, '/forums': Forums, '/payments': Payments @@ -30,10 +38,13 @@ const appsList = { 'Notification': { path: '#/notifs', icon: 'fa-bell', text: i18n("app.notifications") }, 'Forum': { path: '#/forums', icon: 'fa-envelope', text: i18n("app.forum") }, 'Schedule': { path: '#/schedule', icon: 'fa-calendar-days', text: i18n("app.schedules") }, - 'Requests': { path: '#/requests', icon: 'fa-users', text: "Requests" }, + 'ManageSchedules': { path: '#/manage-schedule', icon: 'fa-calendar-days', text: i18n("app.manageSchedules")}, 'ManageCourses': { path: '#/manage-courses', icon: 'fa-book', text: i18n("app.manage.courses") }, 'StudentsList':{ path: '#/students-list',icon: 'fa-users',text: i18n("app.studentList")}, 'UsersList':{ path: '#/users-list',icon: 'fa-users',text: i18n("app.users")}, + 'ManageOwnedLessons':{path: '#/manage-owned-lessons',icon:'fa-calendar-days',text: i18n("app.manageOwnLessons")}, + 'LessonRequests':{path: '#/manage-schedule-requests', icon:'fa-book', text: i18n("app.lessonRequests")}, + 'Requests': { path: '#/requests', icon: 'fa-users', text: "Requests" }, 'Payments':{path: '#/payments', icon:'fa-users', text:i18n("app.payments")} } diff --git a/frontend/src/rest/courses.js b/frontend/src/rest/courses.js index 92677e9..3a87f5c 100644 --- a/frontend/src/rest/courses.js +++ b/frontend/src/rest/courses.js @@ -8,8 +8,6 @@ import { restGet, restPost, restDelete, restPatch } from './restConsumer.js' * Create a new course */ export async function createCourse(name, credits, owner){ - console.log(owner); - return restPost("/course", {title: name, credits: credits, owner} ) } diff --git a/frontend/src/rest/lessonSchedule.js b/frontend/src/rest/lessonSchedule.js new file mode 100644 index 0000000..bb80c81 --- /dev/null +++ b/frontend/src/rest/lessonSchedule.js @@ -0,0 +1,69 @@ +import {restGet,restPatch,restPost,restDelete} from "@/rest/restConsumer.js"; + +/** + * Create a new lesson + */ +export async function createLesson(datas){ + return restPost("/lesson", datas ) +} + +/** + * Delete a lesson + */ +export async function deleteLesson(id){ + return restDelete("/lesson/" + id); +} + +/** + * Get information on a particular course + * + * @return all attribute of the lesson + * - course + * - start + * - end + * - color + * - local + */ +export async function getLesson(id){ + return restGet("/lesson/" + id); +} + +/** + * Get the list of courses to display on secretary's option + * + * @return list of courses of the form + * - id + * - name + * - credits + * - facutly + * - teacher + * - Assistants + */ +export async function getLessons(){ + return restGet("/lessons") +} + +export async function getOwnedLessons(){ + return restGet("/lessons/owned") +} + +export async function getOnesLessons(){ + return restGet("/lessons/OwnCurriculum"); +} + +/** + * Change the options of a course + * + * @param id the id of the course + * @param changes Object with value to changes + * + * The changes object can contain: + * - name + * - credits + * - faculty + * - teacher + * - assistants: should be a list and will replace all assistants + */ +export async function alterLesson(id, changes){ + return restPatch("/lesson/" + id, changes); +} diff --git a/frontend/src/rest/restConsumer.js b/frontend/src/rest/restConsumer.js index 7fce929..a103170 100644 --- a/frontend/src/rest/restConsumer.js +++ b/frontend/src/rest/restConsumer.js @@ -20,6 +20,9 @@ export function restDelete(endPoint) { return _rest(endPoint, {method: "DELETE"}); } +export function restDeleteItem(endPoint, data){ + return _rest(endPoint, {method: "DELETE", credentials: 'include', body: JSON.stringify(data)}); +} export function restPatch(endPoint, data) { return _rest(endPoint, {method: "PATCH", credentials: 'include', body: JSON.stringify(data)}); } diff --git a/frontend/src/rest/scheduleRest.js b/frontend/src/rest/scheduleRest.js new file mode 100644 index 0000000..342d717 --- /dev/null +++ b/frontend/src/rest/scheduleRest.js @@ -0,0 +1,30 @@ +import {restGet, restPost, restPatch, restDelete, restDeleteItem} from "@/rest/restConsumer.js"; + +export async function getAllSchedule(){ + return restGet('/schedules'); +} + +export async function getOwnSchedule(){ + return restGet('/schedule') +} + +export async function createSchedule(curriculum) { + return restPost('/schedule',{curriculum : curriculum}) +} + +export async function getCurriculumSchedule(id){ + return restGet('/schedule/curriculum/' + id) +} + +export async function addLessonToSchedule(id,lessonId){ + return restPost('/schedule/' + id, lessonId) +} + +export async function getSchedule(id){ + return restGet('/schedule/' + id); + +} + +export async function deleteLessonFromSchedule(id,lessonId){ + return restDeleteItem('/schedule/lesson/'+ id, lessonId) +} diff --git a/frontend/src/scheduleFunctions.js b/frontend/src/scheduleFunctions.js new file mode 100644 index 0000000..d3db9aa --- /dev/null +++ b/frontend/src/scheduleFunctions.js @@ -0,0 +1,169 @@ + + +/* + * Get a date object in a date format dd-mm-yyyy + */ + export function formatDate(date) { + var d = new Date(date), + month = '' + (d.getMonth() + 1), + day = '' + d.getDate(), + year = d.getFullYear(); + + if (month.length < 2) + month = '0' + month; + if (day.length < 2) + day = '0' + day; + + return [day, month, year].join('-'); + } + +/* + * Get a date object in a date format yyyy-mm-dd + */ + export function invertedFormatDate(date) { + let d = new Date(date), + month = '' + (d.getMonth() + 1), + day = '' + d.getDate(), + year = d.getFullYear(); + + if (month.length < 2) + month = '0' + month; + if (day.length < 2) + day = '0' + day; + + return [year, month, day].join('-'); + } + + /* + * Create a string date via the hour and the date + */ + export function createLessonEvent(date,hour){ + const str = date.concat(' ',hour); + return new Date(str); + } +/* + * Get the duration of a lesson + */ + export function durationCourse(element){ + const hour = element.lessonEnd.substring(3,5) -element.lessonStart.substring(3,5); + + + return (element.lessonEnd - element.lessonStart)%2; + } + +/* + * Help to sort lessons chronologically + */ + export function sortByDate(a, b) { + const nameA = new Date(a.lessonStart); // ignore upper and lowercase + const nameB = new Date(b.lessonStart); // ignore upper and lowercase + + if (nameA < nameB) { + return -1; + } + if (nameA > nameB) { + return 1; + } + return 0; + } + +/* +* Get the first day of the current month +*/ + export function getFirstDay(d){ + var date = new Date(d); + return new Date(date.getFullYear(), date.getMonth(), 1); + } + +/* + * Convert a list of lesson to a schedule in a week + */ + export function weekFromList(list, weekMonday){ + const weekStart = new Date(weekMonday); + const matrix = new Array(7); + for (let i = 0; i < matrix.length; i++) { + matrix[i] = []; + } + for(let key in list){ + const temp = list[key]; + const day = new Date(list[key].lessonStart); + if((((day.getTime()-weekStart.getTime())/60000)<10080) && (((day.getTime()-weekStart.getTime())/60000)>0)){ + matrix[day.getDay()].push(temp); + matrix[day.getDay()].sort((a,b) => sortByDate(a,b)); + } + } + return matrix; + } + + /* + * Return the last date of a month + */ + export function lastDateOfMonth(d){ + const date = new Date(d); + const temp = new Date(date.getFullYear(), date.getMonth() + 1, 0); + return temp.getDate(); + } +/* + * Convert a list of lesson to a schedule in a month + */ + + export function monthFromList(list,month){ + const beginning = getFirstDay(month); + const matrix = new Array(lastDateOfMonth(month)) + for (let i = 0; i < matrix.length; i++) { + matrix[i] = []; + } + for(let key in list){ + const temp = list[key]; + const day = new Date(list[key].lessonStart); + if(day.getMonth()==month){ + matrix[day.getDate()].push(temp); + matrix[day.getDay()].sort((a,b) => sortByDate(a,b)); + }} + + + return matrix; + } + + +/* + * Put the first element of a weekly schedule (corresponds to sunday) to the end of the list + */ + export function sundayToTheEnd(list){ + const newlist = list; + const sunday = newlist.shift(); + newlist.push(sunday); + return newlist; + } + + /* + * Get the difference of time between 2 dates + */ + export function getDifferenceTime(date1,date2){ + return Math.abs((new Date(date2).getTime() - new Date(date1).getTime())/60000); + } + + /* + * used to shift the lessons correctly + */ + export function getMarginTop(list, index1, index2){ + if(index2 < 0){ + const temp = new Date(list[index1].lessonStart); + temp.setHours(8,0,0); + return Math.abs((new Date(list[index1].lessonStart).getTime()- temp.getTime())/60000); + } + if(new Date(list[index1].lessonStart).getTime() === new Date(list[index2].lessonEnd).getTime()){ + return Math.abs(getMarginTop(list,index2,index2-1)); + } + return Math.abs((new Date(list[index1].lessonStart).getTime()- new Date(list[index2].lessonEnd).getTime())/60000)+getMarginTop(list,index2,index2-1); + } + + /* + * Get the hour and minutes of a date in the right format + */ + export function getHoursMinutes(date){ + const d = new Date(date); + let hours = [d.getHours().toString().length == 1 ? "0" + d.getHours().toString() : d.getHours()]; + return hours+ ":" + d.getMinutes(); + } +