From be54be60434ab091616cafc26ac9550dc615d2db Mon Sep 17 00:00:00 2001 From: mouad el azaar Date: Fri, 13 Dec 2024 08:55:19 +0100 Subject: [PATCH] add sorting studies --- .../controller/StudyController.java | 19 +++++++++++++++---- .../datamanager_back/dto/StudyDTO.java | 3 --- .../controller/StudyControllerTest.java | 4 ++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/rte_france/antares/datamanager_back/controller/StudyController.java b/src/main/java/com/rte_france/antares/datamanager_back/controller/StudyController.java index dcb12dd..9bc196e 100644 --- a/src/main/java/com/rte_france/antares/datamanager_back/controller/StudyController.java +++ b/src/main/java/com/rte_france/antares/datamanager_back/controller/StudyController.java @@ -10,7 +10,10 @@ import org.springframework.data.domain.Sort; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; import static com.rte_france.antares.datamanager_back.mapper.StudyMapper.toStudyPage; @@ -28,10 +31,18 @@ public ResponseEntity> searchStudies( @RequestParam(value = "projectId", required = false, defaultValue = "") Integer projectId, @RequestParam(value = "search", required = false, defaultValue = "") String search, @RequestParam(value = "page", required = false, defaultValue = "1") Integer page, - @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) { + @RequestParam(value = "size", required = false, defaultValue = "20") Integer size, + @RequestParam(value = "sortColumn", required = false) String sortColumn, + @RequestParam(value = "sortDirection", required = false) String sortDirection) { - Pageable paging = PageRequest.of(page-1, size, Sort.by(SORTING_CRITERION)); + Sort sorting = Sort.by(SORTING_CRITERION); - return new ResponseEntity<>(toStudyPage(studyService.findStudiesByCriteria(search,projectId, paging)), HttpStatus.OK); + if (!sortColumn.isEmpty() && !sortDirection.isEmpty()) { + Sort.Direction direction = Sort.Direction.fromString(sortDirection); + sorting = Sort.by(direction, sortColumn); + } + Pageable paging = PageRequest.of(page - 1, size, sorting); + + return new ResponseEntity<>(toStudyPage(studyService.findStudiesByCriteria(search, projectId, paging)), HttpStatus.OK); } } diff --git a/src/main/java/com/rte_france/antares/datamanager_back/dto/StudyDTO.java b/src/main/java/com/rte_france/antares/datamanager_back/dto/StudyDTO.java index 16bdef0..3e774ef 100644 --- a/src/main/java/com/rte_france/antares/datamanager_back/dto/StudyDTO.java +++ b/src/main/java/com/rte_france/antares/datamanager_back/dto/StudyDTO.java @@ -18,13 +18,10 @@ public class StudyDTO { @JsonProperty("id") Integer id; - @JsonProperty("study_name") String name; - @JsonProperty("user_name") String createdBy; - @JsonProperty("creation_date") LocalDateTime creationDate; @JsonProperty("keywords") diff --git a/src/test/java/com/rte_france/antares/datamanager_back/controller/StudyControllerTest.java b/src/test/java/com/rte_france/antares/datamanager_back/controller/StudyControllerTest.java index a722f3d..c6decda 100644 --- a/src/test/java/com/rte_france/antares/datamanager_back/controller/StudyControllerTest.java +++ b/src/test/java/com/rte_france/antares/datamanager_back/controller/StudyControllerTest.java @@ -57,6 +57,8 @@ void getStudiesReturnsPageOfStudies() throws Exception { .param("search", "toto") .param("page", "1") .param("size", "2") + .param("sortColumn", "createdBy") + .param("sortDirection", "desc") .accept(MediaType.APPLICATION_JSON_VALUE)) //Then @@ -74,6 +76,8 @@ void getStudiesReturnsEmptyPageWhenNoStudiesFound() throws Exception { .param("search", "toto") .param("page", "1") .param("size", "2") + .param("sortColumn", "createdBy") + .param("sortDirection", "desc") .accept(MediaType.APPLICATION_JSON_VALUE)) //Then