1 | package mk.profesori.springapp.Service;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.List;
|
---|
5 |
|
---|
6 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
7 | import org.springframework.stereotype.Service;
|
---|
8 |
|
---|
9 | import mk.profesori.springapp.Repository.CityRepository;
|
---|
10 | import mk.profesori.springapp.Repository.FacultyRepository;
|
---|
11 | import mk.profesori.springapp.Repository.OpinionRepository;
|
---|
12 | import mk.profesori.springapp.Repository.ProfessorRepository;
|
---|
13 | import mk.profesori.springapp.Repository.StudyProgrammeRepository;
|
---|
14 | import mk.profesori.springapp.Repository.UniversityRepository;
|
---|
15 | import mk.profesori.springapp.Repository.UserRepository;
|
---|
16 | import mk.profesori.springapp.Model.City;
|
---|
17 | import mk.profesori.springapp.Model.CustomUserDetails;
|
---|
18 | import mk.profesori.springapp.Model.Faculty;
|
---|
19 | import mk.profesori.springapp.Model.Opinion;
|
---|
20 | import mk.profesori.springapp.Model.Professor;
|
---|
21 | import mk.profesori.springapp.Model.StudyProgramme;
|
---|
22 | import mk.profesori.springapp.Model.University;
|
---|
23 |
|
---|
24 | @Service
|
---|
25 | public class MainService {
|
---|
26 |
|
---|
27 | @Autowired
|
---|
28 | private ProfessorRepository professorRepository;
|
---|
29 | @Autowired
|
---|
30 | private StudyProgrammeRepository studyProgrammeRepository;
|
---|
31 | @Autowired
|
---|
32 | private FacultyRepository facultyRepository;
|
---|
33 | @Autowired
|
---|
34 | private UniversityRepository universityRepository;
|
---|
35 | @Autowired
|
---|
36 | private CityRepository cityRepository;
|
---|
37 | @Autowired
|
---|
38 | private OpinionRepository opinionRepository;
|
---|
39 | @Autowired
|
---|
40 | private UserRepository userRepository;
|
---|
41 |
|
---|
42 | public List<Professor> getAllProfessors() {
|
---|
43 |
|
---|
44 | List<Professor> list = new ArrayList<>();
|
---|
45 | professorRepository.findAll().forEach(list::add);
|
---|
46 | return list;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public Professor getProfessorById(Long id) {
|
---|
50 |
|
---|
51 | return professorRepository.findByProfessorId(id);
|
---|
52 | }
|
---|
53 |
|
---|
54 | public List<Professor> getProfessorsByFacultyId(Long facultyId) {
|
---|
55 |
|
---|
56 | Faculty faculty = facultyRepository.findByFacultyId(facultyId);
|
---|
57 |
|
---|
58 | List<Professor> list = new ArrayList<>();
|
---|
59 | professorRepository.findByFaculty(faculty).forEach(list::add);
|
---|
60 | return list;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public List<Professor> getProfessorsByNameContains(String contained) {
|
---|
64 | List<Professor> list = new ArrayList<>();
|
---|
65 | professorRepository.findByProfessorNameContainingIgnoreCase(contained).forEach(list::add);
|
---|
66 | return list;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public List<StudyProgramme> getAllStudyProgrammes() {
|
---|
70 |
|
---|
71 | List<StudyProgramme> list = new ArrayList<>();
|
---|
72 | studyProgrammeRepository.findAll().forEach(list::add);
|
---|
73 | return list;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public StudyProgramme getStudyProgrammeById(Long id) {
|
---|
77 |
|
---|
78 | return studyProgrammeRepository.findByStudyProgrammeId(id);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public List<StudyProgramme> getStudyProgrammesByFacultyId(Long facultyId) {
|
---|
82 |
|
---|
83 | Faculty faculty = facultyRepository.findByFacultyId(facultyId);
|
---|
84 |
|
---|
85 | List<StudyProgramme> list = new ArrayList<>();
|
---|
86 | studyProgrammeRepository.findByFaculty(faculty).forEach(list::add);
|
---|
87 | return list;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public List<Faculty> getAllFaculties() {
|
---|
91 | List<Faculty> list = new ArrayList<>();
|
---|
92 | facultyRepository.findAll().forEach(list::add);
|
---|
93 | return list;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public Faculty getFacultyById(Long id) {
|
---|
97 | return facultyRepository.findByFacultyId(id);
|
---|
98 | }
|
---|
99 |
|
---|
100 | public List<Faculty> getFacultiesByUniversityId(Long universityId) {
|
---|
101 |
|
---|
102 | University university = universityRepository.findByUniversityId(universityId);
|
---|
103 |
|
---|
104 | List<Faculty> list = new ArrayList<>();
|
---|
105 | facultyRepository.findByUniversity(university).forEach(list::add);
|
---|
106 | return list;
|
---|
107 | }
|
---|
108 |
|
---|
109 | public List<University> getAllUniversities() {
|
---|
110 | List<University> list = new ArrayList<>();
|
---|
111 | universityRepository.findAll().forEach(list::add);
|
---|
112 | return list;
|
---|
113 | }
|
---|
114 |
|
---|
115 | public University getUniversityById(Long id) {
|
---|
116 | return universityRepository.findByUniversityId(id);
|
---|
117 | }
|
---|
118 |
|
---|
119 | public List<University> getUniversitiesByCityId(Long cityId) {
|
---|
120 |
|
---|
121 | City city = cityRepository.findByCityId(cityId);
|
---|
122 |
|
---|
123 | List<University> list = new ArrayList<>();
|
---|
124 | universityRepository.findByCity(city).forEach(list::add);
|
---|
125 | return list;
|
---|
126 | }
|
---|
127 |
|
---|
128 | public List<City> getAllCities() {
|
---|
129 | List<City> list = new ArrayList<>();
|
---|
130 | cityRepository.findAll().forEach(list::add);
|
---|
131 | return list;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public City getCityById(Long id) {
|
---|
135 | return cityRepository.findByCityId(id);
|
---|
136 | }
|
---|
137 |
|
---|
138 | public void addOpinion(String title, String content, Long professorId, CustomUserDetails currentUser) {
|
---|
139 |
|
---|
140 | Professor targetProfessor = professorRepository.findByProfessorId(professorId);
|
---|
141 |
|
---|
142 | Opinion opinionToAdd = new Opinion(title, content, currentUser, null, null,
|
---|
143 | null, targetProfessor);
|
---|
144 |
|
---|
145 | opinionRepository.save(opinionToAdd);
|
---|
146 | }
|
---|
147 |
|
---|
148 | public void replyToOpinion(String content, Long professorId, Long postId, CustomUserDetails currentUser) {
|
---|
149 |
|
---|
150 | Professor targetProfessor = professorRepository.findByProfessorId(professorId);
|
---|
151 | Opinion targetOpinion = opinionRepository.findByPostId(postId);
|
---|
152 |
|
---|
153 | Opinion opinionToAdd = new Opinion(null, content, currentUser, null, null,
|
---|
154 | targetOpinion, null, targetProfessor);
|
---|
155 | opinionRepository.save(opinionToAdd);
|
---|
156 |
|
---|
157 | targetOpinion.getChildren().add(opinionToAdd);
|
---|
158 | opinionRepository.save(targetOpinion);
|
---|
159 | }
|
---|
160 |
|
---|
161 | public void upvoteOpinion(Long postId, CustomUserDetails currentUser) {
|
---|
162 | Opinion targetOpinion = opinionRepository.findByPostId(postId);
|
---|
163 |
|
---|
164 | if (!targetOpinion.getLikes().contains(currentUser)) {
|
---|
165 |
|
---|
166 | targetOpinion.getLikes().add(currentUser);
|
---|
167 | // opinionRepository.save(targetOpinion);
|
---|
168 |
|
---|
169 | targetOpinion.getAuthor().setKarma(targetOpinion.getAuthor().getKarma() + 1);
|
---|
170 | userRepository.save(targetOpinion.getAuthor());
|
---|
171 |
|
---|
172 | currentUser.getLikedPosts().add(targetOpinion);
|
---|
173 | userRepository.save(currentUser);
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | public void downvoteOpinion(Long postId, CustomUserDetails currentUser) {
|
---|
178 | Opinion targetOpinion = opinionRepository.findByPostId(postId);
|
---|
179 |
|
---|
180 | if (!targetOpinion.getDislikes().contains(currentUser)) {
|
---|
181 |
|
---|
182 | targetOpinion.getDislikes().add(currentUser);
|
---|
183 | // opinionRepository.save(targetOpinion);
|
---|
184 |
|
---|
185 | targetOpinion.getAuthor().setKarma(targetOpinion.getAuthor().getKarma() - 1);
|
---|
186 | userRepository.save(targetOpinion.getAuthor());
|
---|
187 |
|
---|
188 | currentUser.getDislikedPosts().add(targetOpinion);
|
---|
189 | userRepository.save(currentUser);
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|