Exemplo n.º 1
0
        public async Task<ActionResult> GetOrderItem([FromHeader]long orderID)
        {
            try
            {
                if (orderID <= 0)
                    throw new Exception("One or more validation errors occurred");

                var orderExists = await DbAccessClass.OrderIDExists(orderID, _context);

                if (!orderExists)
                    throw new Exception("Order not found");

                var getOrder = await DbAccessClass.GetOrder(orderID, _context);

                return Ok(_mapper.Map<OrderGet>(getOrder));
            }
            catch (Exception ex)
            {
                return ex.Message switch
                {
                    "Order not found" => NotFound(ex.Message),
                    "One or more validation errors occurred" => UnprocessableEntity(new JsonResult(ex.Message)),
                    _ => BadRequest(ex.Message),
                };
            }
        }
Exemplo n.º 2
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),
                };
            }
        }
Exemplo n.º 3
0
        public async Task<IActionResult> PutOrderItem([FromBody]OrderUpdate orderUpdate)
        {
            try
            {
                if (orderUpdate.OrderID <= 0)
                    throw new Exception("One or more validation errors occurred");

                var orderExists = await DbAccessClass.OrderIDExists(orderUpdate.OrderID, _context);

                if (!orderExists)
                    throw new Exception("Order not found");

                var order = await DbAccessClass.GetOrder(orderUpdate.OrderID, _context);

                await DbAccessClass.UpdateOrder(orderUpdate, order, _context);

                return Ok("Order updated successfully");
            }
            catch (Exception ex)
            {
                return ex.Message switch
                {
                    "Order not found" => NotFound(new JsonResult(ex.Message)),
                    "One or more validation errors occurred" => UnprocessableEntity(new JsonResult(ex.Message)),
                    _ => BadRequest(new JsonResult(ex.Message)),
                };
            }
        }
Exemplo n.º 4
0
        public async Task<ActionResult> DeleteOrder([FromBody]long orderID)
        {
            try
            {
                if (orderID <= 0)
                    throw new Exception("One or more validation errors occurred");

                var orderExists = await DbAccessClass.OrderIDExists(orderID, _context);

                if (!orderExists)
                    throw new Exception("Order not found");

                var order = await DbAccessClass.GetOrder(orderID, _context);

                var deleted = await DbAccessClass.DeleteOrder(order, _context);

                if (!deleted)
                    throw new Exception("Stock amount drops below 0");

                return Ok(new JsonResult("Order deleted successfully"));
            }
            catch (Exception ex)
            {

                return ex.Message switch
                {
                    "Order not found" => NotFound(new JsonResult(ex.Message)),
                    "One or more validation errors occurred" => UnprocessableEntity(new JsonResult(ex.Message)),
                    _ => BadRequest(new JsonResult(ex.Message)),
                };
            }
        }
Exemplo n.º 5
0
        public async Task <ActionResult <ProductItem> > PostProductItem([FromBody] ProductItem productItem)
        {
            try
            {
                var foundProduct = await DbAccessClass.ProductExists(productItem, _context);

                if (foundProduct)
                {
                    throw new Exception("Product already exists");
                }

                if (productItem.Discount > 100 || productItem.Discount < 0)
                {
                    throw new Exception("Percentage out of range");
                }

                var newProduct = await DbAccessClass.AddProduct(productItem, _context, _mapper);

                return(CreatedAtAction("GetProduct", new { id = newProduct.ProductID }, Ok("Product added successfully")));
            }
            catch (Exception ex)
            {
                return(ex.Message switch
                {
                    "Product already exists" => Conflict(new JsonResult(ex.Message)),
                    _ => BadRequest(new JsonResult(ex.Message)),
                });
Exemplo n.º 6
0
        public async Task<ActionResult<IEnumerable<OrderItem>>> ListOrderItems()
        {
            try
            {
                var orders = await DbAccessClass.ListOrders(_context);

                return Ok(_mapper.Map<IEnumerable<OrderGet>>(orders));
            }
            catch (Exception ex)
            {
                return BadRequest(new JsonResult(ex.Message));
            }
        }
Exemplo n.º 7
0
        public async Task <ActionResult <string> > CheckUser([FromBody] GetUser user)
        {
            try
            {
                var userExists = await DbAccessClass.VerifyUser(user.Username, user.Password, _context);

                return(userExists ? Ok(new JsonResult($"Welcome {user.Username}")) : throw new Exception("Wrong username or password"));

                //await Task.WhenAll();

                //return (Username == "Administrator" && Password == "@dm1n")
                //    ? Ok(new JsonResult("User verified"))
                //    : throw new Exception("User not verified");
            }
            catch (Exception ex)
            {
                return(ex.Message switch
                {
                    "User not verified" => Unauthorized(new JsonResult(ex.Message)),
                    _ => BadRequest(new JsonResult(ex.Message)),
                });
Exemplo n.º 8
0
        public async Task <JsonResult> ResetDatabase()
        {
            var status = (await DbAccessClass.ClearDatabase(_context)) ? HttpContext.Response.StatusCode = 200 : HttpContext.Response.StatusCode = 500;

            return((status == 200) ? new JsonResult("API Reset") : new JsonResult("Internal server error"));
        }
Exemplo n.º 9
0
        public async Task <JsonResult> Options()
        {
            var status = (await DbAccessClass.DatabaseCheck(_context)) ? HttpContext.Response.StatusCode = 200 : HttpContext.Response.StatusCode = 500;

            return((status == 200) ? new JsonResult("API is active") : new JsonResult("Internal server error"));
        }
Exemplo n.º 10
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)),
                });