public async Task <InvoiceReadModel> GetInvoice(string invoiceId) { Invoice invoice = await _eventRepository.GetByIdAsync(new InvoiceId(invoiceId)); var taskInvoiceLines = Task.WhenAll(invoice.Lines.Select(async x => { var getShipTask = _shipRepository.GetShip(x.ShipId.ToString()); var getServiceTask = _shipServiceRepository.GetShipService(x.ServiceId.ToString()); await Task.WhenAll(getShipTask, getServiceTask); Ship ship = getShipTask.Result; ShipService service = getServiceTask.Result; return(new InvoiceLineReadModel { Description = $"Service: {service.Name} applied for ship: {ship.Name}", Price = service.Price }); })); var taskCustomer = Task.Run(async() => { Customer foundCustomer = await _customerRepository.GetCustomerAsync(invoice.CustomerId.ToString()); CustomerReadModel readModel = new CustomerReadModel { Address = foundCustomer.Address, Email = foundCustomer.Email, PostalCode = foundCustomer.PostalCode, Residence = foundCustomer.Residence }; return(readModel); }); var taskRental = Task.Run(async() => { Rental foundRental = await _rentalRepository.GetRental(invoice.RentalId.ToString()); RentalReadModel readModel = new RentalReadModel { Price = foundRental.Price }; return(readModel); }); await Task.WhenAll(taskInvoiceLines, taskCustomer, taskRental); var customer = await taskCustomer; var lines = await taskInvoiceLines; var rental = await taskRental; return(new InvoiceReadModel { Customer = customer, Lines = lines, Rental = rental, TotalPrice = rental.Price + lines.Sum(x => x.Price) }); }
private async Task <bool> HandleServiceRequested(string message) { var receivedShipService = JsonSerializer.Deserialize <ServiceRequest>(message); var ship = await _shipRepository.GetShip(receivedShipService.ShipId); if (ship != null) { var trucks = await _truckRepository.GetTrucks(); var containersToSort = trucks.Select(x => x.Container).ToArray(); await _containerRepository.SortContainersAsync(containersToSort); foreach (var truck in trucks) { var selectedTruck = trucks?.FirstOrDefault(); if (selectedTruck != null) { if (receivedShipService.ServiceId == ShipServiceConstants.LoadContainerId) { await _containerRepository.LoadContainerAsync(selectedTruck.Container); await _messagePublisher.PublishMessageAsync(MessageTypes.ShipContainerLoaded, selectedTruck); await PublishServiceCompleteAndDeleteTruck(receivedShipService, selectedTruck); } if (receivedShipService.ServiceId == ShipServiceConstants.UnloadContainerId) { await _containerRepository.UnloadContainerAsync(selectedTruck.Container); await _messagePublisher.PublishMessageAsync(MessageTypes.ShipContainerUnloaded, selectedTruck); await PublishServiceCompleteAndDeleteTruck(receivedShipService, selectedTruck); } } } } return(true); }
public Task <Ship> GetShipAsync(Guid shipId) { return(_shipRepository.GetShip(shipId)); }