source: src/main/java/project/educatum/service/impl/TeacherServiceImpl.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: 8.6 KB
Line 
1package project.educatum.service.impl;
2
3import org.springframework.security.crypto.password.PasswordEncoder;
4import org.springframework.stereotype.Service;
5import project.educatum.model.*;
6import project.educatum.model.Class;
7import project.educatum.model.exceptions.*;
8import project.educatum.model.primarykeys.TeacherStudentRelationID;
9import project.educatum.model.primarykeys.TeacherSubjectRelationID;
10import project.educatum.model.relations.TeacherStudentRelation;
11import project.educatum.model.relations.TeacherSubjectRelation;
12import project.educatum.repository.*;
13import project.educatum.service.TeacherService;
14
15import java.util.ArrayList;
16import java.util.Comparator;
17import java.util.List;
18import java.util.stream.Collectors;
19
20@Service
21public class TeacherServiceImpl implements TeacherService {
22
23 private final TeacherRepository teachersRepository;
24 private final PasswordEncoder passwordEncoder;
25 private final AdminRepository adminRepository;
26 private final StudentRepository studentsRepository;
27 private final TeacherStudentRepository teacherStudentRepository;
28 private final TeacherSubjectRepository teacherSubjectRepository;
29 private final SubjectRepository subjectRepository;
30 private final PaymentRepository paymentRepository;
31 private final ClassRepository classesRepository;
32 private final ListeningRepository listeningRepository;
33
34
35 public TeacherServiceImpl(TeacherRepository teachersRepository, PasswordEncoder passwordEncoder, AdminRepository adminRepository, StudentRepository studentsRepository, TeacherStudentRepository teacherStudentRepository, TeacherSubjectRepository teacherSubjectRepository, SubjectRepository subjectRepository, PaymentRepository paymentRepository, ClassRepository classesRepository, ListeningRepository listeningRepository) {
36 this.teachersRepository = teachersRepository;
37 this.passwordEncoder = passwordEncoder;
38 this.adminRepository = adminRepository;
39 this.studentsRepository = studentsRepository;
40 this.teacherStudentRepository = teacherStudentRepository;
41 this.teacherSubjectRepository = teacherSubjectRepository;
42 this.subjectRepository = subjectRepository;
43 this.paymentRepository = paymentRepository;
44 this.classesRepository = classesRepository;
45 this.listeningRepository = listeningRepository;
46 }
47
48 @Override
49 public List<Teacher> findAll() {
50 return teachersRepository.findAll()
51 .stream()
52 .sorted(Comparator.comparing(Teacher::getEmail))
53 .collect(Collectors.toList());
54 }
55
56 @Override
57 public void register(String ime, String prezime, String email, String password, String repeatPassword, String telBroj, String opis) {
58 if (email == null || email.isEmpty() || password == null || password.isEmpty())
59 throw new InvalidArgumentsException();
60
61 if (!password.equals(repeatPassword)) throw new PasswordsDoNotMatchException();
62
63 for (Teacher n : teachersRepository.findAll()) {
64 if (n.getEmail().equals(email)) {
65 throw new UsernameAlreadyExistsException("Username already exists!");
66 }
67 }
68 for (Student u : studentsRepository.findAll()) {
69 if (u.getEmail().equals(email)) {
70 throw new UsernameAlreadyExistsException("Username already exists!");
71 }
72 }
73 for (Admin a : adminRepository.findAll()) {
74 if (a.getEmail().equals(email)) {
75 throw new UsernameAlreadyExistsException("Username already exists!");
76 }
77 }
78
79 Teacher user = new Teacher(ime, prezime, opis, email, passwordEncoder.encode(password), telBroj);
80 user.setIdAdmin(adminRepository.findAll().get(0));
81 teachersRepository.save(user);
82 }
83
84 @Override
85 public Teacher findByEmail(String email) {
86 return teachersRepository.findByEmail(email);
87 }
88
89 @Override
90 public List<Class> getClassesByTeacher(Integer id) {
91 return classesRepository.findAllByIdTeacher(id)
92 .stream()
93 .sorted(Comparator.comparing(Class::beginningDate))
94 .collect(Collectors.toList());
95 }
96
97 @Override
98 public void save(Teacher teacher){
99 this.teachersRepository.save(teacher);
100 }
101
102 @Override
103 public List<Teacher> findAllByNameLike(String name) {
104 return teachersRepository.findAllByNameContainingIgnoreCase(name);
105 }
106
107 @Override
108 public void delete(Integer id) {
109 Teacher n = teachersRepository.findById(id).orElseThrow(TeacherNotFoundException::new);
110 teachersRepository.delete(n);
111 }
112
113 @Override
114 public List<Student> getStudentsByTeacher(Integer id) {
115 List<Student> students;
116 List<TeacherStudentRelation> teacherStudentRelationList = teacherStudentRepository.findAll();
117 students = teacherStudentRelationList
118 .stream()
119 .map(TeacherStudentRelation::getId)
120 .filter(pId -> pId.getTeacherID().equals(id))
121 .map(TeacherStudentRelationID::getStudentID)
122 .map(studentID -> studentsRepository.findById(studentID).orElseThrow(StudentNotFoundException::new))
123 .collect(Collectors.toList());
124 return students;
125 }
126
127 @Override
128 public List<Teacher> getAllTeachersBySubject(Integer id) {
129 List<Teacher> teachers;
130 List<TeacherSubjectRelation> teacherSubjectRelationList = teacherSubjectRepository.findAll();
131 teachers = teacherSubjectRelationList
132 .stream()
133 .map(TeacherSubjectRelation::getId)
134 .filter(ppId -> ppId.getSubjectID().equals(id))
135 .map(TeacherSubjectRelationID::getTeacherID)
136 .map(idTeacher -> teachersRepository.findById(idTeacher).orElseThrow(TeacherNotFoundException::new))
137 .collect(Collectors.toList());
138 return teachers;
139 }
140
141 @Override
142 public Teacher edit(Integer id, String ime, String prezime,String description, String email, String telephoneNumber) {
143 Teacher teacher = this.findById(id);
144 teacher.setName(ime);
145 teacher.setSurname(prezime);
146 teacher.setDescription(description);
147 teacher.setEmail(email);
148 teacher.setTelephoneNumber(telephoneNumber);
149 return this.teachersRepository.save(teacher);
150 }
151
152 @Override
153 public void updateEnabled(Integer teacherID) {
154 teachersRepository.updateEnabled(teacherID);
155 }
156
157 @Override
158 public List<Subject> getSubjectsByTeacher(Integer id) {
159 List<Subject> subjects;
160 List<TeacherSubjectRelation> teacherSubjectRelationList = teacherSubjectRepository.findAll();
161 subjects = teacherSubjectRelationList
162 .stream()
163 .map(TeacherSubjectRelation::getId)
164 .filter(ppId -> ppId.getTeacherID().equals(id))
165 .map(TeacherSubjectRelationID::getSubjectID)
166 .map(subjectID -> subjectRepository.findById(subjectID).orElseThrow(SubjectNotFoundException::new))
167 .collect(Collectors.toList());
168 return subjects;
169 }
170
171 @Override
172 public Teacher findById(Integer id) {
173 return teachersRepository.findById(id).orElseThrow(TeacherNotFoundException::new);
174 }
175
176 @Override
177 public void addSubject(Integer teacherId, Integer subjectId, String desc) {
178 TeacherSubjectRelationID ppId = new TeacherSubjectRelationID(teacherId, subjectId);
179 teacherSubjectRepository.save(new TeacherSubjectRelation(ppId, desc));
180 }
181
182
183 @Override
184 public void addStudent(Integer teacherID, Integer studentID, Integer priceByClass, Integer numScheduledClasses) {
185 Teacher teacher = teachersRepository.findById(teacherID).orElseThrow(TeacherNotFoundException::new);
186 Student student = studentsRepository.findById(studentID).orElseThrow(StudentNotFoundException::new);
187 TeacherStudentRelationID pId = new TeacherStudentRelationID(teacher.getId(), student.getId());
188 teacherStudentRepository.save(new TeacherStudentRelation(pId, priceByClass, numScheduledClasses));
189 }
190
191
192 @Override
193 public double getRatingForTeacher(Long teacherID) {
194 List<TeacherStudentRelation> teacherStudentRelationList = teacherStudentRepository
195 .findAll()
196 .stream()
197 .filter(t -> t.getId().getTeacherID().equals(teacherID.intValue()))
198 .filter(TeacherStudentRelation::hasRated)
199 .collect(Collectors.toList());
200 float rating = 0;
201 for (TeacherStudentRelation t : teacherStudentRelationList) {
202 rating += t.getRating();
203 }
204 return rating / (teacherStudentRelationList.size() * 1.0);
205 }
206}
Note: See TracBrowser for help on using the repository browser.