Skip to content

Commit

Permalink
added Pagination and Sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-urner committed Apr 4, 2024
1 parent 3853770 commit 4dd514a
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 70 deletions.
8 changes: 2 additions & 6 deletions src/main/java/dev/urner/volodb/dao/ProjectDAO.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package dev.urner.volodb.dao;

import java.util.List;

import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import dev.urner.volodb.entity.Project;

@Repository
public interface ProjectDAO extends ListCrudRepository<Project, Integer> {

public List<Project> findAll();
public interface ProjectDAO extends PagingAndSortingRepository<Project, Integer> {

public Project findById(int projectId);

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/dev/urner/volodb/dao/VolunteerDAO.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package dev.urner.volodb.dao;

import org.springframework.stereotype.Repository;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;

import dev.urner.volodb.entity.Volunteer;

import java.util.List;

@Repository
public interface VolunteerDAO extends ListCrudRepository<Volunteer, Integer> {
public interface VolunteerDAO extends PagingAndSortingRepository<Volunteer, Integer> {

List<Volunteer> findAll();
Page<Volunteer> findAll(Pageable pageable);

Volunteer findById(int id);

Expand Down
12 changes: 5 additions & 7 deletions src/main/java/dev/urner/volodb/dao/VolunteerDocumentDAO.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package dev.urner.volodb.dao;

import java.util.List;

import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import dev.urner.volodb.entity.VolunteerDocument;

@Repository
public interface VolunteerDocumentDAO extends ListCrudRepository<VolunteerDocument, Integer> {

public List<VolunteerDocument> findAll();
public interface VolunteerDocumentDAO extends PagingAndSortingRepository<VolunteerDocument, Integer> {

public VolunteerDocument findById(int id);

public VolunteerDocument save(VolunteerDocument document);

public void deleteById(int id);

public List<VolunteerDocument> findByVolunteerId(int volunteerId);
public Page<VolunteerDocument> findAllByVolunteerId(int volunteerId, Pageable pageable);

}
10 changes: 5 additions & 5 deletions src/main/java/dev/urner/volodb/dao/VolunteerNoteDAO.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package dev.urner.volodb.dao;

import java.util.List;

import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import dev.urner.volodb.entity.VolunteerNote;

@Repository
public interface VolunteerNoteDAO extends ListCrudRepository<VolunteerNote, Integer> {
public interface VolunteerNoteDAO extends PagingAndSortingRepository<VolunteerNote, Integer> {

public List<VolunteerNote> findByVolunteerId(int volunteerId);
public Page<VolunteerNote> findAllByVolunteerId(int volunteerId, Pageable pageable);

public VolunteerNote save(VolunteerNote note);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.urner.volodb.rest;

import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -23,8 +24,10 @@ public class ProjectRestController {

// expose "/project" and return a list of volunteers
@GetMapping
public List<Project> findAll() {
return projectService.findAll();
public Page<Project> findAll(
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
return projectService.findAll(page, pageSize);
}

@GetMapping("/{projectId}")
Expand Down

This file was deleted.

46 changes: 39 additions & 7 deletions src/main/java/dev/urner/volodb/rest/VolunteerRestController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.urner.volodb.rest;

import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand All @@ -15,11 +16,11 @@
import dev.urner.volodb.entity.VolunteerNotFoundException;
import dev.urner.volodb.entity.VolunteerNote;
import dev.urner.volodb.security.UserPrincipal;
import dev.urner.volodb.service.VolunteerDocumentService;
import dev.urner.volodb.service.VolunteerNoteService;
import dev.urner.volodb.service.VolunteerService;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Map;
import java.time.LocalDateTime;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -32,11 +33,27 @@ public class VolunteerRestController {

private final VolunteerService volunteerService;
private final VolunteerNoteService volunteerNoteService;
private final VolunteerDocumentService volunteerDocumentService;

// expose "/volunteers" and return a list of volunteers
@GetMapping
public List<Volunteer> findAll() {
return volunteerService.findAll();
public Page<Volunteer> findAll(
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
@RequestParam(name = "sortField", defaultValue = "") String sortField,
@RequestParam(name = "sortOrder", defaultValue = "asc") String sortOrder) {

// unsorted
if (sortField.equals(""))
return volunteerService.findAll(page, pageSize);

// sorted
if (sortOrder.equals("asc"))
return volunteerService.findAll(page, pageSize, sortField, false);
if (sortOrder.equals("desc"))
return volunteerService.findAll(page, pageSize, sortField, true);

throw new VolunteerInvalidFormatException("SortOrder '" + sortOrder + "' not supported.");
}

// add mapping for GET /volunteers/{volunteerId}
Expand Down Expand Up @@ -105,12 +122,16 @@ public String setVolunteerAvatar(@PathVariable int volunteerId, @RequestParam Mu
return volunteerService.SetAvatar(avatar, volunteerId);
}

// Volunteer-Notes:
// **********************************
// Notes
// **********************************

// GET all notes BY VoloID
@GetMapping("/{volunteerId}/notes")
public List<VolunteerNote> getAllNotesFromVolo(@PathVariable int volunteerId) {
return volunteerNoteService.findAllByVolunteerId(volunteerId);
public Page<VolunteerNote> getAllNotesFromVolo(@PathVariable int volunteerId,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
return volunteerNoteService.findAllByVolunteerId(volunteerId, page, pageSize);
}

// POST note BY VoloID
Expand Down Expand Up @@ -138,14 +159,25 @@ public String deleteByNoteId(@PathVariable int volunteerId, @PathVariable int no
return "Volunteer-Note with Id '" + noteId + "' deleted.";
}

// **********************************
// Documents
// **********************************

@GetMapping("/{volunteerId}/documents")
public Page<VolunteerDocument> getAllDocuments(@PathVariable int volunteerId,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
return volunteerDocumentService.findAllByVolunteerId(volunteerId, page, pageSize);
}

@PostMapping("/{volunteerId}/documents")
public VolunteerDocument postNewDocument(@PathVariable int volunteerId, @RequestParam MultipartFile document,
@RequestParam int documentTypeId, @AuthenticationPrincipal UserPrincipal principal) {
return volunteerService.saveDocument(document, documentTypeId, volunteerId, principal.getUsername());
}

// **********************************
// Exceptionhandler:
// Exceptionhandling:
// **********************************

@ExceptionHandler
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/dev/urner/volodb/service/ProjectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;

Expand All @@ -22,8 +24,8 @@ public class ProjectService {
private final ProjectDAO projectDAO;
private final CountryService countryService;

public List<Project> findAll() {
return projectDAO.findAll();
public Page<Project> findAll(int page, int pageSize) {
return projectDAO.findAll(PageRequest.of(page, pageSize));
}

public Project findByProjectId(int theProjectId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import dev.urner.volodb.dao.VolunteerDocumentDAO;
Expand All @@ -17,8 +19,8 @@ public class VolunteerDocumentService {
private final VolunteerDocumentDAO volunteerDocumentDAO;
private final VolunteerDocumentTypeService documentTypeService;

public List<VolunteerDocument> findAll() {
return volunteerDocumentDAO.findAll();
public Page<VolunteerDocument> findAll(int page, int pageSize) {
return volunteerDocumentDAO.findAll(PageRequest.of(page, pageSize));
}

public VolunteerDocument findById(int documentId) {
Expand Down Expand Up @@ -49,8 +51,8 @@ public void deleteById(int documentId) {
documentTypeService.deleteById(documentId);
}

public List<VolunteerDocument> findByVolunteerId(int volunteerId) {
return volunteerDocumentDAO.findByVolunteerId(volunteerId);
public Page<VolunteerDocument> findAllByVolunteerId(int volunteerId, int page, int pageSize) {
return volunteerDocumentDAO.findAllByVolunteerId(volunteerId, PageRequest.of(page, pageSize));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import dev.urner.volodb.dao.VolunteerNoteDAO;
Expand All @@ -18,9 +20,9 @@ public class VolunteerNoteService {

private final VolunteerNoteDAO volunteerNoteDAO;

public List<VolunteerNote> findAllByVolunteerId(int volunteerId) {
public Page<VolunteerNote> findAllByVolunteerId(int volunteerId, int page, int pageSize) {

List<VolunteerNote> notes = volunteerNoteDAO.findByVolunteerId(volunteerId);
Page<VolunteerNote> notes = volunteerNoteDAO.findAllByVolunteerId(volunteerId, PageRequest.of(page, pageSize));

if (notes == null)
throw new VolunteerNoteNotFoundException("There are no Notes for Volunteer-Id: " + volunteerId);
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/dev/urner/volodb/service/VolunteerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import dev.urner.volodb.entity.VolunteerNotFoundException;
import dev.urner.volodb.entity.Enums.OngoingLegalProceedingsState;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ReflectionUtils;
Expand Down Expand Up @@ -43,8 +46,17 @@ public class VolunteerService {
private final VocationalEduService ves;
private final FileService fileService;

public List<Volunteer> findAll() {
return volunteerDAO.findAll();
public Page<Volunteer> findAll(int page, int pageSize) {
return volunteerDAO.findAll(PageRequest.of(page, pageSize, Sort.by("person.firstname").descending()));
}

public Page<Volunteer> findAll(int page, int pageSize, String sortField, boolean descending) {
Sort sort = Sort.by(sortField);

if (descending)
sort = sort.descending();

return volunteerDAO.findAll(PageRequest.of(page, pageSize, sort));
}

public Volunteer findById(int theId) {
Expand Down

0 comments on commit 4dd514a

Please sign in to comment.