Example #1
0
        // CAST Vote
        public async Task <bool> CastVoteAsync(VoteCreate model)
        {
            var targetOrder = await _context.OrdersOfBusiness.FindAsync(model.OrderOfBusinessId);

            if (targetOrder == null)
            {
                return(false);
            }

            var userVote = targetOrder.Votes.FirstOrDefault(o => o.VoterId == _userId);

            if (userVote != null)
            {
                userVote.Status      = (userVote.Status != model.Status) ? model.Status : VoteStatus.NoVote;
                userVote.IsAnonymous = model.IsAnonymous;
            }
            else
            {
                targetOrder.Votes.Add(new VoteEntity {
                    Status = model.Status, VoterId = _userId, IsAnonymous = model.IsAnonymous
                });
            }

            return(await _context.SaveChangesAsync() == 1);
        }
Example #2
0
        public async Task <IHttpActionResult> CastVote(int id, VoteCreate model)
        {
            _service.SetUserId(User.Identity.GetUserId());

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (model == null)
            {
                return(BadRequest("Request body cannot be empty."));
            }

            if (model.OrderOfBusinessId != id)
            {
                return(BadRequest($"Body ID ({model.OrderOfBusinessId}) and URI ID ({id}) mismatch."));
            }

            if (await _service.CastVoteAsync(model))
            {
                return(Ok("Vote cast."));
            }

            return(BadRequest("Vote cannot be submitted."));
        }
Example #3
0
        public bool CreateVote(VoteCreate vote)
        {
            var newVote =
                new Vote()
            {
                PlayerId = vote.PlayerId
            };

            using (var ctx = new ApplicationDbContext())
            {
                ctx.Votes.Add(newVote);
                return(ctx.SaveChanges() == 1);
            }
        }
Example #4
0
        public IHttpActionResult Post(VoteCreate vote)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            VoteService voteService = new VoteService();

            if (!voteService.CreateVote(vote))
            {
                return(InternalServerError());
            }

            return(Ok());
        }