public IActionResult Post([FromBody] Order order) { if (order == null) { return(BadRequest()); } // Get the Product from Product API var orderedProduct = productServiceGateway.Get(order.ProductId); // Get the Customer from Customer API var customer = customerServiceGateway.Get(order.CustomerId); if (customer == null) { messagePublisher.PublishCustomerExist("CustomerNotification"); } if (order.Quantity <= orderedProduct.ItemsInStock - orderedProduct.ItemsReserved) { /* * // reduce the number of items in stock for the ordered product, * // and create a new order. * orderedProduct.ItemsReserved += order.Quantity; * var updateRequest = new RestRequest(orderedProduct.Id.ToString(), Method.PUT); * updateRequest.AddJsonBody(orderedProduct); * var updateResponse = c.Execute(updateRequest); */ if (orderedProduct.Price <= customer.CreditStanding) { try { // Publish OrderStatusChangedMessage. If this operation // fails, the order will not be created messagePublisher.PublishOrderStatusChangedMessage(order.ProductId, order.Quantity, "orderCompleted"); messagePublisher.PublishCustomerActivity(customer.Id, order.Id, "orderNotifiedCustomer"); // Create order. order.Status = Order.OrderStatus.completed; var newOrder = repository.Add(order); return(CreatedAtRoute("GetOrder", new { id = newOrder.Id }, newOrder)); } catch { return(StatusCode(500, "An error happened. Try again.")); } } else { return(StatusCode(500, "Your money is not enough to buy this product. Sorry.")); } } else { // If the order could not be created, "return no content". return(StatusCode(500, "Not enough items in stock.")); } }