Changeset 3c0f9a9 in Git for src/main/java


Ignore:
Timestamp:
02/07/22 19:57:58 (3 years ago)
Author:
Petar Partaloski <ppartaloski@…>
Branches:
main
Children:
ad4243e, c02189f
Parents:
7f36551
Message:

Added genre liking, fixed counter, improved paging, improved searches

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  
    11package com.wediscussmovies.project.model.primarykeys;
     2
     3import lombok.Data;
    24
    35import javax.persistence.Column;
     
    68import java.io.Serializable;
    79
     10@Data
    811@Embeddable
    912public class UserGenresPK implements Serializable {
     
    3033        UserGenresPK that = (UserGenresPK) o;
    3134
    32 
    33 
    3435        return userId == that.userId && genreId == that.genreId;
    3536    }
  • src/main/java/com/wediscussmovies/project/querymodels/GenreLikes.java

    r7f36551 r3c0f9a9  
    33import lombok.Data;
    44
     5import javax.persistence.Entity;
     6import javax.persistence.Table;
     7import java.util.Comparator;
     8import java.util.Objects;
     9
    510@Data
    611public class GenreLikes {
    7 
     12    private Integer genreId;
    813    private String name;
    914    private Long likes;
    1015
    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;
    1220        this.name = name;
    1321        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);
    1436    }
    1537}
  • src/main/java/com/wediscussmovies/project/repository/GenreRepository.java

    r7f36551 r3c0f9a9  
    44import com.wediscussmovies.project.querymodels.GenreLikes;
    55import org.springframework.data.jpa.repository.JpaRepository;
     6import org.springframework.data.jpa.repository.Modifying;
    67import org.springframework.data.jpa.repository.Query;
     8import org.springframework.data.repository.query.Param;
    79import org.springframework.stereotype.Repository;
    810
    911import javax.transaction.Transactional;
     12import java.sql.Date;
    1013import java.util.List;
    1114
     
    1417     List<Genre> findAllByGenreType(String genre);
    1518
    16      @Query(value = "select new com.wediscussmovies.project.querymodels.GenreLikes(g.genreType, 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" +
    1720             " left join UserGenres ug on ug.id.genreId = g.genreId" +
    18              " group by g.genreType" +
     21             " group by g.genreId, g.genreType" +
    1922             " order by count(ug.id.userId) desc")
    2023     @Transactional
    2124     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);
    2230}
  • src/main/java/com/wediscussmovies/project/repository/MovieRepository.java

    r7f36551 r3c0f9a9  
    44import com.wediscussmovies.project.model.Person;
    55import org.springframework.data.jpa.repository.JpaRepository;
     6import org.springframework.data.jpa.repository.Query;
    67import org.springframework.stereotype.Repository;
    78
     
    1314     List<Movie> findAllByDirector(Person director);
    1415     List<Movie> findAllByDirectorIsNull();
     16     @Query(value="select m.movieId from Movie m")
     17     List<Integer> findAllMovieIds();
    1518}
  • src/main/java/com/wediscussmovies/project/service/GenreService.java

    r7f36551 r3c0f9a9  
    1414    Genre save(String genreName);
    1515    List<GenreLikes> findAllWithLikes();
     16    void likeGenre(Integer genreId,Integer userId);
     17    void unlikeGenre(Integer genreId,Integer userId);
    1618
    1719}
  • src/main/java/com/wediscussmovies/project/service/MovieService.java

    r7f36551 r3c0f9a9  
    66import com.wediscussmovies.project.model.Person;
    77import com.wediscussmovies.project.model.User;
     8import org.springframework.data.jpa.repository.Query;
    89
    910import java.sql.Date;
     
    1617     List<Movie> searchByTitle(String title);
    1718     Movie findById(Integer id);
     19     List<Integer> listAllIds();
    1820     Movie save(String title, String description, String imageUrl, Date airingDate,Double  rating,
    1921                Integer directorId,List<Integer> actorIds,List<Integer> genreIds);
  • src/main/java/com/wediscussmovies/project/service/PersonService.java

    r7f36551 r3c0f9a9  
    55import com.wediscussmovies.project.model.Person;
    66import com.wediscussmovies.project.model.User;
     7import com.wediscussmovies.project.model.relation.MovieLikes;
    78
    89import java.sql.Date;
    910import java.util.List;
     11import java.util.stream.Collectors;
    1012
    1113public interface PersonService {
     
    2729    void deleteById(Integer id);
    2830    void addGradePerson(Integer personId, User user, Grade grade);
    29 
    3031}
  • src/main/java/com/wediscussmovies/project/service/impl/GenreServiceImpl.java

    r7f36551 r3c0f9a9  
    11package com.wediscussmovies.project.service.impl;
    22
     3import com.wediscussmovies.project.model.User;
    34import com.wediscussmovies.project.model.exception.GenreNotExistException;
     5import com.wediscussmovies.project.model.exception.UserNotExistException;
     6import com.wediscussmovies.project.model.primarykeys.GenreLikesPK;
     7import com.wediscussmovies.project.model.primarykeys.UserGenresPK;
     8import com.wediscussmovies.project.model.relation.MovieLikes;
     9import com.wediscussmovies.project.model.relation.UserGenres;
    410import com.wediscussmovies.project.querymodels.GenreLikes;
     11import com.wediscussmovies.project.repository.GenreLikesRepository;
    512import com.wediscussmovies.project.repository.GenreRepository;
    613import com.wediscussmovies.project.model.Genre;
     14import com.wediscussmovies.project.repository.UserRepository;
    715import com.wediscussmovies.project.service.GenreService;
    816import org.springframework.stereotype.Service;
     
    1321public class GenreServiceImpl implements GenreService {
    1422    private final GenreRepository genreRepository;
     23    private final UserRepository userRepository;
     24    private final GenreLikesRepository genreLikesRepository;
    1525
    16     public GenreServiceImpl(GenreRepository genreRepository) {
     26
     27    public GenreServiceImpl(GenreRepository genreRepository, UserRepository userRepository, GenreLikesRepository genreLikesRepository) {
    1728        this.genreRepository = genreRepository;
     29        this.userRepository = userRepository;
     30        this.genreLikesRepository = genreLikesRepository;
    1831    }
    1932
     
    3649    @Override
    3750    public List<GenreLikes> findAllWithLikes() {
    38         return this.genreRepository.findAllWithLikes();
     51        List<GenreLikes> genreLikesList = this.genreRepository.findAllWithLikes();
     52        genreLikesList.sort(GenreLikes.sorter);
     53        return genreLikesList;
    3954    }
    4055
     
    4358        return this.genreRepository.findAll();
    4459    }
     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    }
    4573}
  • src/main/java/com/wediscussmovies/project/service/impl/MovieServiceImpl.java

    r7f36551 r3c0f9a9  
    7676
    7777    @Override
     78    public List<Integer> listAllIds() {
     79        return this.movieRepository.findAllMovieIds();
     80    }
     81
     82    @Override
    7883    @Transactional
    7984    public Movie save(String title, String description, String imageUrl,
  • src/main/java/com/wediscussmovies/project/service/impl/PersonServiceImpl.java

    r7f36551 r3c0f9a9  
    99import com.wediscussmovies.project.model.primarykeys.PersonRatesPK;
    1010import com.wediscussmovies.project.model.relation.MovieActors;
    11 import com.wediscussmovies.project.model.relation.MovieRates;
    1211import 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;
     12import com.wediscussmovies.project.repository.*;
    1713import com.wediscussmovies.project.model.Person;
    1814import com.wediscussmovies.project.service.PersonService;
     
    3228    private final MovieActorsRepository movieActorsRepository;
    3329    private final PersonRatesRepository personRatesRepository;
    34 
    3530
    3631    public PersonServiceImpl(PersonRepository personRepository,
     
    156151
    157152
     153
     154
    158155    private void addActorForMovies(Person person, List<Integer> movieIds){
    159156
  • src/main/java/com/wediscussmovies/project/web/controller/GenreController.java

    r7f36551 r3c0f9a9  
    11package com.wediscussmovies.project.web.controller;
    22
     3import com.wediscussmovies.project.LoggedUser;
     4import com.wediscussmovies.project.model.Genre;
     5import com.wediscussmovies.project.model.User;
     6import com.wediscussmovies.project.model.relation.UserGenres;
     7import com.wediscussmovies.project.querymodels.GenreLikes;
     8import com.wediscussmovies.project.repository.GenreLikesRepository;
    39import com.wediscussmovies.project.service.GenreService;
    410import org.springframework.stereotype.Controller;
     
    713import org.springframework.web.bind.annotation.RequestMapping;
    814
     15import java.util.ArrayList;
     16import java.util.List;
     17
    918@Controller
    1019@RequestMapping("/genres")
    1120public class GenreController {
    1221    private final GenreService genreService;
     22    private final GenreLikesRepository genreLikesRepository;
    1323
    14     public GenreController(GenreService genreService) {
     24    public GenreController(GenreService genreService, GenreLikesRepository genreLikesRepository) {
    1525        this.genreService = genreService;
     26        this.genreLikesRepository = genreLikesRepository;
    1627    }
    1728    @GetMapping
    1829    public String getGenres(Model model){
    1930        model.addAttribute("genres",genreService.findAllWithLikes());
     31        model.addAttribute("allGenres", genreService.findAll());
    2032        model.addAttribute("contentTemplate","genres");
     33        addModelPropertiesForUser(model);
    2134        return "template";
    2235    }
     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    }
    2347}
  • src/main/java/com/wediscussmovies/project/web/controller/MovieController.java

    r7f36551 r3c0f9a9  
    99import com.wediscussmovies.project.service.PersonService;
    1010import com.wediscussmovies.project.web.DesignFrontMovies;
     11import com.wediscussmovies.project.web.PageFrontMovies;
    1112import lombok.Data;
    1213import org.springframework.format.annotation.DateTimeFormat;
     
    3738    }
    3839
    39     @GetMapping
     40    @GetMapping("/old")
    4041    public String getMovies(@RequestParam(required = false) String titleQuery, Model model,
    4142                            @RequestParam(required = false) String error){
     
    5556        model.addAttribute("movie_rows", movie_rows);
    5657        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");
    5777        if (error != null && !error.equals(" "))
    5878            model.addAttribute("error",error);
     
    160180    }
    161181    private void addModelProperties(Model model){
    162 
    163         model.addAttribute("directors",  personService.findAllDirectors());
     182        model.addAttribute("directors", personService.findAllDirectors());
    164183        model.addAttribute("actors", personService.findAllActors());
    165184        model.addAttribute("genres", genreService.findAll());
     
    169188        model.addAttribute("likedMovies",this.movieService.findLikedMoviesByUser(user));
    170189        model.addAttribute("user",user);
    171         }
     190    }
    172191
    173192}
  • src/main/java/com/wediscussmovies/project/web/controller/PersonController.java

    r7f36551 r3c0f9a9  
    11package com.wediscussmovies.project.web.controller;
    22
     3import com.wediscussmovies.project.LoggedUser;
    34import com.wediscussmovies.project.model.Person;
     5import com.wediscussmovies.project.model.User;
    46import com.wediscussmovies.project.service.MovieService;
    57import com.wediscussmovies.project.service.PersonService;
     
    135137    }
    136138
     139    private void addModelPropertiesForUser(Model model){
     140        User user = LoggedUser.getLoggedUser();
     141        model.addAttribute("user",user);
     142    }
     143
    137144
    138145}
Note: See TracChangeset for help on using the changeset viewer.