1 | package project.educatum.service.impl;
|
---|
2 |
|
---|
3 | import org.springframework.stereotype.Service;
|
---|
4 | import project.educatum.model.Class;
|
---|
5 | import project.educatum.model.Homework;
|
---|
6 | import project.educatum.model.Teacher;
|
---|
7 | import project.educatum.model.exceptions.HomeworkNotFoundException;
|
---|
8 | import project.educatum.model.exceptions.TeacherNotFoundException;
|
---|
9 | import project.educatum.repository.AdminRepository;
|
---|
10 | import project.educatum.repository.ClassRepository;
|
---|
11 | import project.educatum.repository.HomeworkRepository;
|
---|
12 | import project.educatum.repository.TeacherRepository;
|
---|
13 | import project.educatum.service.HomeworkService;
|
---|
14 |
|
---|
15 | import java.util.Comparator;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.stream.Collectors;
|
---|
18 |
|
---|
19 | @Service
|
---|
20 | public class HomeworkServiceImpl implements HomeworkService {
|
---|
21 |
|
---|
22 | private final HomeworkRepository homeworkRepository;
|
---|
23 | private final TeacherRepository teacherRepository;
|
---|
24 | private final ClassRepository classRepository;
|
---|
25 |
|
---|
26 | public HomeworkServiceImpl(HomeworkRepository homeworkRepository, AdminRepository adminRepository, TeacherRepository teacherRepository, ClassRepository classRepository) {
|
---|
27 | this.homeworkRepository = homeworkRepository;
|
---|
28 | this.teacherRepository = teacherRepository;
|
---|
29 | this.classRepository = classRepository;
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public List<Homework> findAll() {
|
---|
34 | return this.homeworkRepository.findAll()
|
---|
35 | .stream()
|
---|
36 | .sorted(Comparator.comparing(Homework::getDescription))
|
---|
37 | .collect(Collectors.toList());
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public List<Homework> findAllByDescriptionLike(String desc) {
|
---|
42 | if (desc != null) {
|
---|
43 | return this.homeworkRepository.findAllByDescriptionContainingIgnoreCase(desc);
|
---|
44 | } else {
|
---|
45 | return this.homeworkRepository.findAll();
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public Homework create(String desc, Integer teacherID, Integer classID) {
|
---|
51 | Teacher teacher = teacherRepository.findById(teacherID).orElseThrow(TeacherNotFoundException::new);
|
---|
52 | Class aClass = classRepository.findById(classID).orElseThrow();
|
---|
53 | Homework homework = new Homework(desc, teacher, aClass);
|
---|
54 | return this.homeworkRepository.save(homework);
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public Homework findById(Integer id) {
|
---|
59 | return this.homeworkRepository.findById(id).orElseThrow(HomeworkNotFoundException::new);
|
---|
60 | }
|
---|
61 | } |
---|