Example #1
0
        //public List<CarDetailsDto> GetAllCarsWithDetails()
        //{
        //    using (ReCapContext context = new ReCapContext())
        //    {
        //        return (from car in context.Cars
        //                join color in context.Colors on car.ColorId equals color.ColorId into temp
        //                from t in temp.DefaultIfEmpty()
        //                join brand in context.Brands on car.BrandId equals brand.BrandId into temp2
        //                from t2 in temp2.DefaultIfEmpty()
        //                select new CarDetailsDto
        //                {
        //                    BrandName = t2.BrandName == null ? "Marka yok" : t2.BrandName,
        //                    ColorName = t.ColorName == null ? "Renk yok" : t.ColorName,
        //                    DailyPrice = car.DailyPrice,
        //                    CarName = car.CarName,
        //                    CarId = car.CarId,
        //                    Description = car.Description,
        //                    ModelYear = car.ModelYear
        //                }).ToList();
        //    }
        //}

        public List <CarDetailsDto> GetAllCarsWithDetails(CarFilterDto carFilterDto)
        {
            // IRepo, UoW (repos, contexts, transaction-rollback), filter(success, error checking),
            using (ReCapContext context = new ReCapContext())
            {
                var result = context.Cars
                             .Include(c => c.Color) // class'lara ayirma
                             .Include(c => c.Brand) // IQueryable
                             .Include(c => c.CarImages)
                             .WhereIf(carFilterDto.brandId.HasValue, c => c.Brand.BrandId == carFilterDto.brandId)
                             .WhereIf(carFilterDto.colorId.HasValue, c => c.Color.ColorId == carFilterDto.colorId)
                             .WhereIf(carFilterDto.MinPrice.HasValue, c => c.DailyPrice > carFilterDto.MinPrice)
                             .WhereIf(carFilterDto.MaxPrice.HasValue, c => c.DailyPrice < carFilterDto.MaxPrice)
                             .Select(c => new CarDetailsDto
                {
                    BrandName   = c.Brand.BrandName == null ? "Marka yok" : c.Brand.BrandName,
                    ColorName   = c.Color.ColorName == null ? "Renk yok" : c.Color.ColorName,
                    DailyPrice  = c.DailyPrice,
                    CarName     = c.CarName,
                    CarId       = c.CarId,
                    Description = c.Description,
                    IsDeleted   = c.IsActive,
                    ModelYear   = c.ModelYear,
                    ImageUrl    = c.CarImages.Any(ci => ci.CarId == c.CarId) ? c.CarImages.FirstOrDefault(ci => ci.CarId == c.CarId).ImagePath : "no-preview.png"
                });

                return(result.ToList());
            }
        }
Example #2
0
        //public async Task<List<CarDetailDto>> GetCarDetailsByFilterAsync(CarFilterDto carFilterDto)
        //{
        //    using (ReCapContext context = new ReCapContext())
        //    {

        //        var result = from car in context.Cars
        //                     join color in context.Colors
        //                     on car.ColorId equals color.Id
        //                     join brand in context.Brands
        //                     on car.BrandId equals brand.Id
        //                     join carCreditScore in context.CarCreditScores
        //                     on car.Id equals carCreditScore.CarId into gj
        //                     from x in gj.DefaultIfEmpty()
        //                     join fuelType in context.FuelTypes
        //                    on car.FuelTypeId equals fuelType.Id
        //                     join gearType in context.GearTypes
        //                     on car.GearTypeId equals gearType.Id
        //                     where color.Name == carFilterDto.ColorName
        //                     && brand.Name == carFilterDto.BrandName
        //                     select new CarDetailDto
        //                     {
        //                         BrandName = brand.Name,
        //                         ColorName = color.Name,
        //                         ModelYear = car.ModelYear,
        //                         DailyPrice = car.DailyPrice,
        //                         Description = car.Description,
        //                         Id = car.Id,
        //                         MinCreditScore = x.MinCreditScore,
        //                         FuelTypeName = fuelType.Name,
        //                         GearTypeName = gearType.Name,
        //                         HorsePower = car.HorsePower,
        //                         Name = car.Name
        //                     };
        //        return await result.AsNoTracking().ToListAsync();
        //    }
        //}

        public async Task <List <CarDetailDto> > GetCarDetailsByFilterAsync(CarFilterDto carFilterDto)
        {
            using (ReCapContext context = new ReCapContext())
            {
                var result = from car in context.Cars
                             join color in context.Colors
                             on car.ColorId equals color.Id
                             join brand in context.Brands
                             on car.BrandId equals brand.Id
                             join carCreditScore in context.CarCreditScores
                             on car.Id equals carCreditScore.CarId into gj
                             from x in gj.DefaultIfEmpty()
                             join fuelType in context.FuelTypes
                             on car.FuelTypeId equals fuelType.Id
                             join gearType in context.GearTypes
                             on car.GearTypeId equals gearType.Id
                             select new CarDetailDto
                {
                    BrandName      = brand.Name,
                    ColorName      = color.Name,
                    ModelYear      = car.ModelYear,
                    DailyPrice     = car.DailyPrice,
                    Description    = car.Description,
                    Id             = car.Id,
                    MinCreditScore = x.MinCreditScore,
                    FuelTypeName   = fuelType.Name,
                    GearTypeName   = gearType.Name,
                    HorsePower     = car.HorsePower,
                    Name           = car.Name
                };

                return(await result.AsNoTracking().ApplyFilter(carFilterDto).ToListAsync());
            }
        }
Example #3
0
        //car/getall
        //form id="a" -> json
        public IActionResult GetAllCarsWithDetails([FromQuery] CarFilterDto carFilterDto)
        {
            var result = _carService.GetAllCarsWithDetails(carFilterDto);

            if (result.Success)
            {
                return(Ok(result));
            }
            return(Ok(result));
        }
Example #4
0
        public async Task <IActionResult> GetCarDetailsByFiltersAsync(CarFilterDto carFilterDto)
        {
            var result = await _carService.GetCarDetailsByFiltersAsync(carFilterDto);

            if (result.Success)
            {
                return(Ok(result));
            }

            return(BadRequest(result));
        }
Example #5
0
 public IDataResult <List <CarDetailsDto> > GetAllCarsWithDetails(CarFilterDto carFilterDto)
 {
     return(new SuccessDataResult <List <CarDetailsDto> >(_carDal.GetAllCarsWithDetails(carFilterDto)));
 }
 public List <CarDetailsDto> GetAllCarsWithDetails(CarFilterDto carFilterDto)
 {
     throw new NotImplementedException();
 }
Example #7
0
        public async Task <IDataResult <List <CarDetailDto> > > GetCarDetailsByFiltersAsync(CarFilterDto carFilterDto)
        {
            var carDetails = await _carDal.GetCarDetailsByFilterAsync(carFilterDto);

            foreach (var item in carDetails)
            {
                item.ImagePaths = (await _carImageService.GetAllByCarIdAsync(item.Id)).Data;
            }

            if (carDetails.Count == 0)
            {
                return(new ErrorDataResult <List <CarDetailDto> >(null, Messages.CarNotFoundByFilters));
            }

            return(new SuccessDataResult <List <CarDetailDto> >(carDetails, Messages.CarGetListByFilters));
        }