Changeset e0ef1b1 in Git for src/main/java/com/wediscussmovies/project/service/impl/PersonServiceImpl.java
- Timestamp:
- 02/04/22 23:22:13 (3 years ago)
- Branches:
- main
- Children:
- 5b447b0
- Parents:
- 3ded84d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/wediscussmovies/project/service/impl/PersonServiceImpl.java
r3ded84d re0ef1b1 1 1 package com.wediscussmovies.project.service.impl; 2 2 3 import com.wediscussmovies.project.model.*; 4 import com.wediscussmovies.project.model.enumerations.PersonType; 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; 5 16 import com.wediscussmovies.project.repository.PersonRepository; 17 import com.wediscussmovies.project.model.Person; 6 18 import com.wediscussmovies.project.service.PersonService; 7 19 import org.springframework.stereotype.Service; 8 20 21 import javax.transaction.Transactional; 22 import java.sql.Date; 9 23 import java.util.List; 10 24 import java.util.Optional; 25 import java.util.stream.Collectors; 11 26 12 27 @Service 13 28 public class PersonServiceImpl implements PersonService { 29 14 30 private final PersonRepository personRepository; 31 private final MovieRepository movieRepository; 32 private final MovieActorsRepository movieActorsRepository; 33 private final PersonRatesRepository personRatesRepository; 15 34 16 35 17 public PersonServiceImpl(PersonRepository personRepository) { 36 public PersonServiceImpl(PersonRepository personRepository, 37 MovieRepository movieRepository, MovieActorsRepository movieActorsRepository, 38 PersonRatesRepository personRatesRepository) { 18 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(); 19 48 } 20 49 21 50 @Override 22 51 public List<Person> findAllDirectors() { 23 return personRepository.findAllByType('D');52 return this.personRepository.findAllByType('D'); 24 53 } 25 54 26 55 @Override 27 public Optional<Person> findActorById(Integer id) { 28 return personRepository.findPersonByPersonIdAndType(id, 'A'); 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'); 29 65 } 30 66 31 67 @Override 32 public Optional<Person> findById(Integer person_id) { 33 return personRepository.findById(person_id); 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 34 82 } 35 83 36 84 @Override 37 public Optional<Person> findDirectorById(Integer id) { 38 return personRepository.findPersonByPersonIdAndType(id, 'D'); 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 39 111 } 40 112 41 113 @Override 42 public List<Person> findAllActors() { 43 return personRepository.findAllByType('A'); 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 44 121 } 45 122 46 123 @Override 47 public boolean save(Person person) { 48 Person p = personRepository.save(person); 49 if(p != null) 50 return true; 51 return false; 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); 52 132 } 53 133 54 134 @Override 55 public List<Person> findActorsByNameLike(String name) {56 return personRepository.findAllByTypeAndNameLike('A', name);135 public void deleteById(Integer id) { 136 this.personRepository.deleteById(id); 57 137 } 58 138 59 139 @Override 60 public List<Person> findActorsBySurnameLike(String surname) { 61 return personRepository.findAllByTypeAndSurnameLike('A', surname); 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 62 155 } 63 156 64 @Override 65 public List<Person> findDirectorsByNameLike(String name) { 66 return personRepository.findAllByTypeAndNameLike('D', name); 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 67 181 } 68 182 69 @Override 70 public List<Person> findDirectorsBySurnameLike(String surname) { 71 return personRepository.findAllByTypeAndSurnameLike('D', surname); 183 private Movie findMovieById(Integer id){ 184 return this.movieRepository.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id)); 72 185 } 186 73 187 }
Note:
See TracChangeset
for help on using the changeset viewer.