From 04b3b311a9ee4943f5eaaf71aaf1d13395bc787a Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 30 Jan 2025 17:00:16 +0400 Subject: [PATCH] Add pagination for indicators --- .../controller/AspirantController.java | 25 +++++++++++++++++-- .../aspirant/service/AspirantService.java | 6 +++++ .../indicator/service/IndicatorService.java | 6 +++++ .../templates/aspirant/editReport.html | 9 +++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main/java/ru/ulstu/aspirant/controller/AspirantController.java b/src/main/java/ru/ulstu/aspirant/controller/AspirantController.java index 08ee801..f87162b 100644 --- a/src/main/java/ru/ulstu/aspirant/controller/AspirantController.java +++ b/src/main/java/ru/ulstu/aspirant/controller/AspirantController.java @@ -1,18 +1,26 @@ package ru.ulstu.aspirant.controller; +import org.springframework.data.domain.Page; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.aspirant.model.Report; import ru.ulstu.aspirant.service.AspirantService; import ru.ulstu.indicator.model.Indicator; +import ru.ulstu.model.OffsetablePageRequest; import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.IntStream; @Controller @RequestMapping("aspirant") public class AspirantController { + private static final Integer DEFAULT_PAGE_SIZE = 1; + private static final Integer DEFAULT_PAGE_NUMBER = 1; private final AspirantService aspirantService; public AspirantController(AspirantService aspirantService) { @@ -20,9 +28,22 @@ public class AspirantController { } @GetMapping("aspirantReport") - public String createReport(Model model) { - List indicators = aspirantService.getIndicatorsByCourse(); + public String createReport(Model model, + @RequestParam Optional page, + @RequestParam Optional size) { + int currentPage = page.orElse(DEFAULT_PAGE_NUMBER); + int pageSize = size.orElse(DEFAULT_PAGE_SIZE); + + Page indicators = aspirantService.getIndicatorsByCourse(new OffsetablePageRequest(currentPage - 1, pageSize)); model.addAttribute("indicators", indicators); + int totalPages = indicators.getTotalPages(); + if (totalPages > 0) { + List pageNumbers = IntStream.rangeClosed(1, totalPages) + .boxed() + .collect(Collectors.toList()); + model.addAttribute("pageNumbers", pageNumbers); + } + model.addAttribute("report", new Report()); return "aspirant/editReport"; } diff --git a/src/main/java/ru/ulstu/aspirant/service/AspirantService.java b/src/main/java/ru/ulstu/aspirant/service/AspirantService.java index bf718c7..2b6014a 100644 --- a/src/main/java/ru/ulstu/aspirant/service/AspirantService.java +++ b/src/main/java/ru/ulstu/aspirant/service/AspirantService.java @@ -1,5 +1,7 @@ package ru.ulstu.aspirant.service; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import ru.ulstu.admin.model.AspirantForm; import ru.ulstu.aspirant.model.Aspirant; @@ -48,4 +50,8 @@ public class AspirantService { Aspirant aspirant = getAspirantByUser(UserUtils.getCurrentUserLogin()); return indicatorService.getIndicatorsByCourse(aspirant.getCourse()); } + + public Page getIndicatorsByCourse(Pageable pageable) { + return indicatorService.getIndicatorsByCourse(pageable); + } } diff --git a/src/main/java/ru/ulstu/indicator/service/IndicatorService.java b/src/main/java/ru/ulstu/indicator/service/IndicatorService.java index 6c96f09..0e784b7 100644 --- a/src/main/java/ru/ulstu/indicator/service/IndicatorService.java +++ b/src/main/java/ru/ulstu/indicator/service/IndicatorService.java @@ -1,5 +1,7 @@ package ru.ulstu.indicator.service; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import ru.ulstu.admin.model.IndicatorForm; import ru.ulstu.indicator.model.Course; @@ -39,4 +41,8 @@ public class IndicatorService { public List getIndicatorsByCourse(Course course) { return indicatorRepository.findAllByCourse(course); } + + public Page getIndicatorsByCourse(Pageable pageable) { + return indicatorRepository.findAll(pageable); + } } diff --git a/src/main/resources/templates/aspirant/editReport.html b/src/main/resources/templates/aspirant/editReport.html index 4efa54a..045392e 100644 --- a/src/main/resources/templates/aspirant/editReport.html +++ b/src/main/resources/templates/aspirant/editReport.html @@ -99,6 +99,15 @@ Отмена + +