Ignore:
Timestamp:
10/07/21 20:07:41 (3 years ago)
Author:
KostaFortumanov <kfortumanov@…>
Branches:
master
Children:
0c37625
Parents:
0f4f552
Message:

post pagination

Location:
src/main/java/it/finki/charitable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/it/finki/charitable/controller/DonationPostController.java

    r0f4f552 r276a8b6  
    44import it.finki.charitable.services.*;
    55import it.finki.charitable.util.FileUploadUtil;
     6import org.springframework.data.domain.Page;
    67import org.springframework.format.annotation.DateTimeFormat;
    78import org.springframework.security.core.context.SecurityContextHolder;
     
    122123
    123124    @RequestMapping("/album")
    124     public String album(Model model) {
    125         List<DonationPost> postList = donationPostService.findAllByApproved(true);
    126         if (postList.size() == 0) {
     125    public String album(Model model,
     126                        @RequestParam int page,
     127                        @RequestParam String sort,
     128                        @RequestParam(required = false,defaultValue = "") String order) {
     129        Page<DonationPost> postList = donationPostService.findPaginated(page,6, sort, order);
     130        if (postList.getSize() == 0) {
    127131            model.addAttribute("noPosts", true);
    128132            return "album";
    129133        }
     134        model.addAttribute("totalPages", postList.getTotalPages());
    130135        model.addAttribute("postList", postList);
    131136        return "album";
  • src/main/java/it/finki/charitable/repository/DonationPostRepository.java

    r0f4f552 r276a8b6  
    44import it.finki.charitable.entities.DonationPost;
    55import it.finki.charitable.entities.Moderator;
     6import org.springframework.data.domain.Page;
     7import org.springframework.data.domain.Pageable;
    68import org.springframework.data.jpa.repository.JpaRepository;
    79import org.springframework.stereotype.Repository;
     
    1416    List<DonationPost> findAllByApproved(Boolean approved);
    1517    List<DonationPost> findAllByModerator(Moderator moderator);
     18    Page<DonationPost> findAllByApproved(Pageable pageable, boolean approved);
    1619}
  • src/main/java/it/finki/charitable/services/DonationPostService.java

    r0f4f552 r276a8b6  
    5454    }
    5555
    56     public Page<DonationPost> findPaginated(int pageNo, int pageSize, String sort) {
    57         Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Sort.by(sort));
    58         return donationPostRepository.findAll(pageable);
     56    public Page<DonationPost> findPaginated(int pageNo, int pageSize, String sort, String order) {
     57        Sort s = Sort.by(sort);
     58        s = order.equals("asc") ? s.ascending() : s.descending();
     59        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, s);
     60        return donationPostRepository.findAllByApproved(pageable, true);
    5961    }
    6062}
Note: See TracChangeset for help on using the changeset viewer.