From 041b9c2fd6a4196b37dbf7a717a65df8d3c0e553 Mon Sep 17 00:00:00 2001 From: linxiaoxin Date: Mon, 30 Sep 2024 13:59:49 +0800 Subject: [PATCH] using request parameter as input for GET method --- .../statistics_ms/constant/Pagination.java | 7 +++++++ .../controller/StatisticsController.java | 21 +++++++++++-------- .../controller/StatisticsControllerTest.java | 21 ++++++------------- 3 files changed, 25 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/quemistry/statistics_ms/constant/Pagination.java diff --git a/src/main/java/com/quemistry/statistics_ms/constant/Pagination.java b/src/main/java/com/quemistry/statistics_ms/constant/Pagination.java new file mode 100644 index 0000000..6baa546 --- /dev/null +++ b/src/main/java/com/quemistry/statistics_ms/constant/Pagination.java @@ -0,0 +1,7 @@ +package com.quemistry.statistics_ms.constant; + +public class Pagination { + + public static final int DEFAULT_PAGE_SIZE = 10; + public static final int MAX_PAGE_SIZE = 60; +} diff --git a/src/main/java/com/quemistry/statistics_ms/controller/StatisticsController.java b/src/main/java/com/quemistry/statistics_ms/controller/StatisticsController.java index bad713b..93a365f 100644 --- a/src/main/java/com/quemistry/statistics_ms/controller/StatisticsController.java +++ b/src/main/java/com/quemistry/statistics_ms/controller/StatisticsController.java @@ -1,15 +1,14 @@ package com.quemistry.statistics_ms.controller; +import com.quemistry.statistics_ms.constant.Pagination; import com.quemistry.statistics_ms.model.MCQStatisticsDto; -import com.quemistry.statistics_ms.model.StatisticsRequest; import com.quemistry.statistics_ms.model.StatisticsResponse; import com.quemistry.statistics_ms.model.TopicSkillStatisticsDto; import com.quemistry.statistics_ms.service.StatisticsService; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.PositiveOrZero; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -24,14 +23,18 @@ public StatisticsController(StatisticsService statisticsService) { } @GetMapping("mcq") - public ResponseEntity>> getMcqStatistics(@RequestBody StatisticsRequest request){ - var result = statisticsService.retrieveMcqStatics(request.getPageNo(), request.getPageSize()); + public ResponseEntity>> getMcqStatistics( + @RequestParam(name="pageno", required = false, defaultValue = "0") @PositiveOrZero Integer pageNo, + @RequestParam(name="pagesize", required = false, defaultValue = "10") @PositiveOrZero @Max(Pagination.MAX_PAGE_SIZE) Integer pageSize ) { + var result = statisticsService.retrieveMcqStatics(pageNo, pageSize); return ResponseEntity.ok(result); } @GetMapping("topic-skill") - public ResponseEntity>> getTopicSkillStatistics(@RequestBody StatisticsRequest request){ - var result = statisticsService.retrieveTopicSkillStatics(request.getPageNo(), request.getPageSize()); + public ResponseEntity>> getTopicSkillStatistics( + @RequestParam(name="pageno", required = false, defaultValue = "0") @PositiveOrZero Integer pageNo, + @RequestParam(name="pagesize", required = false, defaultValue = "10") @PositiveOrZero @Max(Pagination.MAX_PAGE_SIZE) Integer pageSize){ + var result = statisticsService.retrieveTopicSkillStatics(pageNo, pageSize); return ResponseEntity.ok(result); } } diff --git a/src/test/java/com/quemistry/statistics_ms/controller/StatisticsControllerTest.java b/src/test/java/com/quemistry/statistics_ms/controller/StatisticsControllerTest.java index 274f85f..b20aeaa 100644 --- a/src/test/java/com/quemistry/statistics_ms/controller/StatisticsControllerTest.java +++ b/src/test/java/com/quemistry/statistics_ms/controller/StatisticsControllerTest.java @@ -9,7 +9,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -52,13 +51,9 @@ void givenGetMcqStatistics_thenStatus200() throws Exception { when(statisticsService.retrieveMcqStatics(pageNo, pageSize)).thenReturn(response); mockMvc.perform(get("/v1/stats/mcq") - .contentType(MediaType.APPLICATION_JSON) - .content(""" - { - "pageNo": "0", - "pageSize": "5" - } - """)); + .param("pageno", "0") + .param("pagesize", "5") + ); verify(statisticsService).retrieveMcqStatics(pageNo, pageSize); } @@ -79,13 +74,9 @@ void givenGetTopicSkillStatistics_thenStatus200() throws Exception { when(statisticsService.retrieveTopicSkillStatics(pageNo, pageSize)).thenReturn(response); mockMvc.perform(get("/v1/stats/topic-skill") - .contentType(MediaType.APPLICATION_JSON) - .content(""" - { - "pageNo": "0", - "pageSize": "5" - } - """)); + .param("pageno", "0") + .param("pagesize", "5") + ); verify(statisticsService).retrieveTopicSkillStatics(pageNo, pageSize); }