Skip to content

Commit

Permalink
Adding pagination support on multiple users
Browse files Browse the repository at this point in the history
  • Loading branch information
surajcm committed Mar 11, 2024
1 parent 4b0eac6 commit d7d4b1d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 11 deletions.
7 changes: 4 additions & 3 deletions gradle/staticCodeAnalysis.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,21 @@ jacocoTestReport {
}
}

tasks.withType(Checkstyle) {
tasks.withType(Checkstyle).configureEach {
reports {
xml.required = false
html.required = true
html.stylesheet resources.text.fromFile('config/checkstyle/xsl/checkstyle-simple.xsl')
}
}

tasks.withType(Pmd) {
tasks.withType(Pmd).configureEach {
reports {
xml.required = false
html.required = true
}
}

cpd {
toolVersion = '6.45.0'
minimumTokenCount = 40
Expand All @@ -73,7 +74,7 @@ jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.27
minimum = 0.26
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.quiz.darkhold.user.entity.User;
import com.quiz.darkhold.user.exception.UserNotFoundException;
import com.quiz.darkhold.user.service.UserService;
import com.quiz.darkhold.user.service.UserServiceImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
Expand All @@ -12,6 +13,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
Expand All @@ -34,9 +36,25 @@ public UserController(final UserService userService) {
*/
@GetMapping("/userManagement")
public String manageUsers(final Model model) {
logger.info("Into the manageUsers method");
var listUsers = userService.listAll();
model.addAttribute("listusers", listUsers);
return listByPage(1, model);
}

@RequestMapping("/user/page/{pageNumber}")
public String listByPage(final @PathVariable(name = "pageNumber") int pageNumber,
final Model model) {
logger.info("ListByPage method of user controller ");
var page = userService.getAllUsers(pageNumber);
var startCount = (pageNumber - 1) * UserServiceImpl.USERS_PER_PAGE + 1;
long endCount = (long) startCount + UserServiceImpl.USERS_PER_PAGE - 1;
if (endCount > page.getTotalElements()) {
endCount = page.getTotalElements();
}
model.addAttribute("currentPage", pageNumber);
model.addAttribute("totalPages", page.getTotalPages());
model.addAttribute("startCount", startCount);
model.addAttribute("endCount", endCount);
model.addAttribute("totalItems", page.getTotalElements());
model.addAttribute("listusers", page.getContent());
return "user/usermanagement";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package com.quiz.darkhold.user.repository;

import com.quiz.darkhold.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
public interface UserRepository extends PagingAndSortingRepository<User, Long>, CrudRepository<User, Long> {

User findByEmail(String email);

Long countById(Long id);

@Query("UPDATE User u SET u.enabled = ?2 WHERE u.id = ?1")
@Modifying
void updateEnabledStatus(Long id, boolean enabled);

}


Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.quiz.darkhold.user.entity.Role;
import com.quiz.darkhold.user.entity.User;
import com.quiz.darkhold.user.exception.UserNotFoundException;
import org.springframework.data.domain.Page;

import java.util.List;

Expand All @@ -11,7 +12,7 @@ public interface UserService {

User findByUsername(String username);

List<User> listAll();
Page<User> getAllUsers(final int pageNumber);

List<Role> listRoles();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.quiz.darkhold.user.repository.RoleRepository;
import com.quiz.darkhold.user.repository.UserRepository;
import jakarta.transaction.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

Expand All @@ -14,6 +16,8 @@
@Service
@Transactional
public class UserServiceImpl implements UserService {
public static final int USERS_PER_PAGE = 5;

private final UserRepository userRepository;
private final RoleRepository roleRepository;
private final BCryptPasswordEncoder encoder;
Expand Down Expand Up @@ -59,8 +63,9 @@ public User findByUsername(final String email) {
}

@Override
public List<User> listAll() {
return userRepository.findAll();
public Page<User> getAllUsers(final int pageNumber) {
var pageable = PageRequest.of(pageNumber - 1, USERS_PER_PAGE);
return userRepository.findAll(pageable);
}

@Override
Expand Down
29 changes: 29 additions & 0 deletions src/main/resources/templates/user/usermanagement.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,35 @@ <h2>Manage Users</h2>
</tr>
</tbody>
</table>
<div class="text-center m-1">
<span th:if="${totalItems > 0}">
Showing users #[[${startCount}]] to [[${endCount}]] of [[${totalItems}]]
</span>
<span th:unless="${totalItems > 0}">
No users found
</span>
</div>
<div>
<nav>
<ul class="pagination justify-content-center">
<li th:class="${currentPage > 1 ? 'page-item' : 'page-item disabled'} " >
<a class="page-link" th:href="@{/user/page/1}">First</a>
</li>
<li th:class="${currentPage > 1 ? 'page-item' : 'page-item disabled'} " >
<a class="page-link" th:href="@{'/user/page/' + ${currentPage - 1 }}">Previous</a>
</li>
<li th:class="${currentPage != i ? 'page-item' : 'page-item active'} " th:each="i : ${#numbers.sequence(1, totalPages)}">
<a class="page-link" th:href="@{'/user/page/' + ${i}}">[[${i}]]</a>
</li>
<li th:class="${currentPage < totalPages ? 'page-item' : 'page-item disabled'}" >
<a class="page-link" th:href="@{'/user/page/' + ${currentPage + 1 }}">Next</a>
</li>
<li th:class="${currentPage < totalPages ? 'page-item' : 'page-item disabled'}">
<a class="page-link" th:href="@{'/user/page/' + ${totalPages}}">Last</a>
</li>
</ul>
</nav>
</div>
</form>
<div class="modal fade text-center" id="confirmModal">
<div class="modal-dialog">
Expand Down

0 comments on commit d7d4b1d

Please sign in to comment.