Exemple #1
0
func (vs *VoteService) checkVoteStatus(voteType string, userID, postID int) (vote *model.Vote, err error) {
	c := vs.pool.Get()
	var status int
	status, err = redigo.Int(c.Do("GET", voteType+":"+strconv.Itoa(postID)+":"+strconv.Itoa(userID)))
	if err != nil {
		return
	}

	if status == 1 {
		vote = model.NewVote(voteType, postID, userID, true)
		return
	}

	if status == 0 {
		vote = nil
		err = ErrUserDidNotVote
		return
	}

	if status == -1 {
		vote = model.NewVote(voteType, postID, userID, false)
		return
	}
	return
}
Exemple #2
0
func TestVoteService(t *testing.T) {
	vs := NewVoteService(GetRedisPool())

	votes := []*model.Vote{
		model.NewVote("post", 1, 1, true),
		model.NewVote("post", 1, 1, true),
		model.NewVote("post", 1, 2, true),
		model.NewVote("post", 1, 3, true),
		model.NewVote("comment", 2, 1, false),
		model.NewVote("comment", 2, 2, false),
		model.NewVote("comment", 2, 3, false),
		model.NewVote("comment", 2, 4, false),
		model.NewVote("comment", 2, 5, false),
	}

	for _, vote := range votes {
		err := vs.SaveVote(vote)
		if err != nil {
			t.Errorf("error saving vote: %s", err)
		}
	}

	commentTotal, err := vs.GetRealCommentVotes(2)
	if err != nil {
		t.Errorf("error getting comment votes, %s", err)
	}

	postTotal, err := vs.GetRealPostVotes(1)
	if err != nil {
		t.Errorf("error getting post votes, %s", err)
	}

	if commentTotal != -5 {
		t.Errorf("commentTotal is not the exact number in GetRealCommentVotes(), %d", commentTotal)
	}

	if postTotal != 4 {
		t.Errorf("postTotal is not the exact number in GetRealPostVotes(), %d", postTotal)
	}

	vote, err := vs.CheckUserPostVote(1, 1)
	if err != nil {
		t.Errorf("check user post vote returned an error, %s", err)
	}

	if !vote.Up {
		t.Errorf("upvote not returned as an upvote...")
	}

	vote, err = vs.CheckUserCommentVote(4, 2)
	if err != nil {
		t.Errorf("check user comment vote returned an error, %s", err)
	}

	if vote.Up {
		t.Errorf("downvote was not returned as a downvote.....")
	}

}
Exemple #3
0
// VoteComment lets a user upvote a comment
func VoteComment(ds *datastore.Datastore) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		user, err := util.Authenticate(r, ds)
		if err != nil {
			util.JSONError(w, err, http.StatusUnauthorized)
			return
		}

		postID, err := strconv.Atoi(ps.ByName("post_id"))
		if err != nil {
			util.JSONError(w, err, http.StatusBadRequest)
			return
		}

		commentID, err := strconv.Atoi(ps.ByName("comment_id"))
		if err != nil {
			util.JSONError(w, err, http.StatusBadRequest)
			return
		}

		voteStatus, err := strconv.ParseBool(ps.ByName("vote"))
		if err != nil {
			util.JSONError(w, err, http.StatusBadRequest)
			return
		}

		comment, err := ds.GetComment(commentID)
		if err != nil {
			util.JSONError(w, err, http.StatusInternalServerError)
			return
		}

		if postID != comment.PostID {
			util.JSONError(w, err, http.StatusBadRequest)
			return
		}

		v, err := ds.CheckUserCommentVote(user.ID, commentID)
		if v == nil {
			err = ds.SaveVote(model.NewVote("comment", commentID, user.ID, voteStatus))
			if err != nil {
				util.JSONError(w, err, http.StatusInternalServerError)
				return
			}
			return
		}

		v.Up = voteStatus
		err = ds.UpdateVote(v)
		if err != nil {
			util.JSONError(w, err, http.StatusInternalServerError)
			return
		}

		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]string{"success": "upvoted comment"})
	}
}