[7a0bf79] | 1 | package com.wediscussmovies.project.service.impl;
|
---|
| 2 |
|
---|
[e0ef1b1] | 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;
|
---|
[7a0bf79] | 16 | import com.wediscussmovies.project.repository.PersonRepository;
|
---|
[e0ef1b1] | 17 | import com.wediscussmovies.project.model.Person;
|
---|
[7a0bf79] | 18 | import com.wediscussmovies.project.service.PersonService;
|
---|
| 19 | import org.springframework.stereotype.Service;
|
---|
| 20 |
|
---|
[e0ef1b1] | 21 | import javax.transaction.Transactional;
|
---|
| 22 | import java.sql.Date;
|
---|
[7a0bf79] | 23 | import java.util.List;
|
---|
| 24 | import java.util.Optional;
|
---|
[e0ef1b1] | 25 | import java.util.stream.Collectors;
|
---|
[7a0bf79] | 26 |
|
---|
| 27 | @Service
|
---|
| 28 | public class PersonServiceImpl implements PersonService {
|
---|
[e0ef1b1] | 29 |
|
---|
[7a0bf79] | 30 | private final PersonRepository personRepository;
|
---|
[e0ef1b1] | 31 | private final MovieRepository movieRepository;
|
---|
| 32 | private final MovieActorsRepository movieActorsRepository;
|
---|
| 33 | private final PersonRatesRepository personRatesRepository;
|
---|
[7a0bf79] | 34 |
|
---|
[7fafead] | 35 |
|
---|
[e0ef1b1] | 36 | public PersonServiceImpl(PersonRepository personRepository,
|
---|
| 37 | MovieRepository movieRepository, MovieActorsRepository movieActorsRepository,
|
---|
| 38 | PersonRatesRepository personRatesRepository) {
|
---|
[7a0bf79] | 39 | this.personRepository = personRepository;
|
---|
[e0ef1b1] | 40 | this.movieRepository = movieRepository;
|
---|
| 41 | this.movieActorsRepository = movieActorsRepository;
|
---|
| 42 | this.personRatesRepository = personRatesRepository;
|
---|
[7a0bf79] | 43 | }
|
---|
| 44 |
|
---|
| 45 | @Override
|
---|
[e0ef1b1] | 46 | public List<Person> findAll() {
|
---|
| 47 | return this.personRepository.findAll();
|
---|
[7a0bf79] | 48 | }
|
---|
| 49 |
|
---|
| 50 | @Override
|
---|
[e0ef1b1] | 51 | public List<Person> findAllDirectors() {
|
---|
| 52 | return this.personRepository.findAllByType('D');
|
---|
[7a0bf79] | 53 | }
|
---|
| 54 |
|
---|
[2a5d6a3] | 55 | @Override
|
---|
[e0ef1b1] | 56 | public Person findById(Integer id) {
|
---|
| 57 | return this.personRepository.findById(id).orElseThrow(() -> new PersonNotExistException(id));
|
---|
[2a5d6a3] | 58 | }
|
---|
[7a0bf79] | 59 |
|
---|
[e0ef1b1] | 60 |
|
---|
| 61 |
|
---|
[7a0bf79] | 62 | @Override
|
---|
[e0ef1b1] | 63 | public List<Person> findAllActors() {
|
---|
| 64 | return this.personRepository.findAllByType('A');
|
---|
[7a0bf79] | 65 | }
|
---|
| 66 |
|
---|
| 67 | @Override
|
---|
[e0ef1b1] | 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 |
|
---|
[7a0bf79] | 82 | }
|
---|
[2a5d6a3] | 83 |
|
---|
| 84 | @Override
|
---|
[e0ef1b1] | 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 |
|
---|
[2a5d6a3] | 111 | }
|
---|
| 112 |
|
---|
| 113 | @Override
|
---|
[e0ef1b1] | 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 |
|
---|
[2a5d6a3] | 121 | }
|
---|
| 122 |
|
---|
| 123 | @Override
|
---|
[e0ef1b1] | 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);
|
---|
[2a5d6a3] | 132 | }
|
---|
| 133 |
|
---|
| 134 | @Override
|
---|
[e0ef1b1] | 135 | public void deleteById(Integer id) {
|
---|
| 136 | this.personRepository.deleteById(id);
|
---|
[2a5d6a3] | 137 | }
|
---|
| 138 |
|
---|
| 139 | @Override
|
---|
[e0ef1b1] | 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 |
|
---|
[2a5d6a3] | 164 | }
|
---|
[e0ef1b1] | 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 |
|
---|
[7a0bf79] | 187 | }
|
---|