Ejemplo n.º 1
0
        public void DeleteCommentShouldDeleteCommentAndDeleteVotesFromIt()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "DeleteCommentShouldDeleteCommentAndDeleteVotesFromIt")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var commentService = new CommentService(dbContext);

            var comment = new Comment();

            dbContext.Comments.Add(comment);

            var vote = new VoteComment
            {
                CommentId = comment.Id
            };

            dbContext.VoteComments.Add(vote);
            dbContext.SaveChanges();

            commentService.DeleteComment(comment.Id);

            var comments = dbContext.Comments.ToList();
            var votes    = dbContext.VoteComments.ToList();

            Assert.Empty(votes);
            Assert.Empty(comments);
        }
Ejemplo n.º 2
0
        public void CreateCommentVoteShouldAddVoteOnCommentAndIncreaseOrDecreaseVoteCount()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "CreateCommentVoteShouldAddVoteOnCommentAndIncreaseOrDecreaseVoteCountDB")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var postService = new Mock <IPostService>();

            var commentId = "asdasdasd";

            var comment = new Comment
            {
                Id        = commentId,
                VoteCount = 7,
            };
            var commentService = new Mock <ICommentService>();

            commentService.Setup(x => x.GetById(commentId)).Returns(comment);

            var vote = new VoteComment
            {
                CommentId = comment.Id,
                UpOrDown  = true,
            };

            var voteService = new VoteService(dbContext, postService.Object, commentService.Object);

            voteService.CreateCommentVote(vote);

            Assert.Equal(8, comment.VoteCount);
        }
Ejemplo n.º 3
0
        public void ChangeCommentVote(VoteComment vote, bool up)
        {
            if (vote == null)
            {
                return;
            }

            var comment = this.commentService.GetById(vote.CommentId);

            if (up == true)
            {
                vote.UpOrDown = true;
            }
            else
            {
                vote.UpOrDown = false;
            }

            if (vote.UpOrDown == true)
            {
                comment.VoteCount += 2;
            }
            else
            {
                comment.VoteCount -= 2;
            }

            this.context.SaveChanges();
        }
Ejemplo n.º 4
0
        public void CreateCommentVote(VoteComment vote)
        {
            this.context.VoteComments.Add(vote);

            var comment = this.commentService.GetById(vote.CommentId);

            if (vote.UpOrDown == true)
            {
                comment.VoteCount++;
            }
            else
            {
                comment.VoteCount--;
            }

            this.context.SaveChanges();
        }
Ejemplo n.º 5
0
        public void DeleteCommentVote(VoteComment vote)
        {
            if (vote == null)
            {
                return;
            }

            var comment = this.commentService.GetById(vote.CommentId);

            if (vote.UpOrDown == true)
            {
                comment.VoteCount--;
            }
            else
            {
                comment.VoteCount++;
            }

            this.context.VoteComments.Remove(vote);
            this.context.SaveChanges();
        }
Ejemplo n.º 6
0
        public IActionResult VoteComment([FromBody] VoteInputModel model)
        {
            var currentUser = this.userService.GetUserByName(this.User.Identity.Name);
            var comment     = this.commentService.GetById(model.Id);
            var userVote    = this.voteService.GetVoteCommentById(comment.Id, currentUser.Id);

            if (model.VoteDown != model.VoteUp && userVote == null)
            {
                var vote = new VoteComment
                {
                    CommentId = model.Id,
                    UserId    = currentUser.Id,
                };

                if (model.VoteUp == true)
                {
                    vote.UpOrDown = true;
                }
                else if (model.VoteDown == true)
                {
                    vote.UpOrDown = false;
                }

                this.voteService.CreateCommentVote(vote);
            }
            else if (model.VoteDown != model.VoteUp && userVote != null)
            {
                this.voteService.ChangeCommentVote(userVote, model.VoteUp);
            }
            else if (model.VoteDown == model.VoteUp && userVote != null)
            {
                this.voteService.DeleteCommentVote(userVote);
            }

            return(new JsonResult(comment.VoteCount));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> VoteComment(bool vote, int id)
        {
            int voteValue = 0;

            if (vote)
            {
                voteValue = 1;
            }
            else
            {
                voteValue = -1;
            }

            var voter   = _userManager.GetUserId(User);
            var comment = await _context.Comment.SingleOrDefaultAsync(r => r.ID == id);

            var hasVoted = await _context.VoteComment.SingleOrDefaultAsync(v => v.CommentID == id && v.VoterID == voter);

            if (comment == null)
            {
                return(NotFound());
            }

            if (hasVoted != null)
            {
                if (hasVoted.Vote != vote)
                {
                    comment.Points += 2 * voteValue;
                    hasVoted.Vote   = vote;

                    try
                    {
                        _context.VoteComment.Update(hasVoted);
                        _context.Comment.Update(comment);
                        _context.SaveChanges();

                        await AddPoints(comment.AuthorID, 2 *voteValue);

                        var result = new
                        {
                            successful = true,
                            message    = "Glasanje na komentaru uspješno obavljeno",
                            points     = comment.Points
                        };
                        return(Json(result));
                    }
                    catch (Exception e)
                    {
                        var error = new
                        {
                            successful = false,
                            message    = "Glasanje na komentaru nije uspjelo",
                        };
                        return(Json(error));
                    }
                }
                else
                {
                    var result = new
                    {
                        successful = false,
                        message    = "Isti glas na komentaru"
                    };
                    return(Json(result));
                }
            }

            var voteTable = new VoteComment
            {
                VoterID   = voter,
                Vote      = vote,
                CommentID = id
            };

            comment.Points += voteValue;

            try
            {
                _context.VoteComment.Add(voteTable);
                _context.Comment.Update(comment);
                _context.SaveChanges();

                await AddPoints(comment.AuthorID, voteValue);

                var result = new
                {
                    successful = true,
                    message    = "Glasanje na komentaru uspješno obavljeno",
                    points     = comment.Points
                };
                return(Json(result));
            }
            catch (Exception e)
            {
                var error = new
                {
                    successful = false,
                    message    = "Glasanje na komentaru nije uspjelo",
                };
                return(Json(error));
            }
        }