示例#1
0
        public async Task<ActionResult<OrderItem>> PostOrderItem([FromBody]OrderItem orderItem, [FromQuery]long productID)
        {
            try
            {
                if (productID <= 0)
                    throw new Exception("One or more validation errors occurred");

                var productExists = await DbAccessClass.ProductIDExists(productID, _context);

                if (!productExists)
                    throw new Exception("Product not found");

                var product = await DbAccessClass.GetProduct(productID, _context);
                var orderExits = await DbAccessClass.OrderExists(product.ProductID, _context);

                if (orderExits)
                    throw new Exception("Order already exists");

                var newOrder = DbAccessClass.AddOrder(orderItem, product, _context);

                return CreatedAtAction("GetOrder", new { id = orderItem.OrderID }, Ok("Order added successfully"));

            }
            catch (Exception ex)
            {
                return ex.Message switch
                {
                    "Product not found" => NotFound(new JsonResult(ex.Message)),
                    "Order already exists" => Conflict(new JsonResult(ex.Message)),
                    "One or more validation errors occurred" => UnprocessableEntity(new JsonResult(ex.Message)),
                    _ => BadRequest(ex.Message),
                };
            }
        }
示例#2
0
        public async Task <ActionResult <SellItem> > PostSellItem([FromBody] SellItem sellItem, [FromQuery] long productID)
        {
            try
            {
                if (productID <= 0)
                {
                    throw new Exception("One or more validation errors occurred");
                }

                var productExists = await DbAccessClass.ProductIDExists(productID, _context);

                if (!productExists)
                {
                    throw new Exception("Product not found");
                }

                var product = await DbAccessClass.GetProduct(productID, _context);

                var availableStock = product.StockAmount - sellItem.Quantity;

                if (product.StockAmount == 0)
                {
                    throw new Exception("Product out of stock");
                }

                if (availableStock < 0)
                {
                    throw new Exception("Not enough quantity available");
                }

                var verifyPrice        = product.SellPrice * sellItem.Quantity;
                var verifyWithDiscount = DbAccessClass.CalculateSubtotal(verifyPrice, product.Discount);

                if (sellItem.ContainerReturned && verifyWithDiscount != sellItem.TotalCost)
                {
                    throw new Exception("Price does not match subtotal");
                }
                else if (!sellItem.ContainerReturned && verifyPrice != sellItem.TotalCost)
                {
                    throw new Exception("Price does not match subtotal");
                }

                if (sellItem.Paid < 0)
                {
                    throw new Exception("Payment is required");
                }

                var change = sellItem.Paid - sellItem.TotalCost;

                if (change < 0)
                {
                    throw new Exception("Not enough payment");
                }

                await DbAccessClass.AddSell(sellItem, product, _context);

                return(CreatedAtAction("GetSell", new { id = sellItem.SellID }, Ok(change)));
            }
            catch (Exception ex)
            {
                return(ex.Message switch
                {
                    "Product not found" => NotFound(new JsonResult(ex.Message)),
                    "Product out of stock" => StatusCode(417, new JsonResult(ex.Message)),
                    "Not enough quantity available" => StatusCode(417, new JsonResult(ex.Message)),
                    "Price does not match subtotal" => StatusCode(409, new JsonResult(ex.Message)),
                    "Payment is required" => StatusCode(402, new JsonResult(ex.Message)),
                    "Not enough payment" => StatusCode(406, new JsonResult(ex.Message)),
                    "One or more validation errors occurred" => UnprocessableEntity(new JsonResult(ex.Message)),
                    _ => BadRequest(new JsonResult(ex.Message)),
                });