public async Task <IActionResult> GetCarModels([FromQuery] CarModelParams carModelParams) { // the example in course have to know the ID and gender of user // setting the Params before call the function in Repository class // var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); // var userFromRepo = await _repo.GetUser(currentUserId); // userParams.UserId = currentUserId; // if (string.IsNullOrEmpty(userParams.Gender)) // { // userParams.Gender = userFromRepo.Gender == "male" ? "female" : "male"; // } var carModels = await _repo.GetCarModels(carModelParams); // Debug.WriteLine($"{carModelParams.PageNumber}, {carModelParams.PageSize}"); // return object of Dto class instead of Model class var carModelsToReturn = _mapper.Map <IEnumerable <CarModelForListDto> >(carModels); Response.AddPagination(carModels.CurrentPage, carModels.PageSize, carModels.TotalCount, carModels.TotalPages); return(Ok(carModelsToReturn)); }
public async Task <PagedList <CarModel> > GetCarModels(CarModelParams carmodelParams) { // in course, this function is return all users (lecture 75) // original is return IEnumerable<T> all carmodels // var carModels = await _context.CarModels.Include(p => p.Photos).ToListAsync(); // return carModels; // pagination var carmodels = _context.CarModels.OrderByDescending(c => c.Length).AsQueryable(); // use the criteria in params to filter the models want to return // users = users.Where(u => u.Id != userParmas.UserId); // users = users.Where(u => u.Gender == userParams.Gender); if (carmodelParams.Likers) { // var userLikers = await GetUserLikes(userParams.UserId, userParams.Likers); // users = users.Where(u => userLikers.Contains(u.Id)); } if (carmodelParams.Likees) { // var userLikees = await GetUserLikes(userParams.UserId, carmodelParams.Likees); // carmodels = carmodels.Where(c => userLikees.Contains(c.Id)); } // in course is checking the age of user // var minDob = DateTime.Today.AddYears(-userParams.MaxAge - 1); // var maxDob = DateTime.Today.AddYears(-userParams.MinAge); // users = users.Where(u => u.DateOfBirth >= minDob && u.DateOfBirth <= maxDob); #region Filter Length if (carmodelParams.MinCarModelLength != CarModelParams.DefaultMinLength || carmodelParams.MaxCarModelLength != CarModelParams.DefaultMaxLength) { carmodels = carmodels.Where(c => c.Length >= carmodelParams.MinCarModelLength && c.Length <= carmodelParams.MaxCarModelLength); } #endregion #region Filter Width if (carmodelParams.MinCarModelWidth != CarModelParams.DefaultMinLength || carmodelParams.MaxCarModelWidth != CarModelParams.DefaultMaxLength) { carmodels = carmodels.Where(c => c.Width >= carmodelParams.MinCarModelWidth && c.Width <= carmodelParams.MaxCarModelWidth); } #endregion #region Filter Height if (carmodelParams.MinCarModelHeight != CarModelParams.DefaultMinLength || carmodelParams.MaxCarModelHeight != CarModelParams.DefaultMaxLength) { carmodels = carmodels.Where(c => c.Height >= carmodelParams.MinCarModelHeight && c.Height <= carmodelParams.MaxCarModelHeight); } #endregion #region Filter BootCapacity if (carmodelParams.MinBootCapacity != CarModelParams.DefaultMinLength || carmodelParams.MaxBootCapacity != CarModelParams.DefaultMaxLength) { carmodels = carmodels.Where(c => c.BootCapacity >= carmodelParams.MinBootCapacity && c.BootCapacity <= carmodelParams.MaxBootCapacity); } #endregion #region Sorting Params if (!string.IsNullOrEmpty(carmodelParams.OrderBy)) { switch (carmodelParams.OrderBy) { case "length": carmodels = carmodels.OrderByDescending(c => c.Length); break; case "bootcapacity": carmodels = carmodels.OrderByDescending(c => c.BootCapacity); break; default: carmodels = carmodels.OrderByDescending(c => c.Length); break; } } #endregion return(await PagedList <CarModel> .CreateAsync(carmodels, carmodelParams.PageNumber, carmodelParams.PageSize)); }