source: src/main/java/com/example/task/service/SubjectService.java

Last change on this file was fdfbdde, checked in by Stojilkova Sara <sara.stojilkova.students.finki.ukim.mk>, 9 months ago

Initial commit

  • Property mode set to 100644
File size: 6.3 KB
Line 
1package com.example.task.service;
2
3import com.example.task.entity.PlaceEntity;
4import com.example.task.entity.StudentEntity;
5import com.example.task.entity.event.*;
6import com.example.task.entity.projection.ExamEvent;
7import com.example.task.entity.projection.NonRepeatingClassEvent;
8import com.example.task.entity.projection.RepeatingClassEvent;
9import com.example.task.entity.schoolyear.SchoolYearEntity;
10import com.example.task.entity.schoolyear.SchoolYearId;
11import com.example.task.entity.subject.SubjectEntity;
12import com.example.task.entity.subject.SubjectId;
13import com.example.task.entity.term.TermEntity;
14import com.example.task.entity.term.TermId;
15import com.example.task.repository.*;
16import com.example.task.repository.event.*;
17import lombok.AllArgsConstructor;
18import org.springframework.stereotype.Service;
19
20import java.sql.Time;
21import java.time.LocalDate;
22import java.time.LocalTime;
23import java.time.format.DateTimeFormatter;
24import java.util.List;
25
26@Service
27@AllArgsConstructor
28public 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}
Note: See TracBrowser for help on using the repository browser.