Exemple #1
0
        public async Task <ActionResult> ClaimPrizeModal(ClaimPrizeModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var userId = User.Identity.GetUserId <int>();
            var prize  = await PrizeReader.GetUserPrize(userId, model.Id);

            if (prize.Status != PrizeStatus.Unclaimed)
            {
                return(CloseModalSuccess());
            }

            if (model.IsPointsClaim)
            {
                var paymentMethod = await PaymentReader.GetMethod(prize.Data);

                if (paymentMethod == null)
                {
                    return(CloseModalError("Unknown Error"));
                }

                var paymentUserMethod = await PaymentReader.GetUserMethod(userId, paymentMethod.Id);

                if (paymentUserMethod == null)
                {
                    await PaymentWriter.CreateMethod(userId, paymentMethod.Id);
                }

                paymentUserMethod = await PaymentReader.GetUserMethod(userId, paymentMethod.Id);

                if (paymentUserMethod == null)
                {
                    return(CloseModalError("Unknown Error"));
                }

                model.Data3 = paymentUserMethod.Data;
            }

            if (string.IsNullOrEmpty(model.Data3))
            {
                ModelState.AddModelError("", "Invalid Crypto Address");
                return(View(model));
            }

            var result = await PrizeWriter.ClaimPrize(userId, model);

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

            return(CloseModalSuccess());
        }
Exemple #2
0
        public async Task <IWriterResult> ClaimPrize(int userId, ClaimPrizeModel model)
        {
            using (var context = DataContextFactory.CreateContext())
            {
                var prize = await context.Prize.FirstOrDefaultAsync(x => x.Id == model.Id && x.UserId == userId);

                if (prize == null)
                {
                    return(new WriterResult(false, "Prize not found"));
                }

                if (prize.Status != PrizeStatus.Unclaimed)
                {
                    return(new WriterResult(false, $"Unable to claim prize in {prize.Status} status"));
                }

                // Data3 = address
                prize.Data3  = model.Data3;
                prize.Status = PrizeStatus.Pending;
                await context.SaveChangesAsync();

                return(new WriterResult(true));
            }
        }