public async Task <ActionResult <TransactionRequest> > Checkout() { decimal price = 0; Receipt receipt = new Receipt(); var CartItem = await this.cart.cart_items.ToListAsync().ConfigureAwait(false); foreach (var cart_item in CartItem) { POSItems item_data = await _context.Items.FindAsync(cart_item.Id); decimal item_price = item_data.price * cart_item.Quantity; price += item_price; receipt.purchased_items.Add(POSController.create_purchase_item_entry(item_price, item_data, cart_item)); } if (this._user.wallet < price) { _validator.SetMessage("Insufficient Funds."); return(BadRequest(new BadRequestObjectResult(_validator))); } receipt.total_price = price; this._user.wallet -= price; receipt.remaining_balance = this._user.wallet; if (await this.UpdateEntry(this._user).ConfigureAwait(false)) { this.cart.Database.ExecuteSqlRaw("TRUNCATE `cart_items`"); return(Ok(receipt)); } else { return(StatusCode(500)); } }
public POSController(IPOSItemValidator validator, ITransactionRequestValidator cartValidator, POSContext context, TransactionContext cart) { this._context = context; this._validator = validator; this._cartValidator = cartValidator; this.cart = cart; this._user = POSController.CreateUser(); if (!this._context.Users.Any(u => u.user_id == this._user.user_id)) { this._user = POSController.CreateUser(); this._context.Users.Add(this._user); this._context.SaveChanges(); } else { var users = this._context.Users.Include(u => u.rights); foreach (var user in users) { if (user.user_id == this._user.user_id) { this._user = user; } } } }