Skip to content

Commit

Permalink
Pagination issues on search - model search with pagination is working…
Browse files Browse the repository at this point in the history
… fine
  • Loading branch information
surajcm committed Apr 22, 2024
1 parent 016d768 commit d863813
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
1 change: 0 additions & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ dependencies {

implementation("net.sf.jasperreports:jasperreports:6.20.0")
implementation("com.lowagie:itext:4.2.2")

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.security:spring-security-test")
testImplementation("org.junit.jupiter:junit-jupiter-api")
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/poseidon/model/dao/ModelDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ public Page<Model> listModels(final int pageNumber) {
return modelRepository.findAll(pageable);
}

public Page<Model> searchModels(final Long id, final String name, final int pageNumber) {
return null;
public Page<Model> searchModels(final Long makeId, final String name, final int pageNumber) {
var pageable = PageRequest.of(pageNumber - 1, PAGE_SIZE);
// id will always be there, so is the page Number. name can be null
if (name == null) {
return modelRepository.findByMakeId(makeId, pageable);
} else {
return modelRepository.findByMakeIdAndModelNameContaining(makeId, name, pageable);
}
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/poseidon/model/repo/ModelRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.poseidon.model.repo;

import com.poseidon.model.entities.Model;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
Expand All @@ -14,4 +16,8 @@ public interface ModelRepository extends PagingAndSortingRepository<Model, Long>

@Query("Select m from Model m where m.modelName like :modelName")
List<Model> findByModelNameWildCard(String modelName);

Page<Model> findByMakeId(Long makeId, PageRequest pageable);

Page<Model> findByMakeIdAndModelNameContaining(Long makeId, String name, PageRequest pageable);
}
4 changes: 2 additions & 2 deletions src/main/java/com/poseidon/model/service/ModelService.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Page<Model> listModels(final int pageNumber) {
return modelDao.listModels(pageNumber);
}

public Page<Model> searchModels(final Long id, final String name, final int pageNumber) {
return modelDao.searchModels(id, name, pageNumber);
public Page<Model> searchModels(final Long makeId, final String name, final int pageNumber) {
return modelDao.searchModels(makeId, name, pageNumber);
}
}
16 changes: 3 additions & 13 deletions src/main/java/com/poseidon/model/web/ModelController.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,9 @@ public String searchModelByPage(final @PathVariable(name = "pageNumber") int pag
final Model model) {
logger.info("ListByPage method of user controller ");
loggingFromSearch(makeForm);
var makeVOs = makeService.searchModels(makeForm.getSearchMakeAndModelVO());
makeForm.setStatusMessage("Found " + makeVOs.size() + " Models");
makeForm.setStatusMessageType("info");
if (!makeVOs.isEmpty()) {
makeVOs.forEach(makeVO -> logger.debug(MAKE_VO_IS, makeVO));
makeForm.setMakeAndModelVOs(makeVOs);
}
var searchMakeVOs = convertMakeToMakeVO(makeService.fetchAllMakes());
if (searchMakeVOs != null) {
searchMakeVOs.forEach(searchMakeVO -> logger.debug("SearchMakeVO is {}", searchMakeVO));
makeForm.setMakeVOs(searchMakeVOs);
}
var page = modelService.listModels(pageNumber);
var page = modelService.searchModels(makeForm.getSearchMakeAndModelVO().getMakeId(),
makeForm.getSearchMakeAndModelVO().getModelName(),
pageNumber);
var startCount = (pageNumber - 1) * PAGE_SIZE + 1;
long endCount = (long) startCount + PAGE_SIZE - 1;
if (endCount > page.getTotalElements()) {
Expand Down

0 comments on commit d863813

Please sign in to comment.