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));
            }
        }
 private static POSContext AddItem(POSContext context, POSItems item)
 {
     if (!context.Items.Any(e => e.Id == item.Id))
     {
         context.Add(item);
         context.SaveChanges();
     }
     return(context);
 }
        private Response201Body Create201ResponseBody(POSItems data)
        {
            Response201Body response = new Response201Body();

            response.ExceutedBy = _user.Name;
            response.data       = data;
            response.timestamp  = DateTime.Now;
            return(response);
        }
        public static POSItems GenerateMockItem1()
        {
            POSItems item = new POSItems();

            item.Id      = 1;
            item.Name    = "PS1";
            item.price   = 20;
            item.InStock = 10;
            return(item);
        }
        public static POSItems GenerateMockItem3()
        {
            POSItems item = new POSItems();

            item.Id      = 3;
            item.Name    = "PS3";
            item.price   = 100;
            item.InStock = 10;
            return(item);
        }
        public static POSItems GenerateMockItem2()
        {
            POSItems item = new POSItems();

            item.Id      = 2;
            item.Name    = "PS2";
            item.price   = 40;
            item.InStock = 5;
            return(item);
        }
        private static PurchasedItem create_purchase_item_entry(decimal price, POSItems item, TransactionRequest request)
        {
            PurchasedItem item_entry = new PurchasedItem();

            item_entry.Id        = item.Id;
            item_entry.item_name = item.Name;
            item_entry.Quantity  = request.Quantity;
            item_entry.price     = price;
            return(item_entry);
        }
        public async Task <dynamic> AddItemToCart(TransactionRequest item)
        {
            if (!POSItemsExists(item.Id))
            {
                return("Item does not exist");
            }
            POSItems entry_item = _context.Items.Find(item.Id);
            int      stock      = (int)entry_item.InStock - (int)item.Quantity;

            if (stock < 0)
            {
                return("Insufficient Stock");
            }
            entry_item.InStock -= item.Quantity;
            await this.UpdateIfExist(item).ConfigureAwait(false);

            return(await UpdateEntry(entry_item).ConfigureAwait(false));
        }
示例#9
0
 public bool ValidateItem(POSItems items)
 {
     if (items.Id <= 0)
     {
         this.SetMessage("ID cannot be zero or less in value");
         return(false);
     }
     if (items.Name == null || items.Name == "")
     {
         this.SetMessage("Name cannot be empty");
         return(false);
     }
     if (items.price <= 0)
     {
         this.SetMessage("Prize cannot be zero or less in value");
         return(false);
     }
     return(true);
 }
        public async Task <ActionResult <POSItems> > PostPOSItems(POSItems pOSItems)
        {
            if (_user == null || !_user.rights.CreateRights)
            {
                return(Unauthorized());
            }
            if (!_validator.ValidateItem(pOSItems))
            {
                return(BadRequest(new BadRequestObjectResult(_validator)));
            }
            if (POSItemsExists(pOSItems.Id))
            {
                _validator.SetMessage("Entry does not exist. Are you trying to do a PUT method?");
                return(BadRequest(new BadRequestObjectResult(_validator)));
            }
            _context.Items.Add(pOSItems);
            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(Created(HttpContext.Request.Path, this.Create201ResponseBody(pOSItems)));
        }
 public async Task <IActionResult> PutPOSItems(POSItems pOSItems)
 {
     if (_user == null || !_user.rights.ModifyRights)
     {
         return(Unauthorized());
     }
     if (!_validator.ValidateItem(pOSItems))
     {
         return(BadRequest(new BadRequestObjectResult(_validator)));
     }
     if (!POSItemsExists(pOSItems.Id))
     {
         _validator.SetMessage("Entry does not exist. Are you trying to do a POST method?");
         return(BadRequest(new BadRequestObjectResult(_validator)));
     }
     if (await this.UpdateEntry(pOSItems).ConfigureAwait(false))
     {
         return(NoContent());
     }
     else
     {
         return(StatusCode(500));
     }
 }