Exemplo n.º 1
0
        public async Task Handle(EditTravel command)
        {
            if (command.AggregateId == null || command.AggregateId == Guid.Empty)
            {
                throw new IncorrectRequestException(ErrorCodes.ParameterCannotBeEmpty, nameof(command.AggregateId));
            }

            if (string.IsNullOrWhiteSpace(command.Destination) && !command.Date.HasValue)
            {
                throw new IncorrectRequestException(ErrorCodes.ParameterCannotBeEmpty, $"{nameof(command.Destination)}, {nameof(command.Date)}");
            }

            Models.Travel travel = await store.Get(command.AggregateId);

            if (travel == null || travel.Deleted)
            {
                throw new IncorrectRequestException(ErrorCodes.ResourceDoesNotExist, nameof(travel));
            }

            if (travel.Version != command.AggregateVersion)
            {
                throw new ResourceStateChangedException(nameof(Travel), travel.Id, travel.Version);
            }

            Identity identity = identityProvider.GetIdentity();

            if (travel.Owner != identity.Username && identity.Role != Roles.Admin)
            {
                throw new UnauthorizedUserException();
            }

            var @event = new TravelUpdated
            {
                RelatedCommandId = command.CommandId,
                Id          = travel.Id,
                Destination = string.IsNullOrWhiteSpace(command.Destination) ? travel.Destination : command.Destination,
                Date        = command.Date.HasValue ? command.Date.Value : travel.Date,
            };

            travel.ApplyEvent(@event);
            @event.AggregateVersion = travel.Version;

            await eventPublisher.Publish(@event);
        }
Exemplo n.º 2
0
 public void Apply(TravelUpdated @event)
 {
     Destination = @event.Destination;
     Date        = @event.Date;
 }