[fdfbdde] | 1 | package com.example.task.service;
|
---|
| 2 |
|
---|
| 3 | import com.example.task.entity.PlaceEntity;
|
---|
| 4 | import com.example.task.entity.StudentEntity;
|
---|
| 5 | import com.example.task.entity.event.*;
|
---|
| 6 | import com.example.task.entity.projection.ExamEvent;
|
---|
| 7 | import com.example.task.entity.projection.NonRepeatingClassEvent;
|
---|
| 8 | import com.example.task.entity.projection.RepeatingClassEvent;
|
---|
| 9 | import com.example.task.entity.schoolyear.SchoolYearEntity;
|
---|
| 10 | import com.example.task.entity.schoolyear.SchoolYearId;
|
---|
| 11 | import com.example.task.entity.subject.SubjectEntity;
|
---|
| 12 | import com.example.task.entity.subject.SubjectId;
|
---|
| 13 | import com.example.task.entity.term.TermEntity;
|
---|
| 14 | import com.example.task.entity.term.TermId;
|
---|
| 15 | import com.example.task.repository.*;
|
---|
| 16 | import com.example.task.repository.event.*;
|
---|
| 17 | import lombok.AllArgsConstructor;
|
---|
| 18 | import org.springframework.stereotype.Service;
|
---|
| 19 |
|
---|
| 20 | import java.sql.Time;
|
---|
| 21 | import java.time.LocalDate;
|
---|
| 22 | import java.time.LocalTime;
|
---|
| 23 | import java.time.format.DateTimeFormatter;
|
---|
| 24 | import java.util.List;
|
---|
| 25 |
|
---|
| 26 | @Service
|
---|
| 27 | @AllArgsConstructor
|
---|
| 28 | public class SubjectService {
|
---|
| 29 |
|
---|
| 30 | private final PlaceRepository placeRepository;
|
---|
| 31 | private final SubjectRepository subjectRepository;
|
---|
| 32 | private final CalendarEventRepository calendarEventRepository;
|
---|
| 33 | private final HappensAtPlaceRepository happensAtPlaceRepository;
|
---|
| 34 | private final NonRepeatingEventRepository nonRepeatingEventRepository;
|
---|
| 35 | private final SubjectEventRepository subjectEventRepository;
|
---|
| 36 | private final StudentRepository studentRepository;
|
---|
| 37 | private final SchoolYearRepository schoolYearRepository;
|
---|
| 38 | private final TermRepository termRepository;
|
---|
| 39 | private final RepeatingEventRepository repeatingEventRepository;
|
---|
| 40 | private final UserAuthenticationService userAuthenticationService;
|
---|
| 41 |
|
---|
| 42 | public List<PlaceEntity> findAllPlaces() {
|
---|
| 43 | return placeRepository.findAll();
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public List<SubjectEntity> findAllSubjects() {
|
---|
| 47 | List<SubjectEntity> arr = subjectRepository.findAll();
|
---|
| 48 | arr.removeIf(s -> s.getId().getTermEntity().getTermId().getSchoolYear().getSchoolYearId().getStudentId().getStudentId() != userAuthenticationService.getLoggedInUser().getStudentId());
|
---|
| 49 | return arr;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public void addExam(String subjectId, Integer placeId, String examName, String examDescription, LocalDate date, String startTime, String endTime) throws Exception {
|
---|
| 53 |
|
---|
| 54 | PlaceEntity place = placeRepository.findById(placeId).orElseThrow(Exception::new);
|
---|
| 55 | SubjectEntity subject = returnSubject(subjectId);
|
---|
| 56 |
|
---|
| 57 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
|
---|
| 58 | LocalTime time1 = LocalTime.parse(startTime, formatter);
|
---|
| 59 | LocalTime time2 = LocalTime.parse(endTime, formatter);
|
---|
| 60 |
|
---|
| 61 | CalendarEvent calendarEvent = new CalendarEvent(examName, "Exam", examDescription, Time.valueOf(time1), Time.valueOf(time2));
|
---|
| 62 | calendarEventRepository.save(calendarEvent);
|
---|
| 63 | NonRepeatingEvent nonRepeatingEvent = new NonRepeatingEvent(new NonRepeatingEventId(calendarEvent), date);
|
---|
| 64 | nonRepeatingEventRepository.save(nonRepeatingEvent);
|
---|
| 65 | SubjectEvent subjectEvent = new SubjectEvent(new SubjectEventId(calendarEvent), subject);
|
---|
| 66 | subjectEventRepository.save(subjectEvent);
|
---|
| 67 | HappensAtPlace happensAtPlace = new HappensAtPlace(new NonRepeatingEventId(calendarEvent), place);
|
---|
| 68 | happensAtPlaceRepository.save(happensAtPlace);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public void addClass(String subjectId, Integer placeId, String examName, String examDescription, LocalDate startingDate, LocalDate endingDate, String day, String startTime, String endTime) throws Exception {
|
---|
| 72 |
|
---|
| 73 | PlaceEntity place = placeRepository.findById(placeId).orElseThrow(Exception::new);
|
---|
| 74 | SubjectEntity subject = returnSubject(subjectId);
|
---|
| 75 |
|
---|
| 76 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
|
---|
| 77 | LocalTime time1 = LocalTime.parse(startTime, formatter);
|
---|
| 78 | LocalTime time2 = LocalTime.parse(endTime, formatter);
|
---|
| 79 |
|
---|
| 80 | CalendarEvent calendarEvent = new CalendarEvent(examName, "Class", examDescription, Time.valueOf(time1), Time.valueOf(time2));
|
---|
| 81 | calendarEventRepository.save(calendarEvent);
|
---|
| 82 |
|
---|
| 83 | if (endingDate == null) {
|
---|
| 84 | NonRepeatingEvent nonRepeatingEvent = new NonRepeatingEvent(new NonRepeatingEventId(calendarEvent), startingDate);
|
---|
| 85 | nonRepeatingEventRepository.save(nonRepeatingEvent);
|
---|
| 86 |
|
---|
| 87 | } else {
|
---|
| 88 | RepeatingEvent repeatingEvent = new RepeatingEvent(new NonRepeatingEventId(calendarEvent), day, 1, startingDate, endingDate);
|
---|
| 89 | repeatingEventRepository.save(repeatingEvent);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | SubjectEvent subjectEvent = new SubjectEvent(new SubjectEventId(calendarEvent), subject);
|
---|
| 93 | subjectEventRepository.save(subjectEvent);
|
---|
| 94 | HappensAtPlace happensAtPlace = new HappensAtPlace(new NonRepeatingEventId(calendarEvent), place);
|
---|
| 95 | happensAtPlaceRepository.save(happensAtPlace);
|
---|
| 96 |
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public List<ExamEvent> findAllUserExams() {
|
---|
| 100 | return subjectRepository.exams(userAuthenticationService.getLoggedInUser().getStudentId());
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public List<NonRepeatingClassEvent> findAllUserNonRepeatingClasses() {
|
---|
| 104 | return subjectRepository.nonRepeatingClasses(userAuthenticationService.getLoggedInUser().getStudentId());
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | public List<RepeatingClassEvent> findAllUserRepeatingClasses() {
|
---|
| 108 | return subjectRepository.repeatingClasses(userAuthenticationService.getLoggedInUser().getStudentId());
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | private SubjectEntity returnSubject(String subjectId) throws Exception {
|
---|
| 112 | Integer subject_id = Integer.parseInt(subjectId.split("\\s+")[0]);
|
---|
| 113 | Integer student_id = Integer.parseInt(subjectId.split("\\s+")[1]);
|
---|
| 114 | Integer starting_year = Integer.parseInt(subjectId.split("\\s+")[2]);
|
---|
| 115 | Integer finishing_year = Integer.parseInt(subjectId.split("\\s+")[3]);
|
---|
| 116 | String termType = subjectId.split("\\s+")[4];
|
---|
| 117 |
|
---|
| 118 | StudentEntity studentEntity = studentRepository.findById(student_id).orElseThrow(Exception::new);
|
---|
| 119 | SchoolYearEntity schoolYearEntity = schoolYearRepository.findById(new SchoolYearId(starting_year, finishing_year, studentEntity)).orElseThrow(Exception::new);
|
---|
| 120 | TermEntity termEntity = termRepository.findById(new TermId(termType, schoolYearEntity)).orElseThrow(Exception::new);
|
---|
| 121 | SubjectEntity subjectEntity = subjectRepository.findById(new SubjectId(subject_id, termEntity)).orElseThrow(Exception::new);
|
---|
| 122 | return subjectEntity;
|
---|
| 123 | }
|
---|
| 124 | }
|
---|