1 | package project.educatum.service.impl;
|
---|
2 |
|
---|
3 | import org.springframework.stereotype.Service;
|
---|
4 | import project.educatum.model.Class;
|
---|
5 | import project.educatum.model.Teacher;
|
---|
6 | import project.educatum.model.Subject;
|
---|
7 | import project.educatum.model.exceptions.SubjectNotFoundException;
|
---|
8 | import project.educatum.model.exceptions.TeacherNotFoundException;
|
---|
9 | import project.educatum.repository.ClassRepository;
|
---|
10 | import project.educatum.repository.TeacherRepository;
|
---|
11 | import project.educatum.repository.SubjectRepository;
|
---|
12 | import project.educatum.service.ClassService;
|
---|
13 |
|
---|
14 | import java.time.LocalDateTime;
|
---|
15 | import java.util.Comparator;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.stream.Collectors;
|
---|
18 |
|
---|
19 | @Service
|
---|
20 | public class ClassServiceImpl implements ClassService {
|
---|
21 |
|
---|
22 | private final ClassRepository classRepository;
|
---|
23 | private final TeacherRepository teacherRepository;
|
---|
24 | private final SubjectRepository subjectRepository;
|
---|
25 |
|
---|
26 | public ClassServiceImpl(ClassRepository classRepository, TeacherRepository teacherRepository, SubjectRepository subjectRepository) {
|
---|
27 | this.classRepository = classRepository;
|
---|
28 | this.teacherRepository = teacherRepository;
|
---|
29 | this.subjectRepository = subjectRepository;
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public List<Class> findAll() {
|
---|
34 | return classRepository.findAll()
|
---|
35 | .stream()
|
---|
36 | .sorted(Comparator.comparing(Class::getBeginningTime))
|
---|
37 | .collect(Collectors.toList());
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public void addClass(LocalDateTime dateTime, String desc, Integer teacherID, Integer subjectID) {
|
---|
42 | Teacher n = teacherRepository.findById(teacherID).orElseThrow(TeacherNotFoundException::new);
|
---|
43 | Subject p = subjectRepository.findById(subjectID).orElseThrow(SubjectNotFoundException::new);
|
---|
44 | Class c = new Class(dateTime, desc, n, p);
|
---|
45 | classRepository.save(c);
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public List<Class> findAllByTeacher(Integer id) {
|
---|
50 | return classRepository
|
---|
51 | .findAllByIdTeacher(id)
|
---|
52 | .stream()
|
---|
53 | .sorted(Comparator.comparing(Class::getBeginningTime))
|
---|
54 | .collect(Collectors.toList());
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public Class findByTopic(String tema) {
|
---|
59 | return classRepository.findByTopic(tema);
|
---|
60 | }
|
---|
61 |
|
---|
62 | }
|
---|