Implementing pagination in few APIs

This commit is contained in:
nisha
2025-01-22 13:53:35 +05:30
parent bae7493926
commit 37bfe691ea
13 changed files with 276 additions and 14 deletions

View File

@@ -4,6 +4,7 @@ import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
@@ -400,4 +401,48 @@ public class NotificationDao {
predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.USER_ID), userId));
return predicates;
}
public PageableResponseBean<List<NotificationResponse>> getNotificationsByUserIdAndCompanyIdByPagination(Long userId, Long companyId, NotificationRequestBean notificationRequestBean) {
UserWithCompanyEntity userWithCompany;
if (companyId != null) {
userWithCompany = companyDao.validateUserWithCompny(userId, companyId);
}else {
userWithCompany=null;
}
Integer pageNo = null;
Integer pageLimit = null;
if (notificationRequestBean.getGlobalFilters() != null) {
pageNo = notificationRequestBean.getGlobalFilters().getPage();
pageLimit = notificationRequestBean.getGlobalFilters().getLimit();
}
if (pageLimit == null || pageLimit <= 0) {
pageLimit = GepafinConstant.DEFAULT_PAGE_LIMIT;
}
if (pageNo == null || pageNo <= 0) {
pageNo = GepafinConstant.DEFAULT_PAGE;
}
Specification<NotificationEntity> spec = search(userId, notificationRequestBean);
if(userWithCompany!=null){
spec = spec.and((root, query, criteriaBuilder) ->
criteriaBuilder.equal(root.get("userWithCompany").get("id"), userWithCompany.getId()));
}
Page<NotificationEntity> entityPage = notificationRepository.findAll(spec, PageRequest.of(pageNo - 1, pageLimit));
// Prepare the response
List<NotificationResponse> notificationResponses = entityPage.getContent().stream()
.map(notification -> {
NotificationResponse response = convertNotificationEntityToNotificationResponse(notification);
return response;
})
.collect(Collectors.toList());
PageableResponseBean<List<NotificationResponse>> pageableResponseBean = new PageableResponseBean<>();
pageableResponseBean.setBody(notificationResponses);
pageableResponseBean.setCurrentPage(entityPage.getNumber() + 1); // Page numbers typically start from 0, so add 1 for user-friendly indexing
pageableResponseBean.setTotalPages(entityPage.getTotalPages());
pageableResponseBean.setTotalRecords(entityPage.getTotalElements());
pageableResponseBean.setPageSize(entityPage.getSize());
return pageableResponseBean;
}
}