예제 #1
0
        public async Task <IActionResult> Create(VoteModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            try
            {
                var user = await GetCurrentUser();

                var vote = new Vote
                {
                    ArticleId = model.ArticleId,
                    VotedOn   = _dateTimeService.GetNowUtc(),
                    UserId    = user.Id,
                    VoteType  = model.VoteType
                };

                vote = await _voteService.CreateVote(vote);

                var artVoteCount = await GetVoteCountForArticle(model.ArticleId, model.VoteType);

                var score = (await _dbContext.Articles.FindAsync(model.ArticleId))?.Score;

                var voteResultModel = new
                {
                    model.ArticleId,
                    model.VoteType,
                    VoteCount = artVoteCount,
                    Score     = score
                };

                return(Created("", voteResultModel));
            }
            catch (ApplicationException ex) when(ex.Message == "Article does not exist")
            {
                _logger.LogError(ex, $"Article {model.ArticleId} does not exist");
                return(NotFound("Article not found"));
            }
            catch (ApplicationException ex) when(ex.Message.Contains("does not allow multiple votes"))
            {
                var errorMsg = "Vote type does not allow multiple votes";

                _logger.LogInformation(errorMsg);
                return(StatusCode((int)HttpStatusCode.BadRequest, errorMsg));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error creating vote", model.ArticleId, model);

                return(StatusCode((int)HttpStatusCode.InternalServerError, "Error creating vote"));
            }
        }
예제 #2
0
 public ActionResult <VoteModel> CreateVote(int songId, [FromBody] VoteModel vote)
 {
     try
     {
         if (!ModelState.IsValid)
         {
             return(BadRequest(ModelState));
         }
         var newVote = service.CreateVote(songId, vote);
         return(Created($"api/songs/{songId}/votes/{vote.Id}", newVote));
     }
     catch (Exception)
     {
         throw;
     }
 }
예제 #3
0
        public async Task <bool> Handle(CreateVoteCommand request, CancellationToken cancellationToken)
        {
            var isVotationValid = await _service.IsValidVotation(request.Vote.VotationId);

            if (!isVotationValid)
            {
                throw new ErrorException("99", "La votación no ya está activa o no existe");
            }

            var hasUserVoted = await _service.UserHasAlreadyVoted(request.Vote.UserId, request.Vote.VotationId);

            // validar que el rol del usuario no es admin

            var userRol = await _userRolService.GetRolByUserId(request.Vote.UserId);

            if (userRol.Id != 2)
            {
                throw new ErrorException("102", "Usuarios con este rol no pueden votar");
            }


            if (hasUserVoted)
            {
                throw new ErrorException("100", "Este usuario ya votó");
            }

            var isValidCandidate = await _service.CandidateIsInVotation(request.Vote.CandidateId, request.Vote.VotationId);

            if (!isValidCandidate)
            {
                throw new ErrorException("101", "Este candidato no pertenece a esta votación");
            }

            var result = await _service.CreateVote(request.Vote);

            if (!result)
            {
                throw new ErrorException("01", "No se ha podido realizar el voto");
            }

            return(true);
        }
예제 #4
0
 public ActionResult <VoteModel> CreateVote(int songId, [FromBody] VoteModel vote)
 {
     try
     {
         if (!ModelState.IsValid)
         {
             return(BadRequest(ModelState));
         }
         var newVote = service.CreateVote(songId, vote);
         return(Created($"api/songs/{songId}/votes/{newVote.Id}", newVote));
     }
     catch (BadOperationRequest ex)
     {
         return(BadRequest(ex.Message));
     }
     catch (Exception ex)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
     }
 }