public IHttpActionResult Put([FromUri] int id, OrderProductEdit updatedOrderProduct) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var service = CreateOrderProductService(); string updated = service.UpdateOrderProduct(id, updatedOrderProduct); if (updated.Contains("There is not enough inventory")) { return(BadRequest(updated)); } if (updated.Contains("finalized")) { return(BadRequest(updated)); } if (updated.Contains("The OrderProduct")) { return(Ok(updated)); } return(InternalServerError()); }
public string UpdateOrderProduct(int id, OrderProductEdit updatedOrderProduct) { using (var ctx = new ApplicationDbContext()) { OrderProduct originalOrderProduct = ctx.OrderProducts.Find(id); if (originalOrderProduct.Order.OrderFinalized) { return("This order has been finalized. You cannot make updates to it."); } Product originalProduct = ctx .Products .Single(oP => oP.ProductId == originalOrderProduct.ProductId); Product updatedProduct = ctx .Products .Single(uP => uP.ProductId == updatedOrderProduct.ProductId); if (originalOrderProduct.ProductId == updatedOrderProduct.ProductId) { if (updatedOrderProduct.ProductCount >= originalProduct.UnitCount + originalOrderProduct.ProductCount) //check if enough original product is in inventory (current inventory + whatever was in the original order) { return($"There is not enough inventory of {originalProduct.ProductName} available to update to this OrderProduct's ProductCount. The current inventory (including the ProductCount on the original OrderProduct) is {originalProduct.UnitCount + originalOrderProduct.ProductCount} units."); } originalProduct.UnitCount += originalOrderProduct.ProductCount; //return original request to inventory originalProduct.UnitCount -= updatedOrderProduct.ProductCount; //remove current request from inventory originalOrderProduct.OrderId = updatedOrderProduct.OrderId; originalOrderProduct.ProductId = updatedOrderProduct.ProductId; originalOrderProduct.ProductCount = updatedOrderProduct.ProductCount; ctx.SaveChanges(); return($"The OrderProduct ID: {id} has been updated."); } else //if original orderproduct product is different from updated orderproduct product { if (updatedOrderProduct.ProductCount >= updatedProduct.UnitCount) { return($"There is not enough inventory of the updated product: {updatedProduct.ProductName} available to update this OrderProduct's Product and ProductCount. The current {updatedProduct.ProductName} inventory is {updatedProduct.UnitCount} units."); //check if enough updated product is in inventory } originalProduct.UnitCount += originalOrderProduct.ProductCount; //return the original orderproduct to inventory (like you didn't mean to add to order) updatedProduct.UnitCount -= updatedOrderProduct.ProductCount; //Subtract new orderproduct request from new product's inventory originalOrderProduct.OrderId = updatedOrderProduct.OrderId; originalOrderProduct.ProductId = updatedOrderProduct.ProductId; originalOrderProduct.ProductCount = updatedOrderProduct.ProductCount; ctx.SaveChanges(); return($"The OrderProduct ID: {id} has been updated with the updated product: {updatedProduct.ProductName}."); } } }