Ejemplo n.º 1
0
        public async Task <IActionResult> UpdateVote([FromBody] SendVoteVM vote)
        {
            string userId = User.Claims.First(c => c.Type == "UserID").Value;
            await _voteService.UpdateVote(vote, userId);

            return(Ok(new { message = "Voted successful", currentRate = await _voteService.GetMemeRate(vote.MemeRefId) }));
        }
Ejemplo n.º 2
0
        public async Task <Result <Vote> > InsertVote(SendVoteVM voteVM, string userId)
        {
            Vote vote = new Vote()
            {
                Value     = voteVM.Value,
                MemeRefId = voteVM.MemeRefId,
                UserId    = userId,
            };

            if (await _repository.IsExistAsync(m => m.MemeRefId == voteVM.MemeRefId && m.UserId == userId) == false)
            {
                var result = await ValidateAsync(vote);

                if (result.Succeeded)
                {
                    await _repository.InsertAsync(vote);

                    return(result);
                }
                throw new MemeSiteException(HttpStatusCode.BadRequest, null, result);
            }
            else
            {
                throw new MemeSiteException(HttpStatusCode.Conflict, "U have voted for this");
            }
        }
Ejemplo n.º 3
0
        public void UpdateVoteNotFound()
        {
            string userId   = "CurrentUserID";
            var    sendVote = new SendVoteVM()
            {
                MemeRefId = 1,
                Value     = Value.downvote
            };

            var service = new VoteService(voteRepoMock.Object, new VoteValidator());
            var result  = service.UpdateVote(sendVote, userId);

            Assert.Equal("Not Found", result.Exception.InnerException.Message);
        }
Ejemplo n.º 4
0
        public void InsertSecondVoteOnSameItem()
        {
            string userId   = "CurrentUserID";
            var    sendVote = new SendVoteVM()
            {
                MemeRefId = 1,
                Value     = Value.downvote
            };

            var service = new VoteService(voteRepoMock.Object, new VoteValidator());

            voteRepoMock.Setup(x => x.IsExistAsync(y => y.MemeRefId == sendVote.MemeRefId && y.UserId == userId)).ReturnsAsync(true);
            var result = service.InsertVote(sendVote, userId);

            Assert.Equal("U have voted for this", result.Exception.InnerException.Message);
        }
Ejemplo n.º 5
0
        public void InsertNewVote()
        {
            string userId   = "CurrentUserID";
            var    sendVote = new SendVoteVM()
            {
                MemeRefId = 1,
                Value     = Value.downvote
            };

            var service = new VoteService(voteRepoMock.Object, new VoteValidator());

            voteRepoMock.Setup(x => x.IsExistAsync(y => y.MemeRefId == sendVote.MemeRefId && y.UserId == userId)).ReturnsAsync(false);
            var result = service.InsertVote(sendVote, userId);

            Assert.NotNull(result);
            Assert.True(result.Result.Succeeded);
        }
Ejemplo n.º 6
0
        public void CorrectUpdateVote()
        {
            string userId   = "CurrentUserID";
            var    sendVote = new SendVoteVM()
            {
                MemeRefId = 1,
                Value     = Value.downvote
            };

            var service = new VoteService(voteRepoMock.Object, new VoteValidator());

            voteRepoMock.Setup(x => x.FindAsync(y => y.MemeRefId == sendVote.MemeRefId && y.UserId == userId))
            .ReturnsAsync(new Vote()
            {
                MemeRefId = 1, UserId = userId, Value = Value.upvote
            });
            var result = service.UpdateVote(sendVote, userId);

            Assert.NotNull(result);
            Assert.True(result.Result.Succeeded);
        }
Ejemplo n.º 7
0
        public async Task <Result <Vote> > UpdateVote(SendVoteVM voteVM, string userId)
        {
            var vote = await _repository.FindAsync(m => m.MemeRefId == voteVM.MemeRefId && m.UserId == userId);

            if (vote == null)
            {
                throw new MemeSiteException(HttpStatusCode.NotFound, "Not Found");
            }
            if (voteVM.Value == vote.Value)
            {
                throw new MemeSiteException(HttpStatusCode.Conflict, "Value is the same");
            }
            vote.Value = voteVM.Value;
            var result = await ValidateAsync(vote);

            if (result.Succeeded)
            {
                result.Value = await _repository.UpdateAsync(vote);

                return(result);
            }
            throw new MemeSiteException(HttpStatusCode.BadRequest, null, result);
        }