001package usecase.vote;
002
003import model.entity.*;
004import model.repository.GenericRepository;
005import model.validation.CommentExists;
006import model.validation.PostExists;
007import usecase.auth.AuthenticationRequired;
008import usecase.auth.CurrentUser;
009
010import javax.enterprise.context.ApplicationScoped;
011import javax.inject.Inject;
012import javax.transaction.Transactional;
013
014/**
015 * Classe che fornisce i servizi relativi ai voti.
016 */
017@ApplicationScoped
018@Transactional
019public class VoteService {
020    private GenericRepository genericRepository;
021    private CurrentUser currentUser;
022
023    protected VoteService(){}
024
025    @Inject
026    protected VoteService(GenericRepository genericRepository, CurrentUser currentUser){
027        this.genericRepository = genericRepository;
028        this.currentUser = currentUser;
029    }
030
031    /**
032     * Aggiunge un voto positivo ad un commento
033     * @param id di un commento esistente
034     */
035    @AuthenticationRequired
036    public void upvoteComment(@CommentExists int id){
037        voteComment(id, (short) +1);
038    }
039
040    /**
041     * Aggiunge un voto negativo ad un commento
042     * @param id di un commento esistente
043     */
044    @AuthenticationRequired
045    public void downvoteComment(@CommentExists int id){
046        voteComment(id, (short) -1);
047    }
048
049    /**
050     * Aggiunge un voto positivo ad un post
051     * @param id di un post esistente
052     */
053    @AuthenticationRequired
054    public void upvotePost(@PostExists int id){
055        votePost(id, (short) +1);
056    }
057
058    /**
059     * Aggiunge un voto negativo ad un post
060     * @param id di un post esistente
061     */
062    @AuthenticationRequired
063    public void downvotePost(@PostExists int id){
064        votePost(id, (short) -1);
065    }
066
067    /**
068     * Rimuove il voto ad un commento
069     * @param id di un commento esistente
070     */
071    @AuthenticationRequired
072    public void unvoteComment(@CommentExists int id){
073        Comment comment = genericRepository.findById(Comment.class, id);
074        User user = genericRepository.findById(User.class, currentUser.getId());
075        CommentVote commentVote = comment.getVote(user);
076        if(commentVote != null)
077            genericRepository.remove(commentVote);
078    }
079
080    /**
081     * Rimuove il voto ad un post
082     * @param id di un post esistente
083     */
084    @AuthenticationRequired
085    public void unvotePost(@PostExists int id){
086        Post post = genericRepository.findById(Post.class,id);
087        User user = genericRepository.findById(User.class, currentUser.getId());
088
089        PostVote vote = post.getVote(user);
090        if(vote != null)
091            genericRepository.remove(vote);
092    }
093
094    /**
095     * Aggiunge un voto ad un post
096     * @param id di un post
097     * @param vote tipo di voto: 1 indica voto positivo, -1 voto negativo
098     */
099    private void votePost(int id, short vote){
100        Post post = genericRepository.findById(Post.class,id);
101        User user = genericRepository.findById(User.class, currentUser.getId());
102
103        PostVote postVote = new PostVote(user,post,vote);
104        genericRepository.merge(postVote);
105    }
106
107    /**
108     * Aggiunge un voto ad un commento
109     * @param id di un commento
110     * @param vote tipo di voto: 1 indica voto positivo, -1 voto negativo
111     */
112    private void voteComment(int id, short vote) {
113        Comment comment = genericRepository.findById(Comment.class,id);
114        User user = genericRepository.findById(User.class, currentUser.getId());
115
116        CommentVote commentVote = new CommentVote(user,comment, vote);
117        genericRepository.merge(commentVote);
118    }
119}