async Task ICommandHandler <EditCustomer> .HandleAsync(EditCustomer command, CancellationToken cancellationToken) { var customer = await _entityRepository.GetEntityByIdAsync <Customer>(command.CustomerId, cancellationToken); Validate.NotNull( customer, $"Customer with ID = {command.CustomerId} does not exist."); var user = _userDataProvider.GetUserData(); Validate.UserPermission( customer.Owner, user, $"User {user.Name} is not permitted to edit customer with ID = {customer.Id}."); await _eventPublisher.PublishAsync( new CustomerUpdated( command.CustomerId, command.Name, command.Owner, command.CorrelationId, customer.SequenceNumber + 1), customer.Id, cancellationToken); }
async Task ICommandHandler <CreateTrip> .HandleAsync(CreateTrip command, CancellationToken cancellationToken) { var destinationExists = await _destinationRepository .DestinationExistsAsync(command.Destination, cancellationToken); if (!destinationExists) { throw new ValidationException($"Destination '{command.Destination}' does not exist."); } var user = _userDataProvider.GetUserData(); if (user?.Name is null) { throw new ValidationException($"Only known user can create a trip."); } var id = Guid.NewGuid(); await _eventPublisher.PublishAsync( new TripCreated( id, command.Destination, false, user.Name, command.CorrelationId, 1), id, cancellationToken); }
async Task ICommandHandler <CreateCustomer> .HandleAsync(CreateCustomer command, CancellationToken cancellationToken) { var user = _userDataProvider.GetUserData(); var id = Guid.NewGuid(); await _eventPublisher.PublishAsync( new CustomerCreated( id, command.Name, user.Name, command.CorrelationId, 1), id, cancellationToken); }
async Task ICommandHandler <AssignCustomerToTrip> .HandleAsync(AssignCustomerToTrip command, CancellationToken cancellationToken) { var trip = await _entityRepository.GetEntityByIdAsync <Trip>(command.TripId, cancellationToken); Validate.NotNull( trip, $"Trip with ID = {command.TripId} does not exist."); var user = _userDataProvider.GetUserData(); Validate.UserPermission( trip.Owner, user, $"User {user.Name} is not permitted to edit trip with ID = {command.TripId}."); var customer = await _entityRepository.GetEntityByIdAsync <Customer>(command.CustomerId, cancellationToken); Validate.NotNull( customer, $"Customer with ID = {command.CustomerId} does not exist."); Validate.UserPermission( customer.Owner, user, $"User {user.Name} is not permitted to edit customer with ID = {command.CustomerId}."); if (trip.AssignedCustomersIds.Contains(command.CustomerId)) { throw new ValidationException($"Customer with ID = {customer.Id} is already assigned to trip with ID = {trip.Id}."); } await _eventPublisher.PublishAsync( new CustomerAssignedToTrip( customer.Id, trip.Id, command.CorrelationId, trip.SequenceNumber), trip.Id, cancellationToken); }
async Task ICommandHandler <EditTrip> .HandleAsync(EditTrip command, CancellationToken cancellationToken) { var trip = await _entityRepository.GetEntityByIdAsync <Trip>(command.TripId, cancellationToken); Validate.NotNull( trip, $"Trip with ID = {command.TripId} does not exist."); var user = _userDataProvider.GetUserData(); Validate.UserPermission( trip.Owner, user, $"User {user.Name} does not have permission to edit trip with ID = {trip.Id}"); if (command.Destination != null && command.Destination != trip.Destination) { var destinationExists = await _destinationRepository .DestinationExistsAsync(command.Destination, cancellationToken); if (!destinationExists) { throw new ValidationException($"Destination '{command.Destination}' does not exist."); } } await _eventPublisher.PublishAsync( new TripUpdated( command.TripId, command.Destination, command.IsCancelled, command.Owner, command.CorrelationId, trip.SequenceNumber + 1), trip.Id, cancellationToken); }