예제 #1
0
        public async Task <IWriterResult <bool> > CreatePaidVote(string userId, CreatePaidVoteModel model)
        {
            if (!await VoteService.CheckVoteItems())
            {
                return(WriterResult <bool> .ErrorResult("The current vote round has ended."));
            }

            using (var context = DataContextFactory.CreateContext())
            {
                var settings = await context.VoteSetting.FirstOrDefaultNoLockAsync();

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

                if (model.VoteCount <= 0 || (settings.Price * model.VoteCount) <= 0)
                {
                    return(WriterResult <bool> .ErrorResult("Invalid vote amount."));
                }

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

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

                var transferResult = await TradeService.QueueTransfer(new CreateTransferModel
                {
                    UserId       = userId,
                    ToUser       = Constants.SystemVoteUserId,
                    CurrencyId   = settings.CurrencyId,
                    Amount       = model.VoteCount *settings.Price,
                    TransferType = TransferType.Vote
                });

                if (transferResult.HasError)
                {
                    return(WriterResult <bool> .ErrorResult(transferResult.Error));
                }

                var vote = new Entity.Vote
                {
                    Created    = DateTime.UtcNow,
                    Count      = model.VoteCount,
                    Type       = VoteType.Paid,
                    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} vote(s) for {1}", model.VoteCount, voteItem.Name));
            }
        }
예제 #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));
            }
        }