示例#1
0
        private void PayBills(object state)
        {
            using var context = new MiBankContextWrapper(_serviceProvider);
            var bills = context.GetAllBills();

            foreach (var bill in bills)
            {
                var resultBag = bill.doScheduledPayment();
                switch (resultBag.transactionResult)
                {
                case ScheduledBillPayResult.OK_NOT_PAID:
                case ScheduledBillPayResult.BLOCKED:
                    //do nothing
                    break;

                case ScheduledBillPayResult.OK_PAID:
                case ScheduledBillPayResult.FAIL_NOT_ENOUGH:
                    context.CreateTransaction(resultBag.transaction);
                    break;

                default:
                    throw new ArgumentException("Unhandled value in switch statement");
                }
            }

            context.SaveChangesAsync();
        }
        public async Task <IActionResult> Pay(BillPayViewModel vm)
        {
            bool update = vm.Bill.BillPayId == 0 ? false : true;

            vm.Bill.Account = await _context.GetAccount(CustomerId, vm.Bill.AccountId);

            vm.Bill.Payee = await _context.GetPayee(vm.Bill.PayeeId);

            switch (vm.Bill.SchedulePayment())
            {
            case BillPayResult.FAIL_DATE:
                ModelState.AddModelError($"Bill.{nameof(BillPay.ScheduleDate)}", "Date cannot be in the past");
                goto ErrorCleanup;

            case BillPayResult.FAIL_NEGATIVE:
                ModelState.AddModelError($"Bill.{nameof(BillPay.Amount)}", "Amount must be above 0");
                goto ErrorCleanup;

            case BillPayResult.FAIL_FORMAT:
                goto ErrorCleanup;

            case BillPayResult.OK:
                if (update)
                {
                    var oldBill = await _context.GetBill(vm.Bill.BillPayId, CustomerId);

                    //check we don't edit someone else's bill
                    if (oldBill == null)
                    {
                        ModelState.AddModelError($"Bill.{nameof(BillPay.BillPayId)}", "Illegal BillId in request");
                        goto ErrorCleanup;
                    }
                    //prevent 'Blocked' attribute from being set by users
                    vm.Bill.Blocked = oldBill.Blocked;
                    _context.UpdateBill(CustomerId, vm.Bill);
                }
                else
                {
                    vm.Bill.Blocked = false;
                    _context.CreateBill(CustomerId, vm.Bill);
                }
                _context.SaveChangesAsync();
                break;
            }
            if (update)
            {
                TempData["successMessage"] = $"Updated bill #{vm.Bill.BillPayId}.";
            }
            else
            {
                TempData["successMessage"] = $"Created bill #{vm.Bill.BillPayId}.";
            }

            return(RedirectToAction(nameof(Index)));

ErrorCleanup:
            vm.Payees   = _context.GetAllPayees();
            vm.Customer = await _context.GetCustomerWithAccounts(CustomerId);

            return(View(vm));
        }