コード例 #1
0
        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);
        }