public async Task Consume(ConsumeContext <OrderTotalsCalculated> consumeContext) { var seatTypeIds = consumeContext.Message.Lines.OfType <SeatOrderLine>().Select(x => x.SeatType).Distinct() .ToArray(); await using var repository = this.contextFactory.Invoke(); var dto = await repository.Set <PricedOrder>().Include(x => x.Lines) .FirstAsync(x => x.OrderId == consumeContext.Message.SourceId); if (!WasNotAlreadyHandled(dto, consumeContext.Message.Version)) { // message already handled, skip. return; } var linesSet = repository.Set <PricedOrderLine>(); foreach (var line in dto.Lines.ToList()) { linesSet.Remove(line); } var seatTypeDescriptions = await GetSeatTypeDescriptions(seatTypeIds, repository); for (int i = 0; i < consumeContext.Message.Lines.Length; i++) { var orderLine = consumeContext.Message.Lines[i]; var line = new PricedOrderLine { LineTotal = orderLine.LineTotal, Position = i, }; if (orderLine is SeatOrderLine seatOrderLine) { // should we update the view model to avoid loosing the SeatTypeId? line.Description = seatTypeDescriptions.Where(x => x.SeatTypeId == seatOrderLine.SeatType) .Select(x => x.Name).FirstOrDefault(); line.UnitPrice = seatOrderLine.UnitPrice; line.Quantity = seatOrderLine.Quantity; } dto.Lines.Add(line); } dto.Total = consumeContext.Message.Total; dto.IsFreeOfCharge = consumeContext.Message.IsFreeOfCharge; dto.OrderVersion = consumeContext.Message.Version; await repository.SaveChangesAsync(); }
public void Handle(OrderTotalsCalculated @event) { var seatTypeIds = @event.Lines.OfType <SeatOrderLine>().Select(x => x.SeatType).Distinct().ToArray(); using (var context = this.contextFactory.Invoke()) { var dto = context.Query <PricedOrder>().Include(x => x.Lines).First(x => x.OrderId == @event.SourceId); if (!WasNotAlreadyHandled(dto, @event.Version)) { // message already handled, skip. return; } var linesSet = context.Set <PricedOrderLine>(); foreach (var line in dto.Lines.ToList()) { linesSet.Remove(line); } var seatTypeDescriptions = GetSeatTypeDescriptions(seatTypeIds, context); for (int i = 0; i < @event.Lines.Length; i++) { var orderLine = @event.Lines[i]; var line = new PricedOrderLine { LineTotal = orderLine.LineTotal, Position = i, }; var seatOrderLine = orderLine as SeatOrderLine; if (seatOrderLine != null) { // should we update the view model to avoid losing the SeatTypeId? line.Description = seatTypeDescriptions.Where(x => x.SeatTypeId == seatOrderLine.SeatType).Select(x => x.Name).FirstOrDefault(); line.UnitPrice = seatOrderLine.UnitPrice; line.Quantity = seatOrderLine.Quantity; } dto.Lines.Add(line); } dto.Total = @event.Total; dto.IsFreeOfCharge = @event.IsFreeOfCharge; dto.OrderVersion = @event.Version; context.SaveChanges(); } }
public void Handle(OrderTotalsCalculated @event) { var seatTypeIds = @event.Lines.OfType <SeatOrderLine>().Select(x => x.SeatType).Distinct().ToArray(); var dto = pricedOrderRepository.GetBy(x => x.OrderId == @event.SourceId).FirstOrDefault(); if (!WasNotAlreadyHandled(dto, @event.Version)) { // message already handled, skip. return; } //var seatTypeDescriptions = GetSeatTypeDescriptions(seatTypeIds, context); dto.Lines = new List <PricedOrderLine>(); for (int i = 0; i < @event.Lines.Length; i++) { var orderLine = @event.Lines[i]; var line = new PricedOrderLine { LineTotal = orderLine.LineTotal, Position = i, }; var seatOrderLine = orderLine as SeatOrderLine; if (seatOrderLine != null) { // should we update the view model to avoid losing the SeatTypeId? line.Description = seatTypeRepository.GetByKey(seatOrderLine.SeatType).Name; line.UnitPrice = seatOrderLine.UnitPrice; line.Quantity = seatOrderLine.Quantity; } dto.Lines.Add(line); } dto.Total = @event.Total; dto.IsFreeOfCharge = @event.IsFreeOfCharge; dto.OrderVersion = @event.Version; pricedOrderRepository.Update(dto); }