/// <summary> /// Get all images /// </summary> /// <param name="page">current page</param> /// <param name="pageSize">number of items to show</param> /// <param name="searchQuery">search items by this criteria</param> /// <returns>IPaginationModel Instance</returns> public virtual async Task <IPaginationModel <Image> > GetImagesAsync(int?page, int pageSize, string searchQuery) { //Check for unwanted values int currentPage = page ?? 1; searchQuery = searchQuery ?? string.Empty; //Count number of iamge instances in repository int totalRecords = await imageRepo.CountAsync(); //Check if variable currentPage valid currentPage = page ?? pgMaker.CheckPageLimits(currentPage, totalRecords, pageSize); //Get instances int skip = pgMaker.Skip(currentPage, pageSize); var instances = await imageRepo.ListPaginationAsync(skip, pageSize); //Prepare pagination model var pgOptions = new PaginationOptions(currentPage, totalRecords, pageSize); return(pgMaker.PreparePaginationModel(instances, pgOptions)); }
/// <summary> /// Categories pagination /// </summary> /// <param name="page">current page</param> /// <param name="searchQuery">search query</param> /// <param name="pageSize">pageSize parametar is fixed(user cant change it) /// in this case so we dont need to check if pagesize is negative</param> /// <returns>Instnace of IPaginationModel</returns> public virtual async Task <IPaginationModel <Category> > GetCategoriesAsync(int?page, string searchQuery, int pageSize) { //Validate input int currentPage = page ?? 1; string search = searchQuery ?? string.Empty; //Get number of instances from database that containts specific text var firstSpecification = new CategorySpecification(search); var numberOfCategories = await repository.CountAsync(firstSpecification); //Is requested page valid ? currentPage = maker.CheckPageLimits(currentPage, numberOfCategories, pageSize); //Get required instances from database int skip = maker.Skip(currentPage, pageSize); int take = pageSize; var secondSpecification = new CategorySpecification(skip, take, search); var listOfCategories = await repository.ListAsync(secondSpecification); //Return instance of IPaginationModel var pgOptions = new PaginationOptions(currentPage, numberOfCategories, pageSize); return(maker.PreparePaginationModel(listOfCategories, pgOptions)); }