private bool ProductItemsAvailable(SharedOrders order) { try { foreach (var orderLine in order.OrderLines) { // Call product service to get the product ordered. // var orderedProduct = productGateway.Get(orderLine.ProductId); // Publish PublishShareProducts. If this operation // fails, the order will not be created messagePublisher.PublishSharedProducts( orderLine.ProductId, "available"); //if (orderLine.Quantity > orderedProduct.ItemsInStock - orderedProduct.ItemsReserved) //{ // return false; //} } return(true); } catch { return(false); } }
public SharedOrders Add(SharedOrders entity) { if (entity.Date == null) { entity.Date = DateTime.Now; } var newOrder = db.Orders.Add(entity).Entity; db.SaveChanges(); return(newOrder); }
public IActionResult Post([FromBody] SharedOrders order) { if (order == null || order.customerId < 1) { return(BadRequest()); } //if (ProductItemsAvailable(order) && CustomerExists(order)) if (CustomerExists(order)) { try { foreach (var item in order.OrderLines) { if (!messagePublisher.ProductExists(item.ProductId, item.Quantity)) { return(StatusCode(500, "Not enough items in stock.")); } } // Publish OrderStatusChangedMessage. If this operation // fails, the order will not be created messagePublisher.PublishOrderStatusChangedMessage( order.customerId, order.OrderLines, "completed"); // Create order. order.Status = SharedOrders.OrderStatus.completed; var newOrder = repository.Add(order); return(CreatedAtRoute("GetOrder", new { id = newOrder.Id }, newOrder)); } catch (Exception e) { return(StatusCode(500, "An error happened. Try again." + e)); } } else { // If there are not enough product items available. return(StatusCode(500, "Not enough items in stock.")); } }
private bool CustomerExists(SharedOrders orders) { return(messagePublisher.PublishCustomerExists(orders.customerId)); }
public void Edit(SharedOrders entity) { db.Entry(entity).State = EntityState.Modified; db.SaveChanges(); }