source: src/main/java/project/educatum/service/impl/HomeworkServiceImpl.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.3 KB
Line 
1package project.educatum.service.impl;
2
3import org.springframework.stereotype.Service;
4import project.educatum.model.Class;
5import project.educatum.model.Homework;
6import project.educatum.model.Teacher;
7import project.educatum.model.exceptions.HomeworkNotFoundException;
8import project.educatum.model.exceptions.TeacherNotFoundException;
9import project.educatum.repository.AdminRepository;
10import project.educatum.repository.ClassRepository;
11import project.educatum.repository.HomeworkRepository;
12import project.educatum.repository.TeacherRepository;
13import project.educatum.service.HomeworkService;
14
15import java.util.Comparator;
16import java.util.List;
17import java.util.stream.Collectors;
18
19@Service
20public 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}
Note: See TracBrowser for help on using the repository browser.