Skip to content

Commit

Permalink
using request parameter as input for GET method
Browse files Browse the repository at this point in the history
  • Loading branch information
linxiaoxin committed Sep 30, 2024
1 parent 7fa86d8 commit 041b9c2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -24,14 +23,18 @@ public StatisticsController(StatisticsService statisticsService) {
}

@GetMapping("mcq")
public ResponseEntity<StatisticsResponse<List<MCQStatisticsDto>>> getMcqStatistics(@RequestBody StatisticsRequest request){
var result = statisticsService.retrieveMcqStatics(request.getPageNo(), request.getPageSize());
public ResponseEntity<StatisticsResponse<List<MCQStatisticsDto>>> 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<StatisticsResponse<List<TopicSkillStatisticsDto>>> getTopicSkillStatistics(@RequestBody StatisticsRequest request){
var result = statisticsService.retrieveTopicSkillStatics(request.getPageNo(), request.getPageSize());
public ResponseEntity<StatisticsResponse<List<TopicSkillStatisticsDto>>> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 041b9c2

Please sign in to comment.