source: src/main/java/project/educatum/service/impl/StudentServiceImpl.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: 6.1 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.exceptions.*;
7import project.educatum.model.primarykeys.InterestID;
8import project.educatum.model.primarykeys.ListeningID;
9import project.educatum.model.relations.TeacherStudentRelation;
10import project.educatum.repository.*;
11import project.educatum.service.StudentService;
12
13import java.time.LocalDate;
14import java.util.ArrayList;
15import java.util.HashSet;
16import java.util.List;
17import java.util.Set;
18import java.util.stream.Collectors;
19
20@Service
21public class StudentServiceImpl implements StudentService {
22
23 private final StudentRepository studentsRepository;
24 private final PasswordEncoder passwordEncoder;
25 private final AdminRepository adminRepository;
26 private final TeacherRepository teachersRepository;
27 private final InterestRepository interestRepository;
28 private static Integer counter = 0;
29 private final ListeningRepository listeningRepository;
30 private final PaymentRepository paymentRepository;
31 private final TeacherStudentRepository teacherStudentRepository;
32
33 public StudentServiceImpl(StudentRepository studentsRepository, PasswordEncoder passwordEncoder, AdminRepository adminRepository, TeacherRepository teachersRepository, InterestRepository interestRepository, ListeningRepository listeningRepository, PaymentRepository paymentRepository, TeacherStudentRepository teacherStudentRepository) {
34 this.studentsRepository = studentsRepository;
35 this.passwordEncoder = passwordEncoder;
36 this.adminRepository = adminRepository;
37 this.teachersRepository = teachersRepository;
38 this.interestRepository = interestRepository;
39 this.listeningRepository = listeningRepository;
40 this.paymentRepository = paymentRepository;
41 this.teacherStudentRepository = teacherStudentRepository;
42 }
43
44 @Override
45 public void register(String name, String surname, String email, String password, String repeatPassword, String telNum, String desc) {
46 if (email == null || email.isEmpty() || password == null || password.isEmpty())
47 throw new InvalidArgumentsException();
48 if (!password.equals(repeatPassword))
49 throw new PasswordsDoNotMatchException();
50
51 for (Teacher n : teachersRepository.findAll())
52 if (n.getEmail().equals(email)) throw new UsernameAlreadyExistsException("Username already exists!");
53
54 for (Student u : studentsRepository.findAll())
55 if (u.getEmail().equals(email)) throw new UsernameAlreadyExistsException("Username already exists!");
56
57 for (Admin a : adminRepository.findAll())
58 if (a.getEmail().equals(email)) throw new UsernameAlreadyExistsException("Username already exists!");
59
60 Student user = new Student(desc, name, surname, email, passwordEncoder.encode(password), telNum);
61 studentsRepository.save(user);
62 }
63
64 @Override
65 public List<Student> findAll() {
66 return studentsRepository.findAll();
67 }
68
69 @Override
70 public Student findByEmail(String email) {
71 return studentsRepository.findByEmail(email);
72 }
73
74 @Override
75 public List<Student> findAllByNameLike(String name, List<Student> students) {
76 List<Student> searchedStudents = studentsRepository.findAllByNameContainingIgnoreCase(name);
77 Set<Student> result = new HashSet<>();
78 for (Student u : searchedStudents) {
79 for (Student u2 : students)
80 if (u.getId().equals(u2.getId())) result.add(u2);
81 }
82 return new ArrayList<>(result);
83 }
84
85 @Override
86 public void delete(Integer id) {
87 Student u = studentsRepository.findById(id).orElseThrow(StudentNotFoundException::new);
88 studentsRepository.delete(u);
89 }
90
91 @Override
92 public Student findById(Integer id) {
93 return studentsRepository.findById(id).orElseThrow(StudentNotFoundException::new);
94 }
95
96 @Override
97 public List<Student> findAllByName(String name) {
98 return studentsRepository.findAllByNameContainingIgnoreCase(name);
99 }
100
101 @Override
102 public void interestedIn(Integer subjectId, Integer studentId) {
103 InterestID zId = new InterestID(subjectId, studentId);
104 Interest z = new Interest(zId, LocalDate.now());
105 interestRepository.save(z);
106 }
107
108
109 @Override
110 public void save(Student student){
111 studentsRepository.save(student);
112 }
113
114 @Override
115 public void addListening(Integer studentID, Integer classID, Integer teacherID) {
116 Teacher t = teachersRepository.findById(teacherID).orElseThrow(TeacherNotFoundException::new);
117 Payment p = new Payment(0, t);
118 paymentRepository.save(p);
119 Student s = studentsRepository.findById(studentID).orElseThrow(StudentNotFoundException::new);
120 ListeningID listeningID = new ListeningID(classID, counter++);
121 Listening l = new Listening(listeningID, p, s, false);
122 listeningRepository.save(l);
123 }
124
125
126 @Override
127 public boolean rateTeacher(Teacher teacher, Student student, Float rating) {
128 List<TeacherStudentRelation> list = teacherStudentRepository.findAll().stream()
129 .filter(t -> t.getId().getStudentID().equals(student.getId()) && t.getId().getTeacherID().equals(teacher.getId()))
130 .collect(Collectors.toList());
131 if (list.size() > 0) {
132 TeacherStudentRelation teacherStudent = list.get(0);
133 teacherStudent.setRating(rating);
134 teacher.setRating(rating);
135 teacherStudent.setHasRated(true);
136 teacherStudentRepository.save(teacherStudent);
137 return true;
138 }
139 return false;
140
141 }
142
143 @Override
144 public Student edit(Integer id, String ime, String prezime, String opis, String email, String telefonskiBroj) {
145 Student student = this.findById(id);
146 student.setName(ime);
147 student.setSurname(prezime);
148 student.setDescription(opis);
149 student.setEmail(email);
150 student.setTelephoneNumber(telefonskiBroj);
151 return this.studentsRepository.save(student);
152 }
153}
Note: See TracBrowser for help on using the repository browser.