Changeset 3c0f9a9 in Git for src/main/java/com/wediscussmovies/project
- Timestamp:
- 02/07/22 19:57:58 (3 years ago)
- Branches:
- main
- Children:
- ad4243e, c02189f
- Parents:
- 7f36551
- Location:
- src/main/java/com/wediscussmovies/project
- Files:
-
- 4 added
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/wediscussmovies/project/model/primarykeys/UserGenresPK.java
r7f36551 r3c0f9a9 1 1 package com.wediscussmovies.project.model.primarykeys; 2 3 import lombok.Data; 2 4 3 5 import javax.persistence.Column; … … 6 8 import java.io.Serializable; 7 9 10 @Data 8 11 @Embeddable 9 12 public class UserGenresPK implements Serializable { … … 30 33 UserGenresPK that = (UserGenresPK) o; 31 34 32 33 34 35 return userId == that.userId && genreId == that.genreId; 35 36 } -
src/main/java/com/wediscussmovies/project/querymodels/GenreLikes.java
r7f36551 r3c0f9a9 3 3 import lombok.Data; 4 4 5 import javax.persistence.Entity; 6 import javax.persistence.Table; 7 import java.util.Comparator; 8 import java.util.Objects; 9 5 10 @Data 6 11 public class GenreLikes { 7 12 private Integer genreId; 8 13 private String name; 9 14 private Long likes; 10 15 11 public GenreLikes(String name, Long likes) { 16 public static Comparator<GenreLikes> sorter = Comparator.comparing(GenreLikes::getLikes).thenComparing(GenreLikes::getName).reversed(); 17 18 public GenreLikes(Integer genreId, String name, Long likes) { 19 this.genreId = genreId; 12 20 this.name = name; 13 21 this.likes = likes; 22 23 } 24 25 @Override 26 public boolean equals(Object o) { 27 if (this == o) return true; 28 if (o == null || getClass() != o.getClass()) return false; 29 GenreLikes that = (GenreLikes) o; 30 return Objects.equals(genreId, that.genreId); 31 } 32 33 @Override 34 public int hashCode() { 35 return Objects.hash(genreId); 14 36 } 15 37 } -
src/main/java/com/wediscussmovies/project/repository/GenreRepository.java
r7f36551 r3c0f9a9 4 4 import com.wediscussmovies.project.querymodels.GenreLikes; 5 5 import org.springframework.data.jpa.repository.JpaRepository; 6 import org.springframework.data.jpa.repository.Modifying; 6 7 import org.springframework.data.jpa.repository.Query; 8 import org.springframework.data.repository.query.Param; 7 9 import org.springframework.stereotype.Repository; 8 10 9 11 import javax.transaction.Transactional; 12 import java.sql.Date; 10 13 import java.util.List; 11 14 … … 14 17 List<Genre> findAllByGenreType(String genre); 15 18 16 @Query(value = "select new com.wediscussmovies.project.querymodels.GenreLikes(g.genre Type, count(ug.id.userId)) from Genre g" +19 @Query(value = "select new com.wediscussmovies.project.querymodels.GenreLikes(g.genreId, g.genreType, count(ug.id.userId)) from Genre g" + 17 20 " left join UserGenres ug on ug.id.genreId = g.genreId" + 18 " group by g.genre Type" +21 " group by g.genreId, g.genreType" + 19 22 " order by count(ug.id.userId) desc") 20 23 @Transactional 21 24 List<GenreLikes> findAllWithLikes(); 25 26 @Modifying 27 @Query(value = "insert into project.user_genres (user_id,genre_id) values(:user_id,:genre_id)",nativeQuery = true) 28 @Transactional 29 void insertInto(@Param("user_id")Integer userId, @Param("genre_id")Integer genreId); 22 30 } -
src/main/java/com/wediscussmovies/project/repository/MovieRepository.java
r7f36551 r3c0f9a9 4 4 import com.wediscussmovies.project.model.Person; 5 5 import org.springframework.data.jpa.repository.JpaRepository; 6 import org.springframework.data.jpa.repository.Query; 6 7 import org.springframework.stereotype.Repository; 7 8 … … 13 14 List<Movie> findAllByDirector(Person director); 14 15 List<Movie> findAllByDirectorIsNull(); 16 @Query(value="select m.movieId from Movie m") 17 List<Integer> findAllMovieIds(); 15 18 } -
src/main/java/com/wediscussmovies/project/service/GenreService.java
r7f36551 r3c0f9a9 14 14 Genre save(String genreName); 15 15 List<GenreLikes> findAllWithLikes(); 16 void likeGenre(Integer genreId,Integer userId); 17 void unlikeGenre(Integer genreId,Integer userId); 16 18 17 19 } -
src/main/java/com/wediscussmovies/project/service/MovieService.java
r7f36551 r3c0f9a9 6 6 import com.wediscussmovies.project.model.Person; 7 7 import com.wediscussmovies.project.model.User; 8 import org.springframework.data.jpa.repository.Query; 8 9 9 10 import java.sql.Date; … … 16 17 List<Movie> searchByTitle(String title); 17 18 Movie findById(Integer id); 19 List<Integer> listAllIds(); 18 20 Movie save(String title, String description, String imageUrl, Date airingDate,Double rating, 19 21 Integer directorId,List<Integer> actorIds,List<Integer> genreIds); -
src/main/java/com/wediscussmovies/project/service/PersonService.java
r7f36551 r3c0f9a9 5 5 import com.wediscussmovies.project.model.Person; 6 6 import com.wediscussmovies.project.model.User; 7 import com.wediscussmovies.project.model.relation.MovieLikes; 7 8 8 9 import java.sql.Date; 9 10 import java.util.List; 11 import java.util.stream.Collectors; 10 12 11 13 public interface PersonService { … … 27 29 void deleteById(Integer id); 28 30 void addGradePerson(Integer personId, User user, Grade grade); 29 30 31 } -
src/main/java/com/wediscussmovies/project/service/impl/GenreServiceImpl.java
r7f36551 r3c0f9a9 1 1 package com.wediscussmovies.project.service.impl; 2 2 3 import com.wediscussmovies.project.model.User; 3 4 import com.wediscussmovies.project.model.exception.GenreNotExistException; 5 import com.wediscussmovies.project.model.exception.UserNotExistException; 6 import com.wediscussmovies.project.model.primarykeys.GenreLikesPK; 7 import com.wediscussmovies.project.model.primarykeys.UserGenresPK; 8 import com.wediscussmovies.project.model.relation.MovieLikes; 9 import com.wediscussmovies.project.model.relation.UserGenres; 4 10 import com.wediscussmovies.project.querymodels.GenreLikes; 11 import com.wediscussmovies.project.repository.GenreLikesRepository; 5 12 import com.wediscussmovies.project.repository.GenreRepository; 6 13 import com.wediscussmovies.project.model.Genre; 14 import com.wediscussmovies.project.repository.UserRepository; 7 15 import com.wediscussmovies.project.service.GenreService; 8 16 import org.springframework.stereotype.Service; … … 13 21 public class GenreServiceImpl implements GenreService { 14 22 private final GenreRepository genreRepository; 23 private final UserRepository userRepository; 24 private final GenreLikesRepository genreLikesRepository; 15 25 16 public GenreServiceImpl(GenreRepository genreRepository) { 26 27 public GenreServiceImpl(GenreRepository genreRepository, UserRepository userRepository, GenreLikesRepository genreLikesRepository) { 17 28 this.genreRepository = genreRepository; 29 this.userRepository = userRepository; 30 this.genreLikesRepository = genreLikesRepository; 18 31 } 19 32 … … 36 49 @Override 37 50 public List<GenreLikes> findAllWithLikes() { 38 return this.genreRepository.findAllWithLikes(); 51 List<GenreLikes> genreLikesList = this.genreRepository.findAllWithLikes(); 52 genreLikesList.sort(GenreLikes.sorter); 53 return genreLikesList; 39 54 } 40 55 … … 43 58 return this.genreRepository.findAll(); 44 59 } 60 61 @Override 62 public void likeGenre(Integer genreId, Integer userId) { 63 User user = this.userRepository.findById(userId).orElseThrow(() -> new UserNotExistException(userId.toString())); 64 Genre genre = this.genreRepository.findById(genreId).orElseThrow(() -> new GenreNotExistException(genreId)); 65 this.genreLikesRepository.save(new UserGenres(genre, user)); 66 } 67 68 @Override 69 public void unlikeGenre(Integer genreId, Integer userId) { 70 UserGenresPK movieLikesPK = new UserGenresPK(userId, genreId); 71 this.genreLikesRepository.deleteById(movieLikesPK); 72 } 45 73 } -
src/main/java/com/wediscussmovies/project/service/impl/MovieServiceImpl.java
r7f36551 r3c0f9a9 76 76 77 77 @Override 78 public List<Integer> listAllIds() { 79 return this.movieRepository.findAllMovieIds(); 80 } 81 82 @Override 78 83 @Transactional 79 84 public Movie save(String title, String description, String imageUrl, -
src/main/java/com/wediscussmovies/project/service/impl/PersonServiceImpl.java
r7f36551 r3c0f9a9 9 9 import com.wediscussmovies.project.model.primarykeys.PersonRatesPK; 10 10 import com.wediscussmovies.project.model.relation.MovieActors; 11 import com.wediscussmovies.project.model.relation.MovieRates;12 11 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; 12 import com.wediscussmovies.project.repository.*; 17 13 import com.wediscussmovies.project.model.Person; 18 14 import com.wediscussmovies.project.service.PersonService; … … 32 28 private final MovieActorsRepository movieActorsRepository; 33 29 private final PersonRatesRepository personRatesRepository; 34 35 30 36 31 public PersonServiceImpl(PersonRepository personRepository, … … 156 151 157 152 153 154 158 155 private void addActorForMovies(Person person, List<Integer> movieIds){ 159 156 -
src/main/java/com/wediscussmovies/project/web/controller/GenreController.java
r7f36551 r3c0f9a9 1 1 package com.wediscussmovies.project.web.controller; 2 2 3 import com.wediscussmovies.project.LoggedUser; 4 import com.wediscussmovies.project.model.Genre; 5 import com.wediscussmovies.project.model.User; 6 import com.wediscussmovies.project.model.relation.UserGenres; 7 import com.wediscussmovies.project.querymodels.GenreLikes; 8 import com.wediscussmovies.project.repository.GenreLikesRepository; 3 9 import com.wediscussmovies.project.service.GenreService; 4 10 import org.springframework.stereotype.Controller; … … 7 13 import org.springframework.web.bind.annotation.RequestMapping; 8 14 15 import java.util.ArrayList; 16 import java.util.List; 17 9 18 @Controller 10 19 @RequestMapping("/genres") 11 20 public class GenreController { 12 21 private final GenreService genreService; 22 private final GenreLikesRepository genreLikesRepository; 13 23 14 public GenreController(GenreService genreService ) {24 public GenreController(GenreService genreService, GenreLikesRepository genreLikesRepository) { 15 25 this.genreService = genreService; 26 this.genreLikesRepository = genreLikesRepository; 16 27 } 17 28 @GetMapping 18 29 public String getGenres(Model model){ 19 30 model.addAttribute("genres",genreService.findAllWithLikes()); 31 model.addAttribute("allGenres", genreService.findAll()); 20 32 model.addAttribute("contentTemplate","genres"); 33 addModelPropertiesForUser(model); 21 34 return "template"; 22 35 } 36 37 private void addModelPropertiesForUser(Model model){ 38 User user = LoggedUser.getLoggedUser(); 39 List<UserGenres> genreLikesList = this.genreLikesRepository.findAllByUser(user); 40 List<Genre> genres = new ArrayList<>(); 41 for(UserGenres g: genreLikesList){ 42 genres.add(genreService.findById(g.getId().getGenreId())); 43 } 44 model.addAttribute("likedGenres",genres); 45 model.addAttribute("user",user); 46 } 23 47 } -
src/main/java/com/wediscussmovies/project/web/controller/MovieController.java
r7f36551 r3c0f9a9 9 9 import com.wediscussmovies.project.service.PersonService; 10 10 import com.wediscussmovies.project.web.DesignFrontMovies; 11 import com.wediscussmovies.project.web.PageFrontMovies; 11 12 import lombok.Data; 12 13 import org.springframework.format.annotation.DateTimeFormat; … … 37 38 } 38 39 39 @GetMapping 40 @GetMapping("/old") 40 41 public String getMovies(@RequestParam(required = false) String titleQuery, Model model, 41 42 @RequestParam(required = false) String error){ … … 55 56 model.addAttribute("movie_rows", movie_rows); 56 57 model.addAttribute("contentTemplate", "moviesList"); 58 if (error != null && !error.equals(" ")) 59 model.addAttribute("error",error); 60 return "template"; 61 } 62 63 64 @GetMapping 65 public String getMoviesAlternative(@RequestParam(required = false) String titleQuery, Model model, 66 @RequestParam(required = false) String error, @RequestParam(required = false) String page){ 67 if (page==null){ 68 return "redirect:/movies?page=1"; 69 } 70 addModelPropertiesForUser(model); 71 List<Movie> movies = PageFrontMovies.getPagedMovies(page, movieService, model); 72 List<List<Movie>> movie_rows = new ArrayList<>(); 73 DesignFrontMovies.designMovieList(movies,movie_rows); 74 model.addAttribute("movies", movies); 75 model.addAttribute("movie_rows", movie_rows); 76 model.addAttribute("contentTemplate", "moviesListPaged"); 57 77 if (error != null && !error.equals(" ")) 58 78 model.addAttribute("error",error); … … 160 180 } 161 181 private void addModelProperties(Model model){ 162 163 model.addAttribute("directors", personService.findAllDirectors()); 182 model.addAttribute("directors", personService.findAllDirectors()); 164 183 model.addAttribute("actors", personService.findAllActors()); 165 184 model.addAttribute("genres", genreService.findAll()); … … 169 188 model.addAttribute("likedMovies",this.movieService.findLikedMoviesByUser(user)); 170 189 model.addAttribute("user",user); 171 190 } 172 191 173 192 } -
src/main/java/com/wediscussmovies/project/web/controller/PersonController.java
r7f36551 r3c0f9a9 1 1 package com.wediscussmovies.project.web.controller; 2 2 3 import com.wediscussmovies.project.LoggedUser; 3 4 import com.wediscussmovies.project.model.Person; 5 import com.wediscussmovies.project.model.User; 4 6 import com.wediscussmovies.project.service.MovieService; 5 7 import com.wediscussmovies.project.service.PersonService; … … 135 137 } 136 138 139 private void addModelPropertiesForUser(Model model){ 140 User user = LoggedUser.getLoggedUser(); 141 model.addAttribute("user",user); 142 } 143 137 144 138 145 }
Note:
See TracChangeset
for help on using the changeset viewer.