public override void Process(IEvent @event)
        {
            var current = _mobStore.Get(@event.Subject);
            var mutated = _mobMutator.Mutate(current, @event);

            _mobStore.Persist(mutated);
        }
예제 #2
0
        public async Task Handle(CreateTravel command)
        {
            if (command.AggregateId == null || command.AggregateId == Guid.Empty)
            {
                throw new IncorrectRequestException(ErrorCodes.ParameterCannotBeEmpty, nameof(command.AggregateId));
            }

            if (string.IsNullOrWhiteSpace(command.Destination))
            {
                throw new IncorrectRequestException(ErrorCodes.ParameterCannotBeEmpty, nameof(command.Destination));
            }

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

            if (travel != null)
            {
                throw new IncorrectRequestException(ErrorCodes.IdAlreadyExists, nameof(command.AggregateId));
            }

            await eventPublisher.Publish(new TravelCreated
            {
                RelatedCommandId = command.CommandId,
                AggregateVersion = 1,
                Id          = Guid.NewGuid(),
                Owner       = identityProvider.GetIdentity().Username,
                Destination = command.Destination,
                Date        = command.Date,
            });
        }
예제 #3
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);
        }
예제 #4
0
        public async Task Handle(DeleteTravel command)
        {
            if (command.AggregateId == null || command.AggregateId == Guid.Empty)
            {
                throw new IncorrectRequestException(ErrorCodes.ParameterCannotBeEmpty, nameof(command.AggregateId));
            }

            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 TravelDeleted
            {
                RelatedCommandId = command.CommandId,
                Id = command.AggregateId,
            };

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

            await eventPublisher.Publish(@event);
        }