Exemplo n.º 1
0
        public Shared.Travel GetTravelsById(int id)
        {
            IQueryable <Models.Travel> list = _AirTechContext.Travel.Where(t => t.Id == id);

            Models.Travel t = list.FirstOrDefault <Models.Travel>();
            return(ConvertToEndPoint(ConvertToBusiness(t)));
        }
Exemplo n.º 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,
            });
        }
Exemplo n.º 3
0
 public static Business.Travel ConvertToBusiness(Models.Travel model)
 {
     return(new Business.Travel
     {
         From = model.From,
         To = model.To,
         Price = model.Price,
         Id = model.Id,
         Stock = model.Stock,
         LuggageStock = model.LuggageStock
     });
 }
Exemplo n.º 4
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.º 5
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);
        }