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)), }; } }
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), }; } }
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)), }; } }