Changeset c02189f in Git for src/main/java/com


Ignore:
Timestamp:
02/08/22 20:13:23 (3 years ago)
Author:
Petar Partaloski <ppartaloski@…>
Branches:
main
Children:
0226942, 3fe36de
Parents:
3c0f9a9
Message:

Added new core functionalities, fixed bugs and improved visual clarity

Location:
src/main/java/com/wediscussmovies/project
Files:
5 added
17 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/wediscussmovies/project/configuration/SecurityConfig.java

    r3c0f9a9 rc02189f  
    2929        http.csrf().disable()
    3030                .authorizeRequests()
    31                 .antMatchers("/movies","/movies/**/","/actors","/persons/**/","/directors","/discussions","/discussions/**/","/discussions/all/**/","/replies","/register","/genres", "/css/**", "/js/**").permitAll()
     31                .antMatchers("/movies","/movies/**/","/actors","/persons/**/","/directors","/discussions","/discussions/**/","/discussions/all/**/","/replies","/register","/genres", "/css/**","/img/**", "/js/**").permitAll()
    3232                .anyRequest()
    3333                .authenticated()
  • src/main/java/com/wediscussmovies/project/model/Genre.java

    r3c0f9a9 rc02189f  
    2222    }
    2323
     24    public Genre(int genreId, String genreType) {
     25        this.genreId = genreId;
     26        this.genreType = genreType;
     27    }
     28
    2429    public Genre(String genreType) {
    2530        this.genreType = genreType;
  • src/main/java/com/wediscussmovies/project/model/Movie.java

    r3c0f9a9 rc02189f  
    6666    }
    6767
     68    public Movie(int movieId, String title, Double imdbRating, String imageUrl) {
     69        this.movieId = movieId;
     70        this.title = title;
     71        this.imdbRating = imdbRating;
     72        this.imageUrl = imageUrl;
     73    }
     74
     75    public String getDateFormatted(){
     76        String dob = airingDate.toString();
     77        String [] parts = dob.split("-");
     78        return parts[2]+"/"+parts[1]+"/"+parts[0];
     79    }
     80
    6881    @Override
    6982    public boolean equals(Object o) {
  • src/main/java/com/wediscussmovies/project/model/Person.java

    r3c0f9a9 rc02189f  
    66
    77import javax.persistence.*;
     8import javax.swing.text.DateFormatter;
    89import java.sql.Date;
     10import java.time.LocalDate;
     11import java.time.format.DateTimeFormatter;
    912import java.util.Collection;
    1013
     
    7376
    7477
     78    public String getDateFormatted(){
     79        String dob = dateOfBirth.toString();
     80        String [] parts = dob.split("-");
     81        return parts[2]+"/"+parts[1]+"/"+parts[0];
     82    }
     83
    7584
    7685    public Person(String name, String surname, Character type, Date dateOfBirth, String imageUrl, String description) {
  • src/main/java/com/wediscussmovies/project/repository/DiscussionRepository.java

    r3c0f9a9 rc02189f  
    44import com.wediscussmovies.project.model.Movie;
    55import com.wediscussmovies.project.model.Person;
     6import com.wediscussmovies.project.querymodels.DiscussionLikes;
     7import com.wediscussmovies.project.querymodels.GenreLikes;
    68import org.springframework.data.jpa.repository.JpaRepository;
     9import org.springframework.data.jpa.repository.Query;
    710import org.springframework.stereotype.Repository;
    811
     12import javax.transaction.Transactional;
    913import java.util.List;
    1014
     
    1418     List<Discussion> findAllByMovie(Movie movie);
    1519     List<Discussion> findAllByPerson (Person person);
     20
     21     @Query(value = "select new com.wediscussmovies.project.querymodels.DiscussionLikes(d.discussionId, count(dl.id.userId)) from Discussion d" +
     22             " left join DiscussionLikes dl on dl.id.discussionId = d.discussionId" +
     23             " group by d.discussionId" +
     24             " order by count(dl.id.userId) desc")
     25     @Transactional
     26     List<DiscussionLikes> findAllWithLikes();
    1627}
  • src/main/java/com/wediscussmovies/project/repository/GenreRepository.java

    r3c0f9a9 rc02189f  
    2828     @Transactional
    2929     void insertInto(@Param("user_id")Integer userId, @Param("genre_id")Integer genreId);
     30
     31     @Query(value = "select new com.wediscussmovies.project.model.Genre (g.genreId, g.genreType) from Genre g" +
     32     " ORDER BY g.genreType DESC")
     33     List<Genre> findAllSorted();
    3034}
  • src/main/java/com/wediscussmovies/project/repository/MovieRepository.java

    r3c0f9a9 rc02189f  
    33import com.wediscussmovies.project.model.Movie;
    44import com.wediscussmovies.project.model.Person;
     5import com.wediscussmovies.project.querymodels.GenreLikes;
     6import com.wediscussmovies.project.querymodels.MovieLikesQM;
    57import org.springframework.data.jpa.repository.JpaRepository;
    68import org.springframework.data.jpa.repository.Query;
     9import org.springframework.data.repository.query.Param;
    710import org.springframework.stereotype.Repository;
    811
     12import javax.transaction.Transactional;
    913import java.util.List;
     14import java.util.Optional;
    1015
    1116@Repository
     
    1621     @Query(value="select m.movieId from Movie m")
    1722     List<Integer> findAllMovieIds();
     23
     24     @Query(value="select new com.wediscussmovies.project.model.Movie(m.movieId, m.title, m.imdbRating, m.imageUrl) from Movie m" +
     25               " where m.movieId = :index")
     26     Optional<Movie> findBasicById(@Param("index")Integer index);
     27
     28
     29     @Query(value = "select new com.wediscussmovies.project.querymodels.MovieLikesQM(m.movieId, count(ml.id.userId)) from Movie m" +
     30             " left join MovieLikes ml on ml.id.movieId = m.movieId" +
     31             " group by m.movieId" +
     32             " order by count(ml.id.userId) desc")
     33     @Transactional
     34     List<MovieLikesQM> findAllWithLikes();
     35
     36     @Query(value = "select new com.wediscussmovies.project.querymodels.MovieLikesQM(m.movieId, count(ml.id.userId)) from Movie m" +
     37             " left join MovieLikes ml on ml.id.movieId = m.movieId" +
     38             " group by m.movieId" +
     39             " having m.movieId = :id")
     40     @Transactional
     41     List<MovieLikesQM> findLikesForMovie(@Param("id") Integer movie_id);
     42
     43
    1844}
  • src/main/java/com/wediscussmovies/project/service/DiscussionService.java

    r3c0f9a9 rc02189f  
    33import com.wediscussmovies.project.model.Discussion;
    44import com.wediscussmovies.project.model.User;
     5import com.wediscussmovies.project.querymodels.DiscussionLikes;
    56
    67import java.util.List;
     
    1617    void unlikeDiscussion(Integer discussionId,Integer userId);
    1718    List<Discussion> findAllForPersonOrMovie(Integer id,Character type);
     19
     20    List<Discussion> findLikedDiscussionsByUser(User user);
     21
     22    DiscussionLikes findLikesForDiscussionWithId(int discussionId);
     23
    1824}
  • src/main/java/com/wediscussmovies/project/service/MovieService.java

    r3c0f9a9 rc02189f  
    66import com.wediscussmovies.project.model.Person;
    77import com.wediscussmovies.project.model.User;
     8import com.wediscussmovies.project.querymodels.MovieLikesQM;
    89import org.springframework.data.jpa.repository.Query;
    910
     
    1718     List<Movie> searchByTitle(String title);
    1819     Movie findById(Integer id);
     20     Movie findBasicById(Integer id);
    1921     List<Integer> listAllIds();
    2022     Movie save(String title, String description, String imageUrl, Date airingDate,Double  rating,
     
    3537
    3638     void addGradeMovie(Integer movieId, User user, Grade grade);
     39
     40    MovieLikesQM findLikesForMovieById(int movieId);
    3741}
  • src/main/java/com/wediscussmovies/project/service/impl/DiscussionServiceImpl.java

    r3c0f9a9 rc02189f  
    55import com.wediscussmovies.project.model.exception.PersonNotExistException;
    66import com.wediscussmovies.project.model.exception.UserNotExistException;
    7 import com.wediscussmovies.project.repository.DiscussionRepository;
     7import com.wediscussmovies.project.model.primarykeys.DiscussionLikesPK;
     8import com.wediscussmovies.project.model.relation.DiscussionLikes;
     9import com.wediscussmovies.project.repository.*;
    810import com.wediscussmovies.project.model.Discussion;
    911import com.wediscussmovies.project.model.Movie;
    1012import com.wediscussmovies.project.model.Person;
    1113import com.wediscussmovies.project.model.User;
    12 import com.wediscussmovies.project.repository.MovieRepository;
    13 import com.wediscussmovies.project.repository.PersonRepository;
    14 import com.wediscussmovies.project.repository.UserRepository;
    1514import com.wediscussmovies.project.service.DiscussionService;
    16 import com.wediscussmovies.project.service.MovieService;
    17 import com.wediscussmovies.project.service.PersonService;
    18 import com.wediscussmovies.project.service.UserService;
    1915import org.springframework.stereotype.Service;
    2016
    2117import java.sql.Date;
    2218import java.time.LocalDate;
     19import java.util.ArrayList;
    2320import java.util.List;
    2421
     
    2724
    2825    private final DiscussionRepository discussionRepository;
     26    private final DiscussionLikesRepository discussionLikesRepository;
    2927    private final UserRepository userRepository;
    3028
     
    3230    private final PersonRepository personRepository;
    3331
    34     public DiscussionServiceImpl(DiscussionRepository discussionRepository, UserRepository userRepository,
     32    public DiscussionServiceImpl(DiscussionRepository discussionRepository, DiscussionLikesRepository discussionLikesRepository, UserRepository userRepository,
    3533                                 MovieRepository movieRepository, PersonRepository personRepository) {
    3634        this.discussionRepository = discussionRepository;
     35        this.discussionLikesRepository = discussionLikesRepository;
    3736        this.userRepository = userRepository;
    3837        this.movieRepository = movieRepository;
     
    9493    @Override
    9594    public void likeDiscussion(Integer discussionId, Integer userId) {
    96         //ova fali od dijagram
     95        Discussion discussion = discussionRepository.findById(discussionId).orElseThrow(() -> new DiscussionNotExistException(discussionId));
     96        User user = userRepository.findById(userId).orElseThrow(() -> new UserNotExistException(userId.toString()));
     97        this.discussionLikesRepository.save(new DiscussionLikes(discussion, user));
     98    }
     99
     100    @Override
     101    public List<Discussion> findLikedDiscussionsByUser(User user) {
     102        List<DiscussionLikes> likes = discussionLikesRepository.findAllByUser(user);
     103        List<Discussion> discussions = new ArrayList<>();
     104        for(DiscussionLikes dl: likes){
     105            discussions.add(dl.getDiscussion());
     106        }
     107        return discussions;
    97108    }
    98109
    99110    @Override
    100111    public void unlikeDiscussion(Integer discussionId, Integer userId) {
    101         // ova fali od dijagram
     112        DiscussionLikesPK pk = new DiscussionLikesPK(discussionId, userId);
     113        this.discussionLikesRepository.deleteById(pk);
    102114    }
     115
     116    @Override
     117    public com.wediscussmovies.project.querymodels.DiscussionLikes findLikesForDiscussionWithId(int discussionId) {
     118        return discussionRepository.findAllWithLikes().stream().filter(d ->  d.getDiscussionId().equals(discussionId)).findFirst().get();
     119    }
     120
    103121
    104122    @Override
  • src/main/java/com/wediscussmovies/project/service/impl/GenreServiceImpl.java

    r3c0f9a9 rc02189f  
    5656    @Override
    5757    public List<Genre> findAll() {
    58         return this.genreRepository.findAll();
     58        return this.genreRepository.findAllSorted();
    5959    }
    6060
  • src/main/java/com/wediscussmovies/project/service/impl/MovieServiceImpl.java

    r3c0f9a9 rc02189f  
    1313import com.wediscussmovies.project.model.relation.MovieLikes;
    1414import com.wediscussmovies.project.model.relation.MovieRates;
     15import com.wediscussmovies.project.querymodels.MovieLikesQM;
    1516import com.wediscussmovies.project.repository.*;
    1617import com.wediscussmovies.project.model.exception.MovieIdNotFoundException;
     
    7374    public Movie findById(Integer id) {
    7475        return this.movieRepository.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id));
     76    }
     77
     78    @Override
     79    public Movie findBasicById(Integer id) {
     80        return this.movieRepository.findBasicById(id).orElseThrow(() -> new MovieIdNotFoundException(id));
     81    }
     82
     83    @Override
     84    public MovieLikesQM findLikesForMovieById(int movieId) {
     85        return this.movieRepository.findLikesForMovie(movieId).get(0);
    7586    }
    7687
  • src/main/java/com/wediscussmovies/project/service/impl/PersonServiceImpl.java

    r3c0f9a9 rc02189f  
    99import com.wediscussmovies.project.model.primarykeys.PersonRatesPK;
    1010import com.wediscussmovies.project.model.relation.MovieActors;
     11import com.wediscussmovies.project.model.relation.MovieLikes;
    1112import com.wediscussmovies.project.model.relation.PersonRates;
    1213import com.wediscussmovies.project.repository.*;
     
    2829    private final MovieActorsRepository movieActorsRepository;
    2930    private final PersonRatesRepository personRatesRepository;
    30 
    3131    public PersonServiceImpl(PersonRepository personRepository,
    3232                             MovieRepository movieRepository, MovieActorsRepository movieActorsRepository,
     
    152152
    153153
    154 
    155154    private void addActorForMovies(Person person, List<Integer> movieIds){
    156155
  • src/main/java/com/wediscussmovies/project/web/PageFrontMovies.java

    r3c0f9a9 rc02189f  
    3232
    3333        for(Integer id: movieIds){
    34             movies.add(movieService.findById(id));
     34            //Trying to improve load times by not pulling all things from the database, excluding un-needed stuff
     35            //
     36            //movies.add(movieService.findById(id));
     37            movies.add(movieService.findBasicById(id));
    3538        }
    3639        model.addAttribute("page", page);
  • src/main/java/com/wediscussmovies/project/web/controller/DiscussionController.java

    r3c0f9a9 rc02189f  
    55import com.wediscussmovies.project.model.Reply;
    66import com.wediscussmovies.project.model.User;
     7import com.wediscussmovies.project.model.relation.DiscussionLikes;
    78import com.wediscussmovies.project.service.DiscussionService;
    89import com.wediscussmovies.project.service.MovieService;
     
    1718import org.springframework.web.bind.annotation.*;
    1819
     20import java.util.ArrayList;
    1921import java.util.List;
    2022
     
    4648        model.addAttribute("contentTemplate", "discussionsList");
    4749        model.addAttribute("user",LoggedUser.getLoggedUser());
     50        this.addModelPropertiesForUser(model);
     51        this.addModelPropertiesLikes(model,null, discussions);
    4852        return "template";
    4953    }
     
    6266            model.addAttribute("user",LoggedUser.getLoggedUser());
    6367            model.addAttribute("replies",this.replyService.findAllByDiscussion(disc));
     68            addModelPropertiesLikes(model, disc, null);
     69            addModelPropertiesForUser(model);
    6470
    6571            return "template";
     
    171177
    172178    }
     179    private void addModelPropertiesForUser(Model model){
     180        User user = LoggedUser.getLoggedUser();
     181        model.addAttribute("likedDiscussions",this.discussionService.findLikedDiscussionsByUser(user));
     182        model.addAttribute("user",user);
     183    }
     184    private void addModelPropertiesLikes(Model model, Discussion discussion, List<Discussion> discussions){
     185        if(discussion==null){
     186            List<com.wediscussmovies.project.querymodels.DiscussionLikes> discussionLikes = new ArrayList<>();
     187            for(Discussion d: discussions){
     188                discussionLikes.add(discussionService.findLikesForDiscussionWithId(d.getDiscussionId()));
     189            }
     190            model.addAttribute("likes", discussionLikes);
     191        }
     192        else{
     193            model.addAttribute("likes",discussionService.findLikesForDiscussionWithId(discussion.getDiscussionId()).getLikes());
     194        }
     195    }
    173196
    174197
  • src/main/java/com/wediscussmovies/project/web/controller/MovieController.java

    r3c0f9a9 rc02189f  
    55import com.wediscussmovies.project.model.Person;
    66import com.wediscussmovies.project.model.User;
     7import com.wediscussmovies.project.querymodels.MovieLikesQM;
    78import com.wediscussmovies.project.service.GenreService;
    89import com.wediscussmovies.project.service.MovieService;
     
    2324import java.time.LocalDate;
    2425import java.util.ArrayList;
     26import java.util.HashMap;
     27import java.util.LinkedList;
    2528import java.util.List;
    2629
     
    3942
    4043    @GetMapping("/old")
    41     public String getMovies(@RequestParam(required = false) String titleQuery, Model model,
     44    public String getMoviesOld(@RequestParam(required = false) String titleQuery, Model model,
    4245                            @RequestParam(required = false) String error){
    4346        List<Movie> movies;
     
    5659        model.addAttribute("movie_rows", movie_rows);
    5760        model.addAttribute("contentTemplate", "moviesList");
     61        model.addAttribute("genres", genreService.findAll());
    5862        if (error != null && !error.equals(" "))
    5963            model.addAttribute("error",error);
     
    6367
    6468    @GetMapping
    65     public String getMoviesAlternative(@RequestParam(required = false) String titleQuery, Model model,
     69    public String getMovies(@RequestParam(required = false) String titleQuery, Model model,
    6670                            @RequestParam(required = false) String error, @RequestParam(required = false) String page){
    6771        if (page==null){
     
    7276        List<List<Movie>> movie_rows = new ArrayList<>();
    7377        DesignFrontMovies.designMovieList(movies,movie_rows);
     78        //addModelPropertiesForMoviesLikes(model, movies);
    7479        model.addAttribute("movies", movies);
    7580        model.addAttribute("movie_rows", movie_rows);
     81        model.addAttribute("genres", genreService.findAll());
    7682        model.addAttribute("contentTemplate", "moviesListPaged");
    7783        if (error != null && !error.equals(" "))
     
    8490        model.addAttribute("movie", movieService.findById(id));
    8591        addModelPropertiesForUser(model);
     92        model.addAttribute("likes", movieService.findLikesForMovieById(id).getLikes());
    8693        model.addAttribute("contentTemplate", "movieShow");
    8794        return "template";
     
    189196        model.addAttribute("user",user);
    190197    }
     198    private void addModelPropertiesForMoviesLikes(Model model, List<Movie> movies){
     199        HashMap<Integer,MovieLikesQM> movieLikes = new HashMap<>();
     200        for(Movie m: movies){
     201            movieLikes.put(m.getMovieId(),movieService.findLikesForMovieById(m.getMovieId()));
     202        }
     203        model.addAttribute("movieLikes", movieLikes);
     204    }
    191205
    192206}
  • src/main/java/com/wediscussmovies/project/web/controller/rest/MovieRestController.java

    r3c0f9a9 rc02189f  
    5252    public ResponseEntity addGrade(@PathVariable Integer movieId, @RequestBody Grade grade){
    5353        try {
     54            if(grade.getRating() < 5)
     55                grade.setRating(5);
     56            else if(grade.getRating()>10)
     57                grade.setRating(10);
    5458            this.movieService.addGradeMovie(movieId, LoggedUser.getLoggedUser(),grade);
    5559            return ResponseEntity.ok(true);
Note: See TracChangeset for help on using the changeset viewer.