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); }