/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bounswe.group7.services;

import com.bounswe.group7.model.Comments;
import com.bounswe.group7.model.VotedComments;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bounswe.group7.repository.CommentsRepository;
import com.bounswe.group7.repository.VotedCommentsRepository;

/**
 *
 * @author myunu
 */
@Service
public class CommentsService {
    
    @Autowired
    UsersService usersService;
    
    @Autowired
    CommentsRepository commentsRepository;
    
    @Autowired
    VotedCommentsRepository votedCommentsRepository;
    
    public Comments createComment(Comments comment)
    {
        comment.setUserId(usersService.getLoggedInUserId());
        comment.setDateCreated(new Date());
        comment.setRate(0);
        return commentsRepository.save(comment);
    }
    
    public List<Comments> getTopicComments(Long topicId)
    {
        return commentsRepository.findByTopicIdOrderByDateCreatedDesc(topicId);
    }
    
    public boolean deleteComment(Long commentId)
    {
        if(commentsRepository.exists(commentId))
            commentsRepository.delete(commentId);
        else return false;
        return true;
    }
    
    public boolean voteComment(VotedComments votedComment)
    {
        VotedComments temp = votedCommentsRepository.findByUserIdAndCommentId(usersService.getLoggedInUserId(), votedComment.getCommentId());
        if(temp != null){
            if(temp.getRate() == votedComment.getRate()) return false;
            Comments comment = commentsRepository.findOne(votedComment.getCommentId());
            comment.setRate(comment.getRate() + votedComment.getRate()*2);
            temp.setRate(votedComment.getRate());
            commentsRepository.save(comment);
            votedCommentsRepository.save(temp);
            return true;
        }
        else{
            votedComment.setUserId(usersService.getLoggedInUserId());
            Comments comment = commentsRepository.findOne(votedComment.getCommentId());
            comment.setRate(comment.getRate() + votedComment.getRate());
            commentsRepository.save(comment);
            votedCommentsRepository.save(votedComment);
            return true;
        }
    }
    public int getCommentRate(Long commentId)
    {
        return commentsRepository.findOne(commentId).getRate();
    }
    
}

@@ -587,16 +587,71 @@
 %0A%0A/**%0A *
+ This class is the service class of the Comments Class.
 %0A * @aut
@@ -658,13 +658,19 @@
 hor 
-my
+Y
 unu
+s Seker
 %0A */
@@ -897,24 +897,277 @@
 sitory;%0A    
+/**%0A     * This method takes a comment object and %0A     * creates a comment with the logged in user,%0A     * sets its rate to zero to initialize it.%0A     * @param comment object that has text in it.%0A     * @return returns the added comment object%0A     */
 %0A    public 
@@ -1414,370 +1414,1248 @@
     
-public List%3CComments%3E getTopicComments(Long topicId)%0A    %7B%0A        return commentsRepository.findByTopicIdOrderByDateCreatedDesc(topicId);%0A    %7D%0A    %0A    public boolean deleteComment(Long commentId)%0A    %7B%0A        if(commentsRepository.exists(commentId))%0A            commentsRepository.delete(commentId);%0A        else return false;%0A        return true;%0A    %7D%0A    
+/**%0A     * This method takes a topic id and returns its comments.%0A     * @param topicId The related topic's id.%0A     * @return returns a Comment List of the topic.%0A     */%0A    public List%3CComments%3E getTopicComments(Long topicId)%0A    %7B%0A        return commentsRepository.findByTopicIdOrderByDateCreatedDesc(topicId);%0A    %7D%0A    %0A    /**%0A     * This method takes a comment id and finds it in the database.%0A     * Deletes it.%0A     * @param commentId id of the comment to be deleted%0A     * @return returns true if it could delete. other wise returns false.%0A     */%0A    public boolean deleteComment(Long commentId)%0A    %7B%0A        if(commentsRepository.exists(commentId))%0A            commentsRepository.delete(commentId);%0A        else return false;%0A        return true;%0A    %7D%0A    %0A    /**%0A     * This method takes a VotedComments object and looks to the database.%0A     * If user voted that comment previously and the vote is same, it fails to vote.%0A     * Otherwise it changes the vote to new one and re-calculates its rate.%0A     * If user never voted previously, it adds the new vote to database.%0A     * @param votedComment the object that has comment id and vote(1 or -1).%0A     * @return returns true if the user can vote. Otherwise false.%0A     */
 %0A   
@@ -3681,24 +3681,224 @@
     %7D%0A    %7D%0A
+    /**%0A     * This method returns the rate of the given comment%0A     * @param commentId Id of the comment which its rate will be given.%0A     * @return returns an integer rate of the comment.%0A     */%0A
     public i

Comment Servive Documentation