Files
Clyde/backend/src/main/java/ovh/herisson/Clyde/Services/LessonRequestService.java

178 lines
6.5 KiB
Java
Raw Normal View History

2024-04-19 21:29:45 +02:00
package ovh.herisson.Clyde.Services;
2024-04-21 00:10:33 +02:00
/******************************************************
* @file LeessonRequestService.java
* @author William Karpinski
* @scope Extension Horaire
******************************************************/
2024-04-19 21:29:45 +02:00
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;
2024-04-19 23:59:30 +02:00
private final UserService userServ;
2024-04-19 21:29:45 +02:00
private final CourseRepository courseRepository;
public LessonRequestService(LessonChangesRequestRepository lessonChangesRepo,
UserRepository userRepo, LessonRepository lessonRepo,
2024-04-19 23:59:30 +02:00
LessonService lessonServ, ScheduleLessonRepository scheduleLessonRepo,
ScheduleRepository scheduleRepository, ScheduleLessonService scheduleLessonService,
UserService userServ, CourseRepository courseRepository) {
2024-04-19 21:29:45 +02:00
this.lessonChangesRepo = lessonChangesRepo;
this.userRepo = userRepo;
this.lessonRepo = lessonRepo;
this.lessonServ = lessonServ;
this.scheduleLessonRepo = scheduleLessonRepo;
this.scheduleRepository = scheduleRepository;
this.scheduleLessonService = scheduleLessonService;
2024-04-19 23:59:30 +02:00
this.userServ = userServ;
2024-04-19 21:29:45 +02:00
this.courseRepository = courseRepository;
}
2024-04-21 00:10:33 +02:00
/**
* Create a new lesson request
*/
2024-04-19 21:29:45 +02:00
public LessonChangesRequest save(LessonChangesRequest lessonRequest){
return lessonChangesRepo.save(lessonRequest);
}
2024-04-21 00:10:33 +02:00
/**
* Find all the requests made by a user
*/
public Iterable<LessonChangesRequest> findOwnRequests(User user){
return lessonChangesRepo.findOwnRequests(user);
}
2024-04-19 21:29:45 +02:00
public LessonChangesRequest findById(long id){
return lessonChangesRepo.findById(id);
}
2024-04-21 00:10:33 +02:00
/**
* Return all the requests
*/
2024-04-19 21:29:45 +02:00
public Iterable<LessonChangesRequest> getAll(){return lessonChangesRepo.findAll();}
2024-04-21 00:10:33 +02:00
/**
* Create a lesson if a request is accepted
*/
2024-04-19 21:29:45 +02:00
public boolean modifyCreateRequestState(LessonChangesRequest lessonRequest, RequestState state, String local ){
if(lessonRequest == null || state == lessonRequest.getState() || state == null){
return false;}
if (state == RequestState.Accepted){
2024-04-21 19:13:03 +02:00
Course course = courseRepository.findById(lessonRequest.getCourse().getCourseID());
if(courseRepository.findById(lessonRequest.getCourse().getCourseID())==null|| local == null){
2024-04-19 21:29:45 +02:00
return false;}
2024-04-19 23:59:30 +02:00
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());
2024-04-19 21:29:45 +02:00
lesson = lessonRepo.save(lesson);
scheduleLessonService.saveToAllSchedule(lesson);
}
lessonRequest.setState(state);
save(lessonRequest);
return true;
}
2024-04-20 19:30:01 +02:00
public Iterable<LessonChangesRequest> findRequestByLessonId(long id){
return lessonChangesRepo.findRequestByLessonId(id);
}
2024-04-21 00:10:33 +02:00
/**
* Refuse all the lesson request that depends on a certain lesson
* Used after the deletion of the lesson
*/
2024-04-20 19:30:01 +02:00
public void refuseAllByLessonId(long id){
Iterable<LessonChangesRequest> toRefuse = findRequestByLessonId(id);
for(LessonChangesRequest element : toRefuse)
element.setState(RequestState.Refused);
2024-04-21 00:10:33 +02:00
}
2024-04-20 19:30:01 +02:00
2024-04-21 00:10:33 +02:00
/**
* Modify a lesson if a request is accepted
*/
2024-04-19 21:29:45 +02:00
public boolean modifyChangeRequestState(Map<String, Object> updates, long lessonId,RequestState state){
if(state == RequestState.Accepted){
Lesson lesson = lessonServ.findById(lessonId);
return lessonServ.modifyData(lesson.getLessonID(),updates);
}
return true;
}
2024-04-21 00:10:33 +02:00
/**
* Delete a lesson if a request is accepted
*/
2024-04-19 23:59:30 +02:00
public void modifyDeleteRequest(LessonChangesRequest lessonChangesRequest, RequestState state){
2024-04-19 21:29:45 +02:00
if(state == RequestState.Accepted){
2024-04-19 23:59:30 +02:00
lessonServ.delete(lessonServ.findById(lessonChangesRequest.getLessonId()));
2024-04-20 19:30:01 +02:00
refuseAllByLessonId(lessonChangesRequest.getLessonId());
}
2024-04-19 21:29:45 +02:00
}
2024-04-21 00:10:33 +02:00
/**
* Construct a lesson request
*/
2024-04-19 23:59:30 +02:00
public LessonChangesRequest createLessonRequest(Map<String,Object> lessonInfos) {
LessonChangesRequest target = new LessonChangesRequest();
for (Map.Entry<String, Object> 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;
}
2024-04-19 21:29:45 +02:00
public void delete (LessonChangesRequest toDelete) {
lessonChangesRepo.delete(toDelete);
}
}