public async Task <ActionResult> CreatePaidVote(CreatePaidVoteModel model)
        {
            if (model.TwoFactorType == TwoFactorType.None)
            {
                ModelState.Remove("Data");
            }

            if (!ModelState.IsValid)
            {
                return(View("CreatePaidVoteModal", model));
            }

            var user = await UserManager.FindByIdAsync(User.Id());

            if (user == null)
            {
                return(UnauthorizedModal());
            }

            // Verify TwoFactor code.
            if (!await UserManager.VerifyUserTwoFactorCodeAsync(TwoFactorComponentType.Transfer, user.Id, model.Data))
            {
                ModelState.AddModelError("Data", "Invalid TwoFactor code.");
                return(View("CreatePaidVoteModal", model));
            }

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

            if (!ModelState.IsWriterResultValid(result))
            {
                return(View("CreatePaidVoteModal", model));
            }

            return(CloseModalSuccess(result.Message, "Vote Success"));
        }
Beispiel #2
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));
            }
        }
        public async Task <ActionResult> CreatePaidVote(int voteItemId, string voteItem)
        {
            var user = await UserManager.FindByIdAsync(User.Id());

            if (user == null)
            {
                return(UnauthorizedModal());
            }

            var voteSettings = await VoteReader.GetVoteSettings();

            if (voteSettings == null)
            {
                return(ViewMessageModal(ViewMessageModel.Error("Invalid Request", "An unknown error occured.")));
            }

            var balance = await BalanceReader.GetBalance(User.Id(), voteSettings.CurrencyId);

            if (balance == null)
            {
                return(ViewMessageModal(ViewMessageModel.Error("Invalid Request", "An unknown error occured.")));
            }

            var model = new CreatePaidVoteModel
            {
                VoteItemId             = voteItemId,
                VoteItem               = voteItem,
                Price                  = voteSettings.Price,
                Balance                = balance.Avaliable,
                Symbol                 = balance.Symbol,
                TwoFactorComponentType = TwoFactorComponentType.Transfer,
                TwoFactorType          = await UserManager.GetUserTwoFactorTypeAsync(user.Id, TwoFactorComponentType.Transfer)
            };

            return(View("CreatePaidVoteModal", model));
        }