public IActionResult PlaceOrder([FromBody] OrderDetails orderDetails) { try { if (orderDetails != null) { var UserId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; orderDetails.OrderBy = int.Parse(UserId); var product = _dbContext.Products.SingleOrDefault(x => x.ProductId == orderDetails.ProductId); var availableQuantity = product.Quantity; var appliedQuantity = orderDetails.Quantity; if (appliedQuantity > availableQuantity) { var response = new ReturnResultStatus(Status.Failed, "Product Quantity Applied", "Product available quantity is less then product applied quantity."); return(Ok(response.Message)); } else if (appliedQuantity == availableQuantity) { _dbContext.Orders.Add(orderDetails); _dbContext.SaveChanges(); var data = _dbContext.Products.SingleOrDefault(x => x.ProductId == orderDetails.ProductId); _dbContext.Remove(data); _dbContext.SaveChanges(); var response = new ReturnResultStatus(Status.Success, "Order Purchase", "Order placed successfully."); return(Ok(response.Message)); } else if (appliedQuantity < availableQuantity) { _dbContext.Orders.Add(orderDetails); _dbContext.SaveChanges(); var data = _dbContext.Products.SingleOrDefault(x => x.ProductId == orderDetails.ProductId); var CurrentQuantity = availableQuantity - appliedQuantity; data.Quantity = CurrentQuantity; _dbContext.Update(data); _dbContext.SaveChanges(); var response = new ReturnResultStatus(Status.Success, "Order Purchase", "Order placed successfully."); return(Ok(response.Message)); } } } catch (Exception ex) { return(BadRequest(new { message = ex.Message })); } return(null); }
public async Task <IActionResult> DeleteProduct([FromBody] ProductDetails productDetails) { try { var result = await _dbContext.Products.Where(x => x.ProductId == productDetails.ProductId).SingleOrDefaultAsync(); if (result != null) { _dbContext.Remove(result); _dbContext.SaveChanges(); var response = new ReturnResultStatus(Status.Success, "Product Deleted", "Product deleted successfully."); return(Ok(response.Message)); } else { var response = new ReturnResultStatus(Status.Failed, "Product not found", "Unable to delete Product."); return(Ok(response.Message)); } } catch (Exception ex) { return(BadRequest(new { message = ex.Message })); } }