1 | package com.wediscussmovies.project.service.impl;
|
---|
2 |
|
---|
3 | import com.wediscussmovies.project.ajaxmodels.Grade;
|
---|
4 | import com.wediscussmovies.project.model.Movie;
|
---|
5 | import com.wediscussmovies.project.model.User;
|
---|
6 | import com.wediscussmovies.project.model.exception.MovieHasAlreadyDirector;
|
---|
7 | import com.wediscussmovies.project.model.exception.MovieIdNotFoundException;
|
---|
8 | import com.wediscussmovies.project.model.exception.PersonNotExistException;
|
---|
9 | import com.wediscussmovies.project.model.primarykeys.PersonRatesPK;
|
---|
10 | import com.wediscussmovies.project.model.relation.MovieActors;
|
---|
11 | import com.wediscussmovies.project.model.relation.MovieRates;
|
---|
12 | import com.wediscussmovies.project.model.relation.PersonRates;
|
---|
13 | import com.wediscussmovies.project.repository.MovieActorsRepository;
|
---|
14 | import com.wediscussmovies.project.repository.MovieRepository;
|
---|
15 | import com.wediscussmovies.project.repository.PersonRatesRepository;
|
---|
16 | import com.wediscussmovies.project.repository.PersonRepository;
|
---|
17 | import com.wediscussmovies.project.model.Person;
|
---|
18 | import com.wediscussmovies.project.service.PersonService;
|
---|
19 | import org.springframework.stereotype.Service;
|
---|
20 |
|
---|
21 | import javax.transaction.Transactional;
|
---|
22 | import java.sql.Date;
|
---|
23 | import java.util.List;
|
---|
24 | import java.util.Optional;
|
---|
25 | import java.util.stream.Collectors;
|
---|
26 |
|
---|
27 | @Service
|
---|
28 | public class PersonServiceImpl implements PersonService {
|
---|
29 |
|
---|
30 | private final PersonRepository personRepository;
|
---|
31 | private final MovieRepository movieRepository;
|
---|
32 | private final MovieActorsRepository movieActorsRepository;
|
---|
33 | private final PersonRatesRepository personRatesRepository;
|
---|
34 |
|
---|
35 |
|
---|
36 | public PersonServiceImpl(PersonRepository personRepository,
|
---|
37 | MovieRepository movieRepository, MovieActorsRepository movieActorsRepository,
|
---|
38 | PersonRatesRepository personRatesRepository) {
|
---|
39 | this.personRepository = personRepository;
|
---|
40 | this.movieRepository = movieRepository;
|
---|
41 | this.movieActorsRepository = movieActorsRepository;
|
---|
42 | this.personRatesRepository = personRatesRepository;
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public List<Person> findAll() {
|
---|
47 | return this.personRepository.findAll();
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public List<Person> findAllDirectors() {
|
---|
52 | return this.personRepository.findAllByType('D');
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public Person findById(Integer id) {
|
---|
57 | return this.personRepository.findById(id).orElseThrow(() -> new PersonNotExistException(id));
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public List<Person> findAllActors() {
|
---|
64 | return this.personRepository.findAllByType('A');
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | @Transactional
|
---|
69 | public Person save(String name, String surname, Character type, Date birthDate, String image_url,
|
---|
70 | String description,List<Integer> movieIds) {
|
---|
71 |
|
---|
72 | Person person = this.personRepository.saveAndFlush(new Person(name,surname,type,birthDate,image_url,description));
|
---|
73 |
|
---|
74 | if (movieIds!= null) {
|
---|
75 | if (type.equals('A'))
|
---|
76 | this.addActorForMovies(person, movieIds);
|
---|
77 | else
|
---|
78 | this.addDirectorForMovies(person, movieIds);
|
---|
79 | }
|
---|
80 | return person;
|
---|
81 |
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | @Transactional
|
---|
86 | public Person edit(Integer personId, String name, String surname,
|
---|
87 | Character type, Date birthDate, String image_url, String description, List<Integer> movieIds) {
|
---|
88 | Person person = this.findById(personId);
|
---|
89 |
|
---|
90 | person.setName(name);
|
---|
91 | person.setSurname(surname);
|
---|
92 | person.setDateOfBirth(birthDate);
|
---|
93 | person.setImageUrl(image_url);
|
---|
94 | person.setType(type);
|
---|
95 | person.setDescription(description);
|
---|
96 |
|
---|
97 |
|
---|
98 | person = this.personRepository.saveAndFlush(person);
|
---|
99 |
|
---|
100 | this.movieActorsRepository.deleteAllByPerson(person);
|
---|
101 | this.removeMovieDirectorsBeforeEdit(person);
|
---|
102 | if (movieIds != null) {
|
---|
103 | if (type.equals('A'))
|
---|
104 | this.addActorForMovies(person, movieIds);
|
---|
105 | else
|
---|
106 | this.addDirectorForMovies(person, movieIds);
|
---|
107 | }
|
---|
108 |
|
---|
109 | return person;
|
---|
110 |
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Override
|
---|
114 | public List<Person> findPersonsByNameOrSurname(Character type,String searchQuery) {
|
---|
115 | if (searchQuery != null && searchQuery.isEmpty())
|
---|
116 | return this.personRepository.findAllByTypeAndNameLikeOrSurnameLike(type,searchQuery,searchQuery);
|
---|
117 | if (type.equals('D'))
|
---|
118 | return this.findAllDirectors();
|
---|
119 | return this.findAllActors();
|
---|
120 |
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override
|
---|
124 | public List<Movie> findAllMoviesByPerson(Person person) {
|
---|
125 | if (person.getType().equals('A'))
|
---|
126 | return this.movieActorsRepository
|
---|
127 | .findAllByPerson(person)
|
---|
128 | .stream()
|
---|
129 | .map(MovieActors::getMovie)
|
---|
130 | .collect(Collectors.toList());
|
---|
131 | return this.movieRepository.findAllByDirector(person);
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override
|
---|
135 | public void deleteById(Integer id) {
|
---|
136 | this.personRepository.deleteById(id);
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override
|
---|
140 | public void addGradePerson(Integer personId, User user, Grade grade) {
|
---|
141 | PersonRatesPK key = new PersonRatesPK(personId, user.getUserId());
|
---|
142 | Optional<PersonRates> personRates = this.personRatesRepository.findById(key);
|
---|
143 | if (personRates.isPresent()){
|
---|
144 | PersonRates rates = personRates.get();
|
---|
145 | rates.setReason(grade.getReason());
|
---|
146 | rates.setStarsRated(grade.getRating());
|
---|
147 | this.personRatesRepository.save(rates);
|
---|
148 | }
|
---|
149 | else{
|
---|
150 | Person person = this.findById(personId);
|
---|
151 | PersonRates rates = new PersonRates(user,person,grade.getReason(),grade.getRating());
|
---|
152 | this.personRatesRepository.save(rates);
|
---|
153 | }
|
---|
154 |
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | private void addActorForMovies(Person person, List<Integer> movieIds){
|
---|
159 |
|
---|
160 | movieIds.stream()
|
---|
161 | .map(this::findMovieById)
|
---|
162 | .forEach(m -> this.movieActorsRepository.save(new MovieActors(m,person)));
|
---|
163 |
|
---|
164 | }
|
---|
165 | private void addDirectorForMovies(Person person, List<Integer> movieIds){
|
---|
166 | movieIds.stream()
|
---|
167 | .map(this::findMovieById)
|
---|
168 | .forEach(m -> this.addMovieDirector(m,person));
|
---|
169 | }
|
---|
170 | private void addMovieDirector(Movie movie,Person person){
|
---|
171 | Person director = movie.getDirector();
|
---|
172 | if (director != null)
|
---|
173 | throw new MovieHasAlreadyDirector(movie.getTitle(),director.getName(),director.getSurname());
|
---|
174 | movie.setDirector(person);
|
---|
175 | this.movieRepository.save(movie);
|
---|
176 | }
|
---|
177 | private void removeMovieDirectorsBeforeEdit(Person person){
|
---|
178 | this.movieRepository.findAllByDirector(person)
|
---|
179 | .forEach(m ->{ m.setDirector(null); this.movieRepository.save(m); });
|
---|
180 |
|
---|
181 | }
|
---|
182 |
|
---|
183 | private Movie findMovieById(Integer id){
|
---|
184 | return this.movieRepository.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id));
|
---|
185 | }
|
---|
186 |
|
---|
187 | }
|
---|