source: src/main/java/project/educatum/service/impl/ClassServiceImpl.java

Last change on this file was d3cf3a1, checked in by Marija Micevska <marija_micevska@…>, 2 years ago

Initial commit

  • Property mode set to 100644
File size: 2.2 KB
Line 
1package project.educatum.service.impl;
2
3import org.springframework.stereotype.Service;
4import project.educatum.model.Class;
5import project.educatum.model.Teacher;
6import project.educatum.model.Subject;
7import project.educatum.model.exceptions.SubjectNotFoundException;
8import project.educatum.model.exceptions.TeacherNotFoundException;
9import project.educatum.repository.ClassRepository;
10import project.educatum.repository.TeacherRepository;
11import project.educatum.repository.SubjectRepository;
12import project.educatum.service.ClassService;
13
14import java.time.LocalDateTime;
15import java.util.Comparator;
16import java.util.List;
17import java.util.stream.Collectors;
18
19@Service
20public 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}
Note: See TracBrowser for help on using the repository browser.