diff --git a/src/main/java/dev/urner/volodb/dao/ProjectDAO.java b/src/main/java/dev/urner/volodb/dao/ProjectDAO.java index 021b4c5..7e0567c 100644 --- a/src/main/java/dev/urner/volodb/dao/ProjectDAO.java +++ b/src/main/java/dev/urner/volodb/dao/ProjectDAO.java @@ -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 { - - public List findAll(); +public interface ProjectDAO extends PagingAndSortingRepository { public Project findById(int projectId); diff --git a/src/main/java/dev/urner/volodb/dao/VolunteerDAO.java b/src/main/java/dev/urner/volodb/dao/VolunteerDAO.java index 376bf2a..ca05b0d 100644 --- a/src/main/java/dev/urner/volodb/dao/VolunteerDAO.java +++ b/src/main/java/dev/urner/volodb/dao/VolunteerDAO.java @@ -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 { +public interface VolunteerDAO extends PagingAndSortingRepository { - List findAll(); + Page findAll(Pageable pageable); Volunteer findById(int id); diff --git a/src/main/java/dev/urner/volodb/dao/VolunteerDocumentDAO.java b/src/main/java/dev/urner/volodb/dao/VolunteerDocumentDAO.java index 584ab0b..c8230ce 100644 --- a/src/main/java/dev/urner/volodb/dao/VolunteerDocumentDAO.java +++ b/src/main/java/dev/urner/volodb/dao/VolunteerDocumentDAO.java @@ -1,16 +1,14 @@ 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 { - - public List findAll(); +public interface VolunteerDocumentDAO extends PagingAndSortingRepository { public VolunteerDocument findById(int id); @@ -18,6 +16,6 @@ public interface VolunteerDocumentDAO extends ListCrudRepository findByVolunteerId(int volunteerId); + public Page findAllByVolunteerId(int volunteerId, Pageable pageable); } diff --git a/src/main/java/dev/urner/volodb/dao/VolunteerNoteDAO.java b/src/main/java/dev/urner/volodb/dao/VolunteerNoteDAO.java index 34aacac..d8c874f 100644 --- a/src/main/java/dev/urner/volodb/dao/VolunteerNoteDAO.java +++ b/src/main/java/dev/urner/volodb/dao/VolunteerNoteDAO.java @@ -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 { +public interface VolunteerNoteDAO extends PagingAndSortingRepository { - public List findByVolunteerId(int volunteerId); + public Page findAllByVolunteerId(int volunteerId, Pageable pageable); public VolunteerNote save(VolunteerNote note); diff --git a/src/main/java/dev/urner/volodb/rest/ProjectRestController.java b/src/main/java/dev/urner/volodb/rest/ProjectRestController.java index 69fda2b..917d92b 100644 --- a/src/main/java/dev/urner/volodb/rest/ProjectRestController.java +++ b/src/main/java/dev/urner/volodb/rest/ProjectRestController.java @@ -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.*; @@ -23,8 +24,10 @@ public class ProjectRestController { // expose "/project" and return a list of volunteers @GetMapping - public List findAll() { - return projectService.findAll(); + public Page findAll( + @RequestParam(name = "page", defaultValue = "0") int page, + @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) { + return projectService.findAll(page, pageSize); } @GetMapping("/{projectId}") diff --git a/src/main/java/dev/urner/volodb/rest/VolunteerDocumentRestController.java b/src/main/java/dev/urner/volodb/rest/VolunteerDocumentRestController.java deleted file mode 100644 index ee2e336..0000000 --- a/src/main/java/dev/urner/volodb/rest/VolunteerDocumentRestController.java +++ /dev/null @@ -1,28 +0,0 @@ -package dev.urner.volodb.rest; - -import java.util.List; -import java.util.Map; - -import org.springframework.web.bind.annotation.RestController; - -import dev.urner.volodb.entity.VolunteerDocument; -import dev.urner.volodb.service.VolunteerDocumentService; -import lombok.RequiredArgsConstructor; - -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; - -@RestController -@RequestMapping("/documents") -@RequiredArgsConstructor -public class VolunteerDocumentRestController { - - private final VolunteerDocumentService volunteerDocumentService; - - @GetMapping("/{volunteerId}") - public List getByVolunteerId(@PathVariable int volunteerId) { - return volunteerDocumentService.findByVolunteerId(volunteerId); - } - -} diff --git a/src/main/java/dev/urner/volodb/rest/VolunteerRestController.java b/src/main/java/dev/urner/volodb/rest/VolunteerRestController.java index 9a35e40..9ed4e20 100644 --- a/src/main/java/dev/urner/volodb/rest/VolunteerRestController.java +++ b/src/main/java/dev/urner/volodb/rest/VolunteerRestController.java @@ -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; @@ -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; @@ -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 findAll() { - return volunteerService.findAll(); + public Page 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} @@ -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 getAllNotesFromVolo(@PathVariable int volunteerId) { - return volunteerNoteService.findAllByVolunteerId(volunteerId); + public Page 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 @@ -138,6 +159,17 @@ public String deleteByNoteId(@PathVariable int volunteerId, @PathVariable int no return "Volunteer-Note with Id '" + noteId + "' deleted."; } + // ********************************** + // Documents + // ********************************** + + @GetMapping("/{volunteerId}/documents") + public Page 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) { @@ -145,7 +177,7 @@ public VolunteerDocument postNewDocument(@PathVariable int volunteerId, @Request } // ********************************** - // Exceptionhandler: + // Exceptionhandling: // ********************************** @ExceptionHandler diff --git a/src/main/java/dev/urner/volodb/service/ProjectService.java b/src/main/java/dev/urner/volodb/service/ProjectService.java index 478ed24..d43379e 100644 --- a/src/main/java/dev/urner/volodb/service/ProjectService.java +++ b/src/main/java/dev/urner/volodb/service/ProjectService.java @@ -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; @@ -22,8 +24,8 @@ public class ProjectService { private final ProjectDAO projectDAO; private final CountryService countryService; - public List findAll() { - return projectDAO.findAll(); + public Page findAll(int page, int pageSize) { + return projectDAO.findAll(PageRequest.of(page, pageSize)); } public Project findByProjectId(int theProjectId) { diff --git a/src/main/java/dev/urner/volodb/service/VolunteerDocumentService.java b/src/main/java/dev/urner/volodb/service/VolunteerDocumentService.java index b466f3b..ea2af19 100644 --- a/src/main/java/dev/urner/volodb/service/VolunteerDocumentService.java +++ b/src/main/java/dev/urner/volodb/service/VolunteerDocumentService.java @@ -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; @@ -17,8 +19,8 @@ public class VolunteerDocumentService { private final VolunteerDocumentDAO volunteerDocumentDAO; private final VolunteerDocumentTypeService documentTypeService; - public List findAll() { - return volunteerDocumentDAO.findAll(); + public Page findAll(int page, int pageSize) { + return volunteerDocumentDAO.findAll(PageRequest.of(page, pageSize)); } public VolunteerDocument findById(int documentId) { @@ -49,8 +51,8 @@ public void deleteById(int documentId) { documentTypeService.deleteById(documentId); } - public List findByVolunteerId(int volunteerId) { - return volunteerDocumentDAO.findByVolunteerId(volunteerId); + public Page findAllByVolunteerId(int volunteerId, int page, int pageSize) { + return volunteerDocumentDAO.findAllByVolunteerId(volunteerId, PageRequest.of(page, pageSize)); } } diff --git a/src/main/java/dev/urner/volodb/service/VolunteerNoteService.java b/src/main/java/dev/urner/volodb/service/VolunteerNoteService.java index 5291d5b..76a45c6 100644 --- a/src/main/java/dev/urner/volodb/service/VolunteerNoteService.java +++ b/src/main/java/dev/urner/volodb/service/VolunteerNoteService.java @@ -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; @@ -18,9 +20,9 @@ public class VolunteerNoteService { private final VolunteerNoteDAO volunteerNoteDAO; - public List findAllByVolunteerId(int volunteerId) { + public Page findAllByVolunteerId(int volunteerId, int page, int pageSize) { - List notes = volunteerNoteDAO.findByVolunteerId(volunteerId); + Page notes = volunteerNoteDAO.findAllByVolunteerId(volunteerId, PageRequest.of(page, pageSize)); if (notes == null) throw new VolunteerNoteNotFoundException("There are no Notes for Volunteer-Id: " + volunteerId); diff --git a/src/main/java/dev/urner/volodb/service/VolunteerService.java b/src/main/java/dev/urner/volodb/service/VolunteerService.java index 375feb7..129569a 100644 --- a/src/main/java/dev/urner/volodb/service/VolunteerService.java +++ b/src/main/java/dev/urner/volodb/service/VolunteerService.java @@ -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; @@ -43,8 +46,17 @@ public class VolunteerService { private final VocationalEduService ves; private final FileService fileService; - public List findAll() { - return volunteerDAO.findAll(); + public Page findAll(int page, int pageSize) { + return volunteerDAO.findAll(PageRequest.of(page, pageSize, Sort.by("person.firstname").descending())); + } + + public Page 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) {