public async Task HandleAsync(CreateSubmission command)
        {
            var callForPapers = await _callForPapersRepository.GetAsync(command.ConferenceId);

            if (callForPapers is null)
            {
                throw new CallForPapersNotFoundException(command.ConferenceId);
            }

            if (!callForPapers.IsOpened)
            {
                throw new CallForPapersClosedException(command.ConferenceId);
            }

            var speakerIds = command.SpeakerIds.Select(id => new AggregateId(id));
            var speakers   = await _speakerRepository.BrowseAsync(speakerIds);

            if (speakers.Count() != command.SpeakerIds.Count())
            {
                throw new MissingSubmissionSpeakersException(command.Id);
            }

            var submission = Submission.Create(command.Id, command.ConferenceId, command.Title, command.Description,
                                               command.Level, command.Tags, speakers);

            await _submissionRepository.AddAsync(submission);

            await _dispatcher.DispatchAsync(submission.Events.ToArray());

            var integrationEvents = _eventMapper.MapAll(submission.Events);
            await _messageBroker.PublishAsync(integrationEvents.ToArray());
        }
Example #2
0
        public async Task HandleAsync(DeleteParcelFromOrder command)
        {
            var order = await _orderRepository.GetAsync(command.OrderId);

            if (order is null)
            {
                throw new OrderNotFoundException(command.OrderId);
            }

            var identity = _appContext.Identity;

            if (identity.IsAuthenticated && identity.Id != order.CustomerId && !identity.IsAdmin)
            {
                throw new UnauthorizedOrderAccessException(order.Id, identity.Id);
            }

            if (!order.DeleteParcel(command.ParcelId))
            {
                throw new ParcelAlreadyDeletedFromOrderException(command.OrderId, command.ParcelId);
            }

            await _orderRepository.UpdateAsync(order);

            var events = _eventMapper.MapAll(order.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
        public async Task HandleAsync(AddParcelToOrder command)
        {
            var order = await _orderRepository.GetContainingParcelAsync(command.ParcelId);

            if (!(order is null))
            {
                throw new ParcelAlreadyAddedToOrderException(command.OrderId, command.ParcelId);
            }

            order = await _orderRepository.GetAsync(command.OrderId);

            if (order is null)
            {
                throw new OrderNotFoundException(command.OrderId);
            }

            var parcel = await _parcelsServiceClient.GetAsync(command.ParcelId);

            if (parcel is null)
            {
                throw new ParcelNotFoundException(command.ParcelId);
            }

            if (!order.AddParcel(new Parcel(parcel.Id, parcel.Name, parcel.Variant, parcel.Size)))
            {
                return;
            }

            await _orderRepository.UpdateAsync(order);

            var events = _eventMapper.MapAll(order.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
        public async Task Handle(BookRentalCarCommand message, IMessageHandlerContext context)
        {
            lock (Console.Out)
            {
                if (context is IMessageBrokerContext messageBrokerContext)
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine($"Received '{message.GetType().Name}' command from message broker. Message Id: '{messageBrokerContext.BrokeredMessage.MessageId}', Subscription: '{messageBrokerContext.BrokeredMessage.MessageReceiverPath}'");
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine($"Received '{message.GetType().Name}' command from local dispatcher");
                }
                Console.WriteLine($"Command Data: {JsonConvert.SerializeObject(message)}");
                Console.ResetColor();
            }

            var carRental     = new Domain.Aggregates.CarRental(); //TODO: just an example - likely more properties would be passed in via ctor
            var reservationId = carRental.Reserve(message.Car.Airport, message.Car.Vendor, message.Car.From, message.Car.Until);
            await _repository.AddAsync(carRental);

            var integrationEvents = _eventMapper.MapAll(carRental.DomainEvents);

            //throw new Exception("without using the outbox, we're in a bad state. aggregate has been saved, integration events will never be published.");

            await context.Publish(integrationEvents);

            lock (Console.Out)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine($"Dispatched domain events: {JsonConvert.SerializeObject(integrationEvents)}");
                Console.ResetColor();
            }
        }
        public async Task HandleAsync(CreateOrder command)
        {
            var order = new Order(command.Id, command.CustomerId, OrderStatus.New, _dateTimeProvider.Now);
            await _orderRepository.AddAsync(order);

            var events = _eventMapper.MapAll(order.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
Example #6
0
 public async Task HandleAsync(StartDelivery command)
 {
     var delivery = Delivery.Create(command.DeliveryId, command.OrderId, DeliveryStatus.InProgress);
     delivery.AddRegistration(new DeliveryRegistration(command.Description, command.DateTime));
     await _repository.AddAsync(delivery);
     var events = _eventMapper.MapAll(delivery.Events);
     await _messageBroker.PublishAsync(events.ToArray());
 }
Example #7
0
        public async Task HandleAsync(AddResource command)
        {
            if (await _repository.ExistsAsync(command.ResourceId))
            {
                throw new ResourceAlreadyExistsException(command.ResourceId);
            }

            var resource = Resource.Create(command.ResourceId);
            await _repository.AddAsync(resource);

            var events = _eventMapper.MapAll(resource.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
Example #8
0
        public async Task HandleAsync(CreateOrder command)
        {
            if (!(await _customerRepository.ExistsAsync(command.CustomerId)))
            {
                throw new CustomerNotFoundException(command.CustomerId);
            }

            var order = Order.Create(command.OrderId, command.CustomerId, OrderStatus.New, _dateTimeProvider.Now);
            await _orderRepository.AddAsync(order);

            var events = _eventMapper.MapAll(order.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
Example #9
0
        public async Task HandleAsync(CompleteDelivery command)
        {
            var delivery = await _repository.GetAsync(command.DeliveryId);

            if (delivery is null)
            {
                throw new DeliveryNotFoundException(command.DeliveryId);
            }

            delivery.Complete();
            await _repository.UpdateAsync(delivery);

            var events = _eventMapper.MapAll(delivery.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
Example #10
0
        public async Task HandleAsync(DeleteResource command)
        {
            var resource = await _repository.GetAsync(command.ResourceId);

            if (resource is null)
            {
                throw new ResourceNotFoundException(command.ResourceId);
            }

            resource.Delete();
            await _repository.DeleteAsync(resource.Id);

            var events = _eventMapper.MapAll(resource.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
Example #11
0
        public async Task ProcessAsync(IEnumerable <IDomainEvent> events)
        {
            if (events is null)
            {
                return;
            }
            var appEvents = _eventMapper.MapAll(events);

            if (!appEvents.Any())
            {
                return;
            }
            _logger.LogTrace("publishing event");
            await _messageBroker.PublishAsync(appEvents);
        }
Example #12
0
        public async Task HandleAsync(ResourceReserved @event)
        {
            var order = await _orderRepository.GetAsync(@event.ResourceId, @event.DateTime);

            if (order is null)
            {
                throw new OrderForReservedVehicleNotFoundException(@event.ResourceId, @event.DateTime);
            }

            order.Approve();
            await _orderRepository.UpdateAsync(order);

            var events = _eventMapper.MapAll(order.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
        public async Task HandleAsync(CancelOrder command)
        {
            var order = await _orderRepository.GetAsync(command.Id);

            if (order is null)
            {
                throw new OrderNotFoundException(command.Id);
            }

            order.Cancel(command.Reason);
            await _orderRepository.UpdateAsync(order);

            var events = _eventMapper.MapAll(order.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
Example #14
0
        public async Task HandleAsync(ParcelDeleted @event)
        {
            var order = await _orderRepository.GetContainingParcelAsync(@event.ParcelId);

            if (order is null)
            {
                return;
            }

            order.DeleteParcel(@event.ParcelId);
            await _orderRepository.UpdateAsync(order);

            var events = _eventMapper.MapAll(order.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
Example #15
0
        public async Task HandleAsync(DeliveryCompleted @event)
        {
            var order = await _orderRepository.GetAsync(@event.OrderId);

            if (order is null)
            {
                throw new OrderNotFoundException(@event.OrderId);
            }

            order.Complete();
            await _orderRepository.UpdateAsync(order);

            var events = _eventMapper.MapAll(order.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
Example #16
0
        public async Task HandleAsync(ReleaseResource command)
        {
            var resource = await _repository.GetAsync(command.ResourceId);

            if (resource is null)
            {
                throw new ResourceNotFoundException(command.ResourceId);
            }

            var reservation = resource.Reservations.FirstOrDefault(r => r.DateTime.Date == command.DateTime.Date);

            resource.ReleaseReservation(reservation);
            await _repository.UpdateAsync(resource);

            var events = _eventMapper.MapAll(resource.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
Example #17
0
        public async Task HandleAsync(ChangeCustomerState command)
        {
            var customer = await _customerRepository.GetAsync(command.CustomerId);

            if (customer is null)
            {
                throw new CustomerNotFoundException(command.CustomerId);
            }

            if (!Enum.TryParse <State>(command.State, true, out var state))
            {
                throw new CannotChangeCustomerStateException(customer.Id, State.Unknown);
            }

            if (customer.State == state)
            {
                return;
            }

            switch (state)
            {
            case State.Valid:
                customer.SetValid();
                break;

            case State.Incomplete:
                customer.SetIncomplete();
                break;

            case State.Suspicious:
                customer.MarkAsSuspicious();
                break;

            case State.Locked:
                customer.Lock();
                break;

            default:
                throw new CannotChangeCustomerStateException(customer.Id, state);
            }

            await _customerRepository.UpdateAsync(customer);

            var events = _eventMapper.MapAll(customer.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
Example #18
0
        public async Task HandleAsync(AddDeliveryRegistration command)
        {
            var delivery = await _repository.GetAsync(command.DeliveryId);

            if (delivery is null)
            {
                throw new DeliveryNotFoundException(command.DeliveryId);
            }

            delivery.AddRegistration(new DeliveryRegistration(command.Description, command.DateTime));
            if (delivery.HasChanged)
            {
                await _repository.UpdateAsync(delivery);

                var events = _eventMapper.MapAll(delivery.Events);
                await _messageBroker.PublishAsync(events.ToArray());
            }
        }
        public async Task HandleAsync(RejectSubmission command)
        {
            var submission = await _repository.GetAsync(command.Id);

            if (submission is null)
            {
                throw new SubmissionNotFoundException(command.Id);
            }

            submission.Reject();

            await _repository.UpdateAsync(submission);

            await _dispatcher.DispatchAsync(submission.Events.ToArray());

            var integrationEvents = _eventMapper.MapAll(submission.Events);
            await _messageBroker.PublishAsync(integrationEvents.ToArray());
        }
Example #20
0
        public async Task HandleAsync(OrderCompleted @event)
        {
            var customer = await _customerRepository.GetAsync(@event.CustomerId);

            if (customer is null)
            {
                throw new CustomerNotFoundException(@event.CustomerId);
            }

            var isVip = customer.IsVip;

            customer.AddCompletedOrder(@event.OrderId);
            _vipPolicy.ApplyVipStatusIfEligible(customer);
            var vipApplied = !isVip && customer.IsVip;
            await _customerRepository.UpdateAsync(customer);

            var events = _eventMapper.MapAll(customer.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
Example #21
0
        public async Task HandleAsync(DeleteParcelFromOrder command)
        {
            var order = await _orderRepository.GetAsync(command.OrderId);

            if (order is null)
            {
                throw new OrderNotFoundException(command.OrderId);
            }

            if (!order.DeleteParcel(command.ParcelId))
            {
                return;
            }

            await _orderRepository.UpdateAsync(order);

            var events = _eventMapper.MapAll(order.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
Example #22
0
        public async Task HandleAsync(ApproveSubmission command)
        {
            var submission = await _submissionRepository.GetAsync(command.Id);

            if (submission is null)
            {
                throw new SubmissionNotFoundException(command.Id);
            }

            submission.Approve();
            await _submissionRepository.UpdateAsync(submission);

            var events = _eventMapper.MapAll(submission.Events);
            await _messageBroker.PublishAsync(events.ToArray());



            await _domainEventDispatcher.DispatchAsync(submission.Events.ToArray());
        }
Example #23
0
        public async Task HandleAsync(CompleteCustomerRegistration command)
        {
            var customer = await _customerRepository.GetAsync(command.CustomerId);

            if (customer is null)
            {
                throw new CustomerNotFoundException(command.CustomerId);
            }

            if (customer.State is State.Valid)
            {
                throw new CannotChangeCustomerStateException(command.CustomerId, State.Valid);
            }

            customer.CompleteRegistration(command.FullName, command.Address);
            await _customerRepository.UpdateAsync(customer);

            var events = _eventMapper.MapAll(customer.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
Example #24
0
        public async Task HandleAsync(CreateSubmission command)
        {
            var speakersIds = command.SpeakersIds.Select(x => new AggregateId(x));
            var speakers    = await _speakerRepository.BrowserAsync(speakersIds);

            if (speakers.Count() != speakersIds.Count())
            {
                throw new InvalidSpeakerNumberException(command.Id);
            }

            var submission = Submission.Create(command.Id, command.ConferenceId, command.Title, command.Description,
                                               command.Level, command.Tags, speakers.ToList());

            await _submissionRepository.AddAsync(submission);

            var events = _eventMapper.MapAll(submission.Events);
            await _messageBroker.PublishAsync(events.ToArray());

            await _domainEventDispatcher.DispatchAsync(submission.Events.ToArray());
        }
        public async Task HandleAsync(AddParcelToOrder command)
        {
            var order = await _orderRepository.GetAsync(command.OrderId);

            if (order is null)
            {
                throw new OrderNotFoundException(command.OrderId);
            }

            ValidateAccessOrFail(order);
            var parcel = await _parcelsServiceClient.GetAsync(command.ParcelId);

            if (parcel is null)
            {
                throw new ParcelNotFoundException(command.ParcelId);
            }

            order.AddParcel(new Parcel(parcel.Id, parcel.Name, parcel.Variant, parcel.Size));
            await _orderRepository.UpdateAsync(order);

            var events = _eventMapper.MapAll(order.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
Example #26
0
        public async Task HandleAsync(ReserveResource command)
        {
            var identity = _appContext.Identity;

            if (identity.IsAuthenticated && identity.Id != command.CustomerId && !identity.IsAdmin)
            {
                throw new UnauthorizedResourceAccessException(command.ResourceId, identity.Id);
            }

            var resource = await _repository.GetAsync(command.ResourceId);

            if (resource is null)
            {
                throw new ResourceNotFoundException(command.ResourceId);
            }

            var customerState = await _customersServiceClient.GetStateAsync(command.CustomerId);

            if (customerState is null)
            {
                throw new CustomerNotFoundException(command.CustomerId);
            }

            if (!customerState.IsValid)
            {
                throw new InvalidCustomerStateException(command.ResourceId, customerState?.State);
            }

            var reservation = new Reservation(command.DateTime, command.Priority);

            resource.AddReservation(reservation);
            await _repository.UpdateAsync(resource);

            var events = _eventMapper.MapAll(resource.Events);
            await _messageBroker.PublishAsync(events.ToArray());
        }
 protected async Task PublishEventsAsync(AggregateRoot aggregateRoot)
 {
     var events = _eventMapper.MapAll(aggregateRoot.Events);
     await _messageBroker.PublishAsync(events.ToArray());
 }