예제 #1
0
        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);
        }
예제 #2
0
        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);
        }