Пример #1
0
            public async Task <IEnumerable <RentalDTO> > Handle(RentalByFiltersQuery request, CancellationToken cancellationToken)
            {
                var query = new QueryRepository <Rental>();

                if (request.CustomerId.HasValue)
                {
                    query.AddSpecification(RentalRepositoryHelper.Specifications.ByCustomerId(request.CustomerId.Value));
                }

                if (request.CarId.HasValue)
                {
                    if (query.HasSpecifications)
                    {
                        query.AddSpecification(SpecificationType.And, RentalRepositoryHelper.Specifications.ByCustomerId(request.CustomerId.Value));
                    }
                    else
                    {
                        query.AddSpecification(RentalRepositoryHelper.Specifications.ByCustomerId(request.CustomerId.Value));
                    }
                }

                query.AddInclusion(RentalRepositoryHelper.Inclusions.Cars());
                query.AddInclusion(RentalRepositoryHelper.Inclusions.Customers());

                IEnumerable <Rental> rentals = await _rentalRepository.GetAllAsync(query);

                return(_mapper.Map <IEnumerable <RentalDTO> >(rentals));
            }
Пример #2
0
            public async Task <IEnumerable <CustomerDTO> > Handle(CustomerByFiltersQuery request, CancellationToken cancellationToken)
            {
                var query = new QueryRepository <Customer>();

                query.AddSpecification(CustomerRepositoryHelper.Specifications.ByName(request.Name));
                query.AddSpecification(SpecificationType.And, CustomerRepositoryHelper.Specifications.ByDriverLicense(request.DriverLicense));
                IEnumerable <Customer> customers = await _customerRepository.GetAllAsync(query);

                return(_mapper.Map <IEnumerable <CustomerDTO> >(customers));
            }
            public async Task <IEnumerable <CarDTO> > Handle(CarByFiltersQuery request, CancellationToken cancellationToken)
            {
                var query = new QueryRepository <Car>();

                query.AddSpecification(CarRepositoryHelper.Specifications.ByModel(request.Model));
                query.AddSpecification(SpecificationType.And, CarRepositoryHelper.Specifications.ByMake(request.Make));

                IEnumerable <Car> cars = await _carRepository.GetAllAsync(query);

                return(_mapper.Map <IEnumerable <CarDTO> >(cars));
            }
Пример #4
0
            public async Task <CustomerDTO> Handle(CustomerByIdQuery request, CancellationToken cancellationToken)
            {
                var query = new QueryRepository <Customer>();

                query.AddSpecification(CustomerRepositoryHelper.Specifications.ById(request.Id));
                Customer customer = await _customerRepository.SingleAsync(query);

                return(_mapper.Map <CustomerDTO>(customer));
            }
Пример #5
0
            public async Task <CarWithMaintenancesDTO> Handle(CarByIdQuery request, CancellationToken cancellationToken)
            {
                var query = new QueryRepository <Car>();

                query.AddSpecification(CarRepositoryHelper.Specifications.ById(request.Id));
                Car car = await _carRepository.SingleAsync(query);

                return(_mapper.Map <CarWithMaintenancesDTO>(car));
            }
            public async Task <MaintenanceDTO> Handle(CreateMaintenanceCommand request, CancellationToken cancellationToken)
            {
                var query = new QueryRepository <Car>();

                query.AddSpecification(CarRepositoryHelper.Specifications.ById(request.CarId));
                Car car = await _carRepository.SingleAsync(query);

                Maintenance maintenance = new Maintenance(request.Date, request.Service, request.Description);

                car.AddMaintenance(maintenance);
                _carRepository.Update(car);
                await _uow.CommitAsync(cancellationToken);

                return(_mapper.Map <MaintenanceDTO>(maintenance));
            }
Пример #7
0
        public async void SingleAsync_ShouldReturnNull()
        {
            // Arrange
            var dbContextMock = new Mock <RentalContext>();
            var mockDbSet     = _cars.AsQueryable().BuildMockDbSet();

            dbContextMock.Setup(x => x.Set <Car>()).Returns(mockDbSet.Object);

            // Act
            var carRepository = new CarRepository(dbContextMock.Object);
            var query         = new QueryRepository <Car>();

            query.AddSpecification(CarRepositoryHelper.Specifications.ByMake("NOTEXIST"));
            var result = await carRepository.SingleAsync(query);

            // Assert
            dbContextMock.Verify(x => x.Set <Car>(), Times.Once);
            Assert.Null(result);
        }
            public async Task <RentalDTO> Handle(CreateRentalCommand request, CancellationToken cancellationToken)
            {
                var carQuery = new QueryRepository <Car>();

                carQuery.AddSpecification(CarRepositoryHelper.Specifications.ById(request.CarId));
                Car car = await _carRepository.SingleAsync(carQuery);

                var customerQuery = new QueryRepository <Customer>();

                customerQuery.AddSpecification(CustomerRepositoryHelper.Specifications.ById(request.CustomerId));
                Customer customer = await _customerRepository.SingleAsync(customerQuery);

                Rental rental = new Rental(request.PickUpDate, request.DropOffDate, customer, car);

                _rentalRepository.Add(rental);
                await _uow.CommitAsync(cancellationToken);

                return(_mapper.Map <RentalDTO>(rental));
            }
Пример #9
0
        public void SingleAsync_ReturnsMoreThanOne_ThrowsException()
        {
            // Arrange
            var dbContextMock = new Mock <RentalContext>();
            var mockDbSet     = _cars.AsQueryable().BuildMockDbSet();

            dbContextMock.Setup(x => x.Set <Car>()).Returns(mockDbSet.Object);

            // Act
            var carRepository = new CarRepository(dbContextMock.Object);
            var query         = new QueryRepository <Car>();

            query.AddSpecification(CarRepositoryHelper.Specifications.ByMake("make"));
            Car result = null;

            Assert.ThrowsAsync <System.Exception>(async() => result = await carRepository.SingleAsync(query));

            // Assert
            dbContextMock.Verify(x => x.Set <Car>(), Times.Once);
        }