private Dictionary <Guid, List <IEvent> > AddOrderLines(OrderAggregate orderAggregate, IEnumerable <OrderLineDto> orderLines) { IEnumerable <WarehouseView> warehouses = repository.Query <WarehouseView, WarehouseView>((w) => new WarehouseView { Id = w.Id, Items = w.Items }); var events = new Dictionary <Guid, List <IEvent> >(); foreach (OrderLineDto orderLine in orderLines) { WarehouseView warehouse = warehouses.FirstOrDefault(w => w.Items.Any(item => item.ProductId == orderLine.ProductNumber && item.Quantity >= orderLine.Quantity)); var orderItem = new OrderLine { ProductId = orderLine.ProductNumber, Quantity = orderLine.Quantity }; if (warehouse != null) { WarehouseAggregate warehouseAggregate = repository.Load <WarehouseAggregate>(warehouse.Id); warehouseAggregate.SubstractItemQuantity(orderLine.ProductNumber, orderLine.Quantity); AddAggEvents(events, warehouseAggregate.Id, warehouseAggregate.PendingEvents.ToList()); orderItem.WarehouseId = warehouseAggregate.Id; } else { orderItem.Status = OrderLineStatus.OutOfStock.ToString(); } orderAggregate.AddLine(orderItem); AddAggEvents(events, orderAggregate.Id, orderAggregate.PendingEvents.ToList()); } return(events); }
public void Replicate(DateTime?timestamp = null) { IEnumerable <Guid> streamIds = aggregationRepository.Query <TView, Guid>((w) => w.Id); foreach (Guid streamId in streamIds) { IEnumerable <IEvent> events = aggregationRepository.GetEvents(streamId, timestamp: timestamp); var aggEvents = new Dictionary <Guid, List <IEvent> >(); aggEvents.Add(streamId, events.ToList()); Action transactionPostProcessFunc = () => aggEvents.SelectMany(agg => agg.Value).ToList().ForEach(@event => readStorageSyncEventBus.Send(@event)); aggregationReplicaRepository.Transaction(() => aggEvents, transactionPostProcessFunc); logger.Log($"### StreamId: {streamId}"); } }
private static IEnumerable <OrderReadView> GetOrder(IAggregationRepository originalAggregationRepostory) { return(originalAggregationRepostory.Query <OrderView, OrderReadView>((w) => new OrderReadView { Id = w.Id })); }