backend Schedule

This commit is contained in:
Wawilski 2024-04-07 14:33:51 +02:00
parent 9937a7db39
commit aa3e1cb868
19 changed files with 662 additions and 8 deletions

View File

@ -74,8 +74,6 @@ public class CourseController {
public ResponseEntity<Map<String ,Object>> 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);

View File

@ -0,0 +1,75 @@
package ovh.herisson.Clyde.EndPoints;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Services.LessonService;
import ovh.herisson.Clyde.Services.ProtectionService;
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 AuthenticatorService authServ;
public LessonController(LessonService lessonServ, AuthenticatorService authServ) {
this.lessonServ = lessonServ;
this.authServ = authServ;
}
@GetMapping("/lesson/{id}")
public ResponseEntity<HashMap<String,Object>> 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);
}
@GetMapping("/lessons")
public ResponseEntity<Iterable<HashMap<String,Object>>> 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);
}
@GetMapping("/lessons/owned")
public ResponseEntity<Iterable<HashMap<String,Object>>> 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);
}
@PostMapping("/lesson")
public ResponseEntity<HashMap<String, Object>> postLesson(@RequestHeader("Authorization")String token,
@RequestBody Lesson lesson){
if(authServ.isNotIn(new Role[]{Role.Secretary,Role.Admin},token))
return new UnauthorizedResponse<>(null);
Lesson createdLesson = lessonServ.save(lesson);
if(createdLesson==null)
return new ResponseEntity<>(null,HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(ProtectionService.lessonWithoutPassword(createdLesson), HttpStatus.OK);
}
@PatchMapping("/lesson/{id}")
public ResponseEntity<Lesson> patchLesson(@RequestHeader("Authorization") String token,
@RequestBody Map<String, Object> updates,
@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()))
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(HttpStatus.OK);
}
}

View File

@ -24,10 +24,15 @@ public class MockController {
public final CourseService courseService;
public final InscriptionService inscriptionService;
public final LessonService lessonService;
public final ScheduleService scheduleService;
public final ScheduleLessonService scheduleLessonService;
ArrayList<User> mockUsers;
public MockController(UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CurriculumCourseService CurriculumCourseService, CurriculumService curriculumService, CourseService courseService, InscriptionService inscriptionService){
public MockController(UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService, CurriculumCourseService CurriculumCourseService, CurriculumService curriculumService, CourseService courseService, InscriptionService inscriptionService, LessonService lessonService, ScheduleService scheduleService, ScheduleLessonService scheduleLessonService){
this.tokenRepo = tokenRepo;
this.userRepo = userRepo;
this.tokenService = tokenService;
@ -35,6 +40,9 @@ public class MockController {
this.curriculumService = curriculumService;
this.courseService = courseService;
this.inscriptionService = inscriptionService;
this.lessonService = lessonService;
this.scheduleService = scheduleService;
this.scheduleLessonService = scheduleLessonService;
}
/** Saves an example of each user type by :
@ -95,7 +103,40 @@ public class MockController {
InscriptionRequest inscriptionRequest = new InscriptionRequest("helen","prenom","non","helen@gmail.com","america",new Date(),(long) 1,RequestState.Pending,"yes.png","password");
inscriptionService.save(inscriptionRequest);
//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");
Lesson lesson_0_chemistry1 = new Lesson(chemistry1, "Wed Mar 27 2024 08:15", "Wed Mar 27 2024 09:15","rgb(100,50,0)","A0B2");
Lesson lesson_0_psycho1 = new Lesson(psycho1, "Sun Mar 24 2024 10:30 ","Sun Mar 24 2024 12:30 ","rgb(100,50,100)", "A0B2");
Lesson lesson_1_progra1 = new Lesson(progra1, "Mon Apr 02 2024 13:30", "Mon Apr 02 2024 15:30","rgb(0,50,100)","A0B2");
Lesson lesson_0_commun = new Lesson(commun, "Mon Apr 01 2024 10:30", "Mon Apr 01 2024 12:30","rgb(0,50,100)","A0B2");
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));
}
}

View File

@ -0,0 +1,66 @@
package ovh.herisson.Clyde.EndPoints;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Services.ScheduleLessonService;
import ovh.herisson.Clyde.Services.ScheduleService;
import ovh.herisson.Clyde.Services.UserCurriculumService;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.Schedule;
import java.util.Map;
@RestController
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
public class ScheduleController {
private final ScheduleService scheduleServ;
private final UserCurriculumService userCurriculumService;
private final AuthenticatorService authServ;
private final ScheduleLessonService scheduleLessonServ;
public ScheduleController(ScheduleService scheduleServ, UserCurriculumService userCurriculumService, AuthenticatorService authServ, ScheduleLessonService scheduleLessonServ) {
this.scheduleServ = scheduleServ;
this.userCurriculumService = userCurriculumService;
this.authServ = authServ;
this.scheduleLessonServ = scheduleLessonServ;
}
@GetMapping("/schedule/{id}")
public ResponseEntity<Map<String,Object>> 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);
}
@GetMapping("/schedule")
public ResponseEntity<Map<String, Object>> findSelfSchedule(@RequestHeader("Authorization") String token){
if(authServ.getUserFromToken(token) == null)
return new UnauthorizedResponse<>(null);
Schedule schedule = scheduleLessonServ.getScheduleByCurriculum(userCurriculumService.findByUser(authServ.getUserFromToken(token)));
if(schedule == null)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(scheduleLessonServ.getDepthScheduleBySchedule(schedule),HttpStatus.OK);
}
@GetMapping("/schedules")
public ResponseEntity<Iterable<Map<String , Object>>> findAllSchedule(){
return new ResponseEntity<>(scheduleLessonServ.getAllSchedule(),HttpStatus.OK);
}
@PostMapping("/schedule")
public ResponseEntity<Schedule> 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);
}
}

View File

@ -0,0 +1,14 @@
package ovh.herisson.Clyde.Repositories;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import ovh.herisson.Clyde.Tables.Lesson;
import ovh.herisson.Clyde.Tables.User;
public interface LessonRepository extends CrudRepository<Lesson, Long> {
Lesson findById(long id);
@Query("select l from Lesson l where l.course.owner = ?1")
Iterable<Lesson> findAllOwnedLesson(User teacher);
}

View File

@ -0,0 +1,23 @@
package ovh.herisson.Clyde.Repositories;
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<ScheduleLesson,Long> {
@Query("select distinct sl.lesson from ScheduleLesson sl where sl.schedule.curriculum = ?1")
Iterable<Lesson> findLessonByCurriculum(Curriculum curriculum);
@Query("select distinct sl.schedule from ScheduleLesson sl")
Iterable<Schedule> findDistinctSchedule();
@Query("select distinct sl.schedule from ScheduleLesson sl where sl.schedule.curriculum = ?1")
Schedule findScheduleByCurriculum(Curriculum curriculum);
@Query("select distinct sl.lesson from ScheduleLesson sl where sl.schedule = ?1")
Iterable<Lesson> findLessonBySchedule(Schedule schedule);
}

View File

@ -0,0 +1,9 @@
package ovh.herisson.Clyde.Repositories;
import org.springframework.data.repository.CrudRepository;
import ovh.herisson.Clyde.Tables.Schedule;
public interface ScheduleRepository extends CrudRepository<Schedule,Long> {
Schedule findById(long id);
}

View File

@ -0,0 +1,59 @@
package ovh.herisson.Clyde.Services;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.LessonRepository;
import ovh.herisson.Clyde.Tables.Lesson;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User;
import java.util.Map;
@Service
public class LessonService {
private final LessonRepository lessonRepo;
public LessonService(LessonRepository lessonRepo){
this.lessonRepo = lessonRepo;
}
public Lesson save(Lesson lesson){
return lessonRepo.save(lesson);
}
public Lesson findById(long id){
return lessonRepo.findById(id);
}
public Iterable<Lesson> findAll(){return lessonRepo.findAll();}
public Iterable<Lesson> findAllOwnedLesson(User teacher){
return lessonRepo.findAllOwnedLesson(teacher);
}
public boolean modifyData(long id, Map<String ,Object> updates, Role role){
Lesson target = lessonRepo.findById(id);
if(target == null || role != Role.Secretary)
return false;
for (Map.Entry<String , Object> entry: updates.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());
}
}
lessonRepo.save(target);
return true;
}
public void delete(Lesson lesson){
lessonRepo.delete(lesson);
}
}

View File

@ -2,6 +2,7 @@ package ovh.herisson.Clyde.Services;
import ovh.herisson.Clyde.Tables.Course;
import ovh.herisson.Clyde.Tables.InscriptionRequest;
import ovh.herisson.Clyde.Tables.Lesson;
import ovh.herisson.Clyde.Tables.User;
import java.util.ArrayList;
@ -69,6 +70,31 @@ public class ProtectionService {
}
public static HashMap<String , Object> lessonWithoutPassword(Lesson lesson){
if(lesson == null)
return null;
HashMap<String, Object> 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());
return toReturn;
}
public static Iterable<HashMap<String ,Object>> lessonsWithoutPassword(Iterable<Lesson> lessons){
ArrayList<HashMap<String,Object>> toReturn = new ArrayList<>();
for (Lesson l: lessons){
toReturn.add(ProtectionService.lessonWithoutPassword(l));
}
return toReturn;
}
public static Map<String, Object> requestWithoutPassword(InscriptionRequest inscriptionRequest) {

View File

@ -0,0 +1,75 @@
package ovh.herisson.Clyde.Services;
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;
@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 void save(ScheduleLesson scheduleLesson){
scheduleLessonRepo.save(scheduleLesson);
}
public Schedule getScheduleByCurriculum(Curriculum curriculum){
return scheduleLessonRepo.findScheduleByCurriculum(curriculum);
}
public Map<String , Object> getDepthScheduleByCurriculum(Curriculum curriculum){
if(curriculum == null)
return null;
HashMap<String , Object> toReturn = new HashMap<>();
ArrayList<Map<String, Object>> lessons = new ArrayList<>();
Iterable<Lesson> foundLessons = scheduleLessonRepo.findLessonByCurriculum(curriculum);
for (Lesson l: foundLessons){
lessons.add(ProtectionService.lessonWithoutPassword(l));
}
toReturn.put("lessons",lessons);
toReturn.put("scheduleId" , scheduleLessonRepo.findScheduleByCurriculum(curriculum).getScheduleID());
return toReturn;
}
public Map<String , Object> getDepthScheduleBySchedule(Schedule schedule){
if(schedule == null)
return null;
HashMap<String , Object> toReturn = new HashMap<>();
ArrayList<Map<String, Object>> lessons = new ArrayList<>();
Iterable<Lesson> foundLessons = scheduleLessonRepo.findLessonByCurriculum(schedule.getCurriculum());
for (Lesson l: foundLessons){
lessons.add(ProtectionService.lessonWithoutPassword(l));
}
toReturn.put("lessons",lessons);
toReturn.put("scheduleId" , schedule.getScheduleID());
return toReturn;
}
public Iterable<Map<String,Object>> getAllSchedule(){
ArrayList<Map<String,Object>> toReturn = new ArrayList<>();
for (Schedule schedule: scheduleRepo.findAll()){
toReturn.add(getDepthScheduleBySchedule(schedule));
}
return toReturn;
}
}

View File

@ -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.findById(id);
}
public void delete(Schedule schedule){
scheduleRepo.delete(schedule);
}
}

View File

@ -0,0 +1,77 @@
package ovh.herisson.Clyde.Tables;
import jakarta.persistence.*;
@Entity
public class Lesson {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int lessonID;
@ManyToOne
@JoinColumn(name= "Course")
private Course course;
private String lessonStart;
private String lessonEnd;
private String color;
private String local;
public Lesson(Course course,String start, String end ,String color,String local){
this.lessonEnd = end;
this.course = course;
this.lessonStart = start;
this.color = color;
this.local = local;
}
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 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;
}
}

View File

@ -0,0 +1,33 @@
package ovh.herisson.Clyde.Tables;
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")
private Curriculum curriculum;
public Schedule(Curriculum curriculum){
this.curriculum = curriculum;
}
public Schedule() {}
public int getScheduleID(){
return scheduleID;
}
public Curriculum getCurriculum(){
return curriculum;
}
}

View File

@ -0,0 +1,53 @@
package ovh.herisson.Clyde.Tables;
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")
private Schedule schedule;
@ManyToOne(fetch = FetchType.EAGER)
@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;
}
}

View File

@ -1,7 +1,12 @@
<script setup>
import { ref } from 'vue'
import {getDifferenceTime,lastDateOfMonth,formatDate,getFirstDay,sortByDate,matrixFromList,sundayToTheEnd,getMarginTop,getHoursMinutes, monthFromList} from '../schedule.js'
import {getDifferenceTime,lastDateOfMonth,formatDate,getFirstDay,sortByDate,matrixFromList,sundayToTheEnd,getMarginTop,getHoursMinutes, monthFromList} from '../scheduleFunctions.js'
import {getAllSchedule} from "@/rest/scheduleRest.js";
const test = await getAllSchedule();
console.log(test);
const schedule = [
{
course:{

View File

@ -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} )
}

View File

@ -0,0 +1,65 @@
import {restGet,restPatch,restPost,restDelete} from "@/rest/restConsumer.js";
/**
* Create a new lesson
*/
export async function createLesson(course, start, end, color, local){
return restPost("/lesson", {course: course , start: start, end: end, color : color , local : local} )
}
/**
* 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(role){
if(role==="Teacher"){
return restGet("/lessons/owned")
}
return restGet("/lessons")
}
/**
* 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("/course/" + id, changes);
}

View File

@ -0,0 +1,13 @@
import {restGet,restPost,restPatch} 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})
}