public async Task HandleAsync(ReserveResource command)
        {
            var identity = _appContext.Identity;

            if (identity.IsAuthenticated && identity.Id != command.CustomerId && !identity.IsAdmin)
            {
                throw new UnauthorizedResourceAccessException(command.ResourceId, identity.Id);
            }

            var resource = await _repository.GetAsync(command.ResourceId);

            if (resource is null)
            {
                throw new ResourceNotFoundException(command.ResourceId);
            }

            var customerState = await _customersServiceClient.GetStateAsync(command.CustomerId);

            if (customerState is null)
            {
                throw new CustomerNotFoundException(command.CustomerId);
            }

            if (!customerState.IsValid)
            {
                throw new InvalidCustomerStateException(command.ResourceId, customerState?.State);
            }

            var reservation = new Reservation(command.DateTime, command.Priority);

            resource.AddReservation(reservation);
            await _repository.UpdateAsync(resource);

            await _eventProcessor.ProcessAsync(resource.Events);
        }
Пример #2
0
        public async Task HandleAsync(AddResource command)
        {
            if (await _repository.ExistsAsync(command.ResourceId))
            {
                throw new ResourceAlreadyExistsException(command.ResourceId);
            }

            var resource = Resource.Create(command.ResourceId, command.Tags);
            await _repository.AddAsync(resource);

            await _eventProcessor.ProcessAsync(resource.Events);
        }
Пример #3
0
        public async Task <Unit> Handle(CreateStockItemCommand request, CancellationToken cancellationToken)
        {
            if (await _repository.ExistsAsync(request.Id))
            {
                throw new ItemAlreadyExistsException(request.Id);
            }
            var item = Domain.Entities.Item.Create(request.Id, request.Amount);
            await _repository.AddAsync(item);

            await _eventProcessor.ProcessAsync(item.Events);

            return(Unit.Value);
        }
        public async Task HandleAsync(AddTrip command)
        {
            if (await _tripRepository.ExistsAsync(command.TripId))
            {
                throw new TripAlreadyExistsException(command.TripId);
            }

            var trip = Core.Entities.Trip.Create(command.TripId, command.Destination, command.Date,
                                                 command.DifficultyLevel);
            await _tripRepository.AddAsync(trip);

            await _eventProcessor.ProcessAsync(trip.Events);
        }
Пример #5
0
        public async Task HandleAsync(JoinToTrip command)
        {
            var trip = await _tripRepository.GetAsync(command.TripId);

            var participant = new Participant(command.UserId);

            trip.AddParticipant(participant);
            await _tripRepository.UpdateAsync(trip);

            var trip2 = await _tripRepository.GetAsync(command.TripId);

            await _eventProcessor.ProcessAsync(trip.Events);
        }
Пример #6
0
        private async Task ChangeReservationStatus(Guid id, ReservationStatus status)
        {
            var reservation = await _repository.GetAsync(id);

            if (reservation is null)
            {
                throw new ReservationNotFoundException(id);
            }

            reservation.ChangeStatus(status);
            await _repository.UpdateAsync(reservation);

            await _processor.ProcessAsync(reservation.DomainEvents);
        }
Пример #7
0
        public async Task HandleAsync(CreateScheduleSchema command)
        {
            var alreadyExists = await _repository.ExistsAsync(command.CinemaId);

            if (alreadyExists)
            {
                throw new ScheduleSchemaAlreadyExistsException(command.CinemaId);
            }

            var scheduleSchema = ScheduleSchema.Create(command.Id, command.CinemaId, command.Times.AsScheduleSchemaTimes());
            await _repository.AddAsync(scheduleSchema);

            await _processor.ProcessAsync(scheduleSchema.DomainEvents);
        }
Пример #8
0
        public async Task <Unit> Handle(CreateItemCommand request, CancellationToken cancellationToken)
        {
            if (await _repository.ExistsAsync(request.Id))
            {
                throw new ItemAlreadyExistsException(request.Id);
            }
            request.Id = request.Id == Guid.Empty ? Guid.NewGuid() : request.Id;
            var item = Item.Create(request.Id, request.Category, request.Name, request.Description, request.Tags, request.UnitPrice);
            await _repository.AddAsync(item);

            await _eventProcessor.ProcessAsync(item.Events);

            return(Unit.Value);
        }
Пример #9
0
        public async Task HandleAsync(DeleteResource command)
        {
            var resource = await _repository.GetAsync(command.ResourceId);

            if (resource is null)
            {
                throw new ResourceNotFoundException(command.ResourceId);
            }

            resource.Delete();
            await _repository.DeleteAsync(resource.Id);

            await _eventProcessor.ProcessAsync(resource.Events);
        }
Пример #10
0
        public async Task <Unit> Handle(DeleteStockItemCommand request, CancellationToken cancellationToken)
        {
            var item = await _repository.GetAsync(request.Id);

            if (item is null)
            {
                throw new ItemNotFoundException(request.Id);
            }
            item.Delete(item);
            await _repository.DeleteAsync(item.Id);

            await _eventProcessor.ProcessAsync(item.Events);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(StockItemStockDownCommand request, CancellationToken cancellationToken)
        {
            var item = await _repository.GetAsync(request.Id);

            if (item is null)
            {
                throw new ItemNotFoundException(request.Id);
            }
            var updatedItem = Domain.Entities.Item.Pop(item, request.Amount);
            await _repository.UpdateAsync(updatedItem);

            await _eventProcessor.ProcessAsync(updatedItem.Events);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(UpdateItemCommand request, CancellationToken cancellationToken)
        {
            var item = await _repository.GetAsync(request.Id);

            if (item is null)
            {
                throw new ItemNotFoundException(request.Id);
            }
            var updatedItem = Item.Update(item, request.Category, request.Name, request.Description, request.Tags, request.UnitPrice);
            await _repository.UpdateAsync(updatedItem);

            await _eventProcessor.ProcessAsync(updatedItem.Events);

            return(Unit.Value);
        }
Пример #13
0
        public async Task HandleAsync(UpdateScheduleSchema command)
        {
            var schema = await _repository.GetAsync(command.Id);

            if (schema is null)
            {
                throw new ScheduleSchemaNotFoundException(command.Id);
            }

            schema.ChangeTimes(command.Times.AsScheduleSchemaTimes());

            await _repository.UpdateAsync(schema);

            await _processor.ProcessAsync(schema.DomainEvents);
        }
        public async Task HandleAsync(ReleaseResourceReservation command)
        {
            var resource = await _repository.GetAsync(command.ResourceId);

            if (resource is null)
            {
                throw new ResourceNotFoundException(command.ResourceId);
            }

            var reservation = resource.Reservations.FirstOrDefault(r => r.DateTime.Date == command.DateTime.Date);

            resource.ReleaseReservation(reservation);
            await _repository.UpdateAsync(resource);

            await _eventProcessor.ProcessAsync(resource.Events);
        }
Пример #15
0
        public async Task HandleAsync(CreateReservation command)
        {
            if (await _repository.ExistsAsync(command.Id))
            {
                throw new ReservationAlreadyExistsException(command.Id);
            }

            var seats    = command.Seats.AsValueObjects();
            var reservee = command.Reservee.AsValueObject();

            var reservation = await _factory.CreateAsync(command.Id, command.CinemaId, command.MovieId, command.HallId,
                                                         command.CustomerId, command.DateTime, command.IsPaymentUponArrival, seats, reservee);

            await _repository.AddAsync(reservation);

            await _processor.ProcessAsync(reservation.DomainEvents);
        }
Пример #16
0
        public async Task HandleAsync(GenerateSchedule command)
        {
            var alreadyExists = await _repository.ExistsAsync(command.CinemaId, command.MovieId);

            if (alreadyExists)
            {
                throw new ScheduleAlreadyExistsException(command.CinemaId, command.MovieId);
            }

            var movie = await _client.GetAsync(command.MovieId);

            if (movie is null)
            {
                throw new MovieNotFoundException(command.MovieId);
            }

            var schedule = await _policy.GenerateScheduleAsync(command.Id, command.CinemaId, command.MovieId,
                                                               command.From, command.To, movie.AgeRestriction);

            await _repository.AddAsync(schedule);

            await _processor.ProcessAsync(schedule.DomainEvents);
        }
 private Task ProcessEventAsync <TEvent>(TEvent @event, ConceptContext context) where TEvent : EventBase => _eventProcessor.ProcessAsync(@event);