예제 #1
0
 public VoteCreateRequestValidatorTest()
 {
     request = new VoteCreateRequest()
     {
         KnowledgeBaseId = 1
     };
     validator = new VoteCreateRequestValidator();
 }
예제 #2
0
        public async Task <ActionResult <VoteGetViewModel> > PostAsync([FromBody] VoteCreateRequest model)
        {
            var voteCreateModel = _mapper.Map <VoteCreateModel>(model);
            var vote            = await _voteService.CastVoteAsync(voteCreateModel);

            await _eventPublisher.PublishAll();

            var voteResponseModel = _mapper.Map <VoteGetViewModel>(vote);

            return(CreatedAtRoute("GetVote", new { voteId = vote.Id }, voteResponseModel));
        }
예제 #3
0
        public async Task <IActionResult> PostVote(int knowledgeBaseId, [FromBody] VoteCreateRequest request)
        {
            var vote = await _context.Votes.FindAsync(knowledgeBaseId, request.UserId);

            if (vote != null)
            {
                return(BadRequest(new ApiBadRequestResponse("This user has been voted for this KB")));
            }

            vote = new Vote()
            {
                KnowledgeBaseId = knowledgeBaseId,
                UserId          = request.UserId
            };
            _context.Votes.Add(vote);

            var knowledgeBase = await _context.KnowledgeBases.FindAsync(knowledgeBaseId);

            if (knowledgeBase != null)
            {
                return(BadRequest(new ApiBadRequestResponse($"Cannot found knowledge base with id {knowledgeBaseId}")));
            }

            knowledgeBase.NumberOfVotes = knowledgeBase.NumberOfVotes.GetValueOrDefault(0) + 1;
            _context.KnowledgeBases.Update(knowledgeBase);

            var result = await _context.SaveChangesAsync();

            if (result > 0)
            {
                return(NoContent());
            }
            else
            {
                return(BadRequest(new ApiBadRequestResponse($"Vote failed")));
            }
        }
예제 #4
0
 public async Task <int> PostVote(VoteCreateRequest request)
 {
     return(await PostAsync <VoteCreateRequest, int>($"/api/knowledgeBases/{request.KnowledgeBaseId}/votes", null));
 }
예제 #5
0
        public async Task <IActionResult> PostVote([FromForm] VoteCreateRequest request)
        {
            var result = await _knowledgeBaseApiClient.PostVote(request);

            return(Ok(result));
        }