Ejemplo n.º 1
0
        public Task Handle(ServiceRemovedEvent message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var notifier = _connectionManager.GetHubContext <Notifier>();

            notifier.Clients.All.serviceRemoved(_responseBuilder.GetServiceRemovedEvent(message));
            return(Task.FromResult(0));
        }
Ejemplo n.º 2
0
        public async Task HandleAsync(RemoveServiceFromOrder command)
        {
            if (command.ClientId is null)
            {
                throw BusinessLogicException($"Cannot remove service from order {command.OrderId} without client info");
            }

            var orderCreatedEvent = await _context.OrderCreatedEvents
                                    .Include(x => x.OrderFinishedEvent)
                                    .Include(x => x.OrderCancelledEvent)
                                    .Include(x => x.OrderPassedEvent)
                                    .Include(x => x.OrderPutInProgressEvent)
                                    .Include(x => x.ServiceAddedEvents)
                                    .ThenInclude(x => x.ServiceRemovedEvent)
                                    .Include(x => x.ServiceAddedEvents)
                                    .ThenInclude(x => x.ServiceCompletedEvent)
                                    .SingleOrDefaultAsync(x => x.Id == command.OrderId);

            if (orderCreatedEvent.ClientId != command.ClientId)
            {
                throw BusinessLogicException($"Client {command.ClientId} cannot remove service from order {command.OrderId}");
            }

            if (orderCreatedEvent.OrderCancelledEvent is not null)
            {
                throw BusinessLogicException($"Cannot remove service from cancelled order {command.OrderId}");
            }

            if (orderCreatedEvent.OrderPassedEvent is not null)
            {
                throw BusinessLogicException($"Cannot remove service from order {command.OrderId} as it has been already passed");
            }

            if (orderCreatedEvent.OrderFinishedEvent is not null)
            {
                throw BusinessLogicException($"Cannot remove service from order {command.OrderId} as it has been already finished");
            }

            if (orderCreatedEvent.OrderPutInProgressEvent is not null)
            {
                throw BusinessLogicException($"Cannot remove service from order {command.OrderId} as it has been put in progress");
            }

            var serviceAddedEvent = orderCreatedEvent.ServiceAddedEvents
                                    .SingleOrDefault(x => x.ServiceId == command.ServiceId && x.ServiceRemovedEvent is null);

            if (serviceAddedEvent.ServiceRemovedEvent is not null)
            {
                throw BusinessLogicException($"Cannot remove service {command.ServiceId} as it has been already removed");
            }

            if (serviceAddedEvent.ServiceCompletedEvent is not null)
            {
                throw BusinessLogicException($"Cannot remove service {command.ServiceId} as it has been already completed");
            }

            var serviceRemovedEvent = new ServiceRemovedEvent
            {
                ServiceAddedEventId = serviceAddedEvent.Id,
                OrderId             = command.OrderId,
                ServiceId           = command.ServiceId
            };

            _context.ServiceRemovedEvents.Add(serviceRemovedEvent);
            await _context.SaveChangesAsync();
        }