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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.