package sculture.dao;

import org.springframework.stereotype.Repository;
import sculture.models.tables.Story;
import sculture.models.tables.relations.VoteStory;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.util.List;

@Repository
@Transactional
public class VoteStoryDao {

    public Story vote(long user_id, long story_id, int vote) {
        VoteStory voteStory = get(user_id, story_id);
        Story story = entityManager.find(Story.class, story_id);

        if (voteStory == null) {
            voteStory = new VoteStory();
            voteStory.setStory_id(story_id);
            voteStory.setUser_id(user_id);
            if (vote == 1) {
                story.setPositive_vote(story.getPositive_vote() + 1);
            } else if (vote == -1)
                story.setNegative_vote(story.getNegative_vote() + 1);
        } else {
            if (vote != voteStory.getVote()) {
                if (voteStory.getVote() == -1)
                    story.setNegative_vote(story.getNegative_vote() - 1);
                if (voteStory.getVote() == 1)
                    story.setPositive_vote(story.getPositive_vote() - 1);
                if (vote == 1)
                    story.setPositive_vote(story.getPositive_vote() + 1);
                if (vote == -1)
                    story.setNegative_vote(story.getNegative_vote() + 1);
            }
        }
        voteStory.setVote(vote);
        entityManager.merge(voteStory);
        entityManager.merge(story);
        return story;
    }

    public VoteStory get(long user_id, long story_id) {
        Query queryVote = entityManager.createQuery("from VoteStory where user_id = :user_id AND story_id = :story_id");
        queryVote.setParameter("user_id", user_id);
        queryVote.setParameter("story_id", story_id);
        List<VoteStory> voteStoryList = queryVote.getResultList();
        VoteStory voteStory = null;
        if (voteStoryList.size() > 0)
            voteStory = voteStoryList.get(0);
        else
        {
            voteStory = new VoteStory();
            voteStory.setVote(0);
            voteStory.setStory_id(story_id);
            voteStory.setUser_id(user_id);
        }
        return voteStory;
    }

    // An EntityManager will be automatically injected from entityManagerFactory
    // setup on DatabaseConfig class.
    @PersistenceContext
    private EntityManager entityManager;
}

@@ -195,16 +195,60 @@
 anager;%0A
+import javax.persistence.NoResultException;%0A
 import j
@@ -1946,24 +1946,25 @@
 ry_id);%0A
+%0A
         
 List%3CVot
@@ -1959,13 +1959,8 @@
     
-List%3C
 Vote
@@ -1964,17 +1964,16 @@
 oteStory
-%3E
  voteSto
@@ -1978,86 +1978,34 @@
 tory
-List = queryVote.getResultList();%0A        VoteStory voteStory = null;%0A
+;%0A        try %7B%0A  
         
 if (
@@ -1992,36 +1992,34 @@
 try %7B%0A          
-if (
+  
 voteStoryList.si
@@ -2015,68 +2015,49 @@
 tory
-List.size() %3E 0)%0A            voteStory = voteStoryList.ge
+ = (VoteStory) queryVote.getSingleResul
 t(
-0
 );%0A 
@@ -2067,20 +2067,45 @@
     
-else%0A       
+%7D catch (NoResultException exception)
  %7B%0A 
@@ -2215,32 +2215,32 @@
 y_id(story_id);%0A
-
             vote
@@ -2258,32 +2258,76 @@
 er_id(user_id);%0A
+            entityManager.merge(voteStory);%0A
         %7D%0A      

Make vote retrievel better