Changeset e0ef1b1 in Git for src/main/java/com/wediscussmovies/project/service/impl/MovieServiceImpl.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/MovieServiceImpl.java
r3ded84d re0ef1b1 1 1 package com.wediscussmovies.project.service.impl; 2 2 3 4 import com.wediscussmovies.project.ajaxmodels.Grade; 3 5 import com.wediscussmovies.project.model.*; 4 import com.wediscussmovies.project.repository.MovieRepository; 6 import com.wediscussmovies.project.model.exception.GenreNotExistException; 7 import com.wediscussmovies.project.model.exception.PersonNotExistException; 8 import com.wediscussmovies.project.model.exception.UserNotExistException; 9 import com.wediscussmovies.project.model.primarykeys.MovieLikesPK; 10 import com.wediscussmovies.project.model.primarykeys.MovieRatesPK; 11 import com.wediscussmovies.project.model.relation.MovieActors; 12 import com.wediscussmovies.project.model.relation.MovieGenres; 13 import com.wediscussmovies.project.model.relation.MovieLikes; 14 import com.wediscussmovies.project.model.relation.MovieRates; 15 import com.wediscussmovies.project.repository.*; 16 import com.wediscussmovies.project.model.exception.MovieIdNotFoundException; 5 17 import com.wediscussmovies.project.service.MovieService; 6 18 import org.springframework.stereotype.Service; 7 19 20 import javax.swing.text.html.Option; 21 import javax.transaction.Transactional; 22 import java.sql.Date; 8 23 import java.util.List; 9 24 import java.util.Optional; 25 import java.util.stream.Collectors; 10 26 11 27 @Service 12 28 public class MovieServiceImpl implements MovieService { 29 13 30 private final MovieRepository movieRepository; 14 15 public MovieServiceImpl(MovieRepository movieRepository) { 31 private final PersonRepository personRepository; 32 private final GenreRepository genreRepository; 33 private final MovieActorsRepository movieActorsRepository; 34 private final MovieGenresRepository movieGenresRepository; 35 private final UserRepository userRepository; 36 private final MovieLikesRepository movieLikesRepository; 37 private final MovieRatesRepository movieRatesRepository; 38 39 public MovieServiceImpl(MovieRepository movieRepository, PersonRepository personRepository, 40 GenreRepository genreRepository, MovieActorsRepository movieActorsRepository, 41 MovieGenresRepository movieGenresRepository, 42 UserRepository userRepository, MovieLikesRepository movieLikesRepository, 43 MovieRatesRepository movieRatesRepository) { 44 16 45 this.movieRepository = movieRepository; 46 this.personRepository = personRepository; 47 this.genreRepository = genreRepository; 48 this.movieActorsRepository = movieActorsRepository; 49 this.movieGenresRepository = movieGenresRepository; 50 this.userRepository = userRepository; 51 this.movieLikesRepository = movieLikesRepository; 52 this.movieRatesRepository = movieRatesRepository; 17 53 } 18 54 19 55 @Override 20 56 public List<Movie> listAll() { 21 return movieRepository.findAll(); 22 } 23 24 @Override 25 public Optional<Movie> findById(Integer id) { 26 return movieRepository.findById(1); 27 } 28 29 @Override 30 public Movie save(Movie movie) { 31 return movieRepository.save(movie); 32 } 57 return this.movieRepository.findAll(); 58 } 59 60 @Override 61 public List<Movie> listAllByType(Character type) { 62 if (type.equals('A')) 63 return this.listAll(); 64 return this.listAllWithoutDirector(); 65 } 66 67 @Override 68 public List<Movie> listAllWithoutDirector() { 69 return this.movieRepository.findAllByDirectorIsNull(); 70 } 71 72 @Override 73 public Movie findById(Integer id) { 74 return this.movieRepository.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id)); 75 } 76 77 @Override 78 @Transactional 79 public Movie save(String title, String description, String imageUrl, 80 Date airingDate, Double rating, Integer directorId,List<Integer> actorIds,List<Integer> genreIds) { 81 82 83 Person director =getDirector(directorId); 84 Movie movie = new Movie(title,description,imageUrl,airingDate,rating,director); 85 movie = this.movieRepository.saveAndFlush(movie); 86 addActorsAndGenresForMovie(movie,actorIds,genreIds); 87 return movie; 88 } 89 90 91 @Transactional 92 @Override 93 public Movie edit(Integer movieId, String title, String description, 94 String imageUrl, Date airingDate, Double rating, 95 Integer directorId, List<Integer> actorIds,List<Integer> genreIds) { 96 97 98 Person director =getDirector(directorId); 99 Movie movie = this.findById(movieId); 100 101 movie.setTitle(title); 102 movie.setDescription(description); 103 movie.setImageUrl(imageUrl); 104 movie.setAiringDate(airingDate); 105 movie.setImdbRating(rating); 106 movie.setDirector(director); 107 108 this.movieActorsRepository.deleteAllByMovie(movie); 109 this.movieGenresRepository.deleteAllByMovie(movie); 110 111 movie = this.movieRepository.save(movie); 112 this.addActorsAndGenresForMovie(movie,actorIds,genreIds); 113 114 return movie; 115 } 116 117 @Override 118 public List<Person> findAllActorsForMovie(Movie movie) { 119 return this.movieActorsRepository.findAllByMovie(movie) 120 .stream() 121 .map(MovieActors::getPerson) 122 .collect(Collectors.toList()); 123 } 124 125 @Override 126 public List<Genre> findAllGenresForMovie(Movie movie) { 127 return this.movieGenresRepository.findAllByMovie(movie) 128 .stream() 129 .map(MovieGenres::getGenre) 130 .collect(Collectors.toList()); 131 } 132 133 @Override 134 public void likeMovie(Integer movieId, Integer userId) { 135 User user = this.userRepository.findById(userId).orElseThrow(() -> new UserNotExistException(userId.toString())); 136 Movie movie = this.findById(movieId); 137 138 this.movieLikesRepository.save(new MovieLikes(movie, user)); 139 } 140 141 @Override 142 public void unlikeMovie(Integer movieId, Integer userId) { 143 MovieLikesPK movieLikesPK = new MovieLikesPK(movieId,userId); 144 this.movieLikesRepository.deleteById(movieLikesPK); 145 } 146 147 @Override 148 public List<Movie> findLikedMoviesByUser(User user) { 149 return 150 this.movieLikesRepository 151 .findAllByUser(user) 152 .stream() 153 .map(MovieLikes::getMovie) 154 .collect(Collectors.toList()); 155 } 156 33 157 34 158 @Override 35 159 public void deleteById(Integer id) { 36 movieRepository.deleteById(id); 160 this.movieRepository.deleteById(id); 161 } 162 163 @Override 164 public void addGradeMovie(Integer movieId, User user, Grade grade) { 165 MovieRatesPK key = new MovieRatesPK(movieId, user.getUserId()); 166 Optional<MovieRates> movieRates = this.movieRatesRepository.findById(key); 167 if (movieRates.isPresent()){ 168 MovieRates rates = movieRates.get(); 169 rates.setReason(grade.getReason()); 170 rates.setStarsRated(grade.getRating()); 171 this.movieRatesRepository.save(rates); 172 } 173 else{ 174 Movie movie = this.findById(movieId); 175 MovieRates rates = new MovieRates(user,movie,grade.getReason(),grade.getRating()); 176 this.movieRatesRepository.save(rates); 177 } 37 178 } 38 179 39 180 @Override 40 181 public List<Movie> searchByTitle(String title) { 41 return movieRepository.findAllByTitleLike("%"+title+"%"); 182 return this.movieRepository.findAllByTitleLike("%"+title+"%"); 183 } 184 185 @Transactional 186 void addActorsAndGenresForMovie(Movie movie, List<Integer> actorIds, List<Integer> genreIds) 187 { 188 189 actorIds.stream() 190 .map(this::findPersonById) 191 .forEach(actor -> this.movieActorsRepository.save(new MovieActors(movie,actor))); 192 193 genreIds.stream() 194 .map(this::findGenreById) 195 .forEach(genre -> this.movieGenresRepository.save(new MovieGenres(movie,genre))); 196 197 } 198 private Person getDirector(Integer directorId){ 199 200 if (directorId != null) 201 return this.findPersonById(directorId); 202 return null; 203 204 } 205 private Person findPersonById(Integer id){ 206 return this.personRepository.findById(id).orElseThrow(() -> new PersonNotExistException(id)); 207 } 208 private Genre findGenreById(Integer id){ 209 return this.genreRepository.findById(id).orElseThrow(() -> new GenreNotExistException(id)); 42 210 } 43 211 }
Note:
See TracChangeset
for help on using the changeset viewer.