public async Task <ActionResult> CreateFreeVote(CreateFreeVoteModel model)
        {
            if (!ModelState.IsValid)
            {
                return(JsonError());
            }

            var result = await VoteWriter.CreateFreeVote(User.Id(), model);

            if (!ModelState.IsWriterResultValid(result))
            {
                return(JsonError(result.FirstError, "Vote Failed", AlertType.Warning));
            }

            return(JsonSuccess(result.Message, "Vote Success"));
        }
Beispiel #2
0
        public async Task <IWriterResult <bool> > CreateFreeVote(string userId, CreateFreeVoteModel model)
        {
            if (!await VoteService.CheckVoteItems())
            {
                return(WriterResult <bool> .ErrorResult("The current vote round has ended."));
            }

            using (var context = DataContextFactory.CreateContext())
            {
                var voteItem = await context.VoteItem.FirstOrDefaultNoLockAsync(x => x.Id == model.VoteItemId);

                if (voteItem == null)
                {
                    return(WriterResult <bool> .ErrorResult("VoteItem not found."));
                }

                var lastDate = DateTime.UtcNow.AddDays(-1);
                if (await context.Vote.AnyAsync(x => x.UserId == userId && x.Created > lastDate))
                {
                    return(WriterResult <bool> .ErrorResult("You have already voted today."));
                }

                var vote = new Entity.Vote
                {
                    Created    = DateTime.UtcNow,
                    Count      = 1,
                    Type       = VoteType.Free,
                    Status     = VoteStatus.Live,
                    UserId     = userId,
                    VoteItemId = model.VoteItemId
                };

                context.Vote.Add(vote);

                var contextResults = await context.SaveChangesWithLoggingAsync();

                return(WriterResult <bool> .ContextResult(contextResults, "Successfully added {0} free vote(s) for {1}", 1, voteItem.Name));
            }
        }