Skip to content

Commit

Permalink
added pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
linxiaoxin committed Sep 29, 2024
1 parent 180266f commit def0cac
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.quemistry.statistics_ms.controller;

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.service.StatisticsService;
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 java.util.List;

@RestController
@RequestMapping("/v1/stats")
public class StatisticsController {
Expand All @@ -22,8 +20,8 @@ public StatisticsController(StatisticsService statisticsService) {
}

@GetMapping("mcq")
public ResponseEntity<List<MCQStatisticsDto>> getStatistics(@RequestBody StatisticsRequest request){
var result = statisticsService.retrieveMcqStatics();
public ResponseEntity<StatisticsResponse> getStatistics(@RequestBody StatisticsRequest request){
var result = statisticsService.retrieveMcqStatics(request.getPageNo(), request.getPageSize());
return ResponseEntity.ok(result);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package com.quemistry.statistics_ms.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler({RuntimeException.class})
public ResponseEntity<Object> handleRuntimeException(RuntimeException exception) {
log.error(exception.getMessage(), exception);

return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body( new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Internal Server Error"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class StatisticsRequest {

private int year;
private int month;
private int pageNo = 0;
private int pageSize = 10;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.quemistry.statistics_ms.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class StatisticsResponse {

private List<MCQStatisticsDto> data;
private int pageNo;
private int pageSize;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
import com.quemistry.statistics_ms.model.MCQStatisticsDto;
import com.quemistry.statistics_ms.repository.entity.MCQStatistics;
import com.quemistry.statistics_ms.repository.entity.MCQStatisticsPK;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface McqStatisticsRepository extends ReadOnlyRepository<MCQStatistics, MCQStatisticsPK> {

@Query("SELECT new com.quemistry.statistics_ms.model.MCQStatisticsDto(s.mcqId, SUM(s.cntAttempt), SUM(s.cntCorrectAnswer)) FROM MCQStatistics s GROUP BY s.mcqId")
List<MCQStatisticsDto> findTotalAttempt();
@Query(value = """
SELECT new com.quemistry.statistics_ms.model.MCQStatisticsDto(s.mcqId, SUM(s.cntAttempt), SUM(s.cntCorrectAnswer))
FROM MCQStatistics s
GROUP BY s.mcqId
ORDER BY (SUM(s.cntCorrectAnswer)/SUM(s.cntAttempt))
""" ,
countQuery = """
SELECT COUNT(s.mcqId ) FROM MCQStatistics s GROUP BY s.mcqId
""")
Page<MCQStatisticsDto> findTotalAttempt(Pageable pageable);
}

Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.quemistry.statistics_ms.service;

import com.quemistry.statistics_ms.model.MCQStatisticsDto;
import org.springframework.stereotype.Service;

import java.util.List;
import com.quemistry.statistics_ms.model.StatisticsResponse;

public interface StatisticsService {
List<MCQStatisticsDto> retrieveMcqStaticsForMonth(int year, int month);
List<MCQStatisticsDto> retrieveMcqStatics();
StatisticsResponse retrieveMcqStatics(int pageNo, int pageSize);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.quemistry.statistics_ms.service.impl;

import com.quemistry.statistics_ms.model.MCQStatisticsDto;
import com.quemistry.statistics_ms.model.StatisticsResponse;
import com.quemistry.statistics_ms.repository.McqStatisticsRepository;
import com.quemistry.statistics_ms.service.StatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class StatisticsServiceImpl implements StatisticsService {
Expand All @@ -19,13 +18,15 @@ public StatisticsServiceImpl(McqStatisticsRepository mcqStatisticsRepository) {
}

@Override
public List<MCQStatisticsDto> retrieveMcqStaticsForMonth(int year, int month) {
public StatisticsResponse retrieveMcqStatics(int pageNo, int pageSize) {
var paging = PageRequest.of(pageNo, pageSize);

return List.of();
}
var result = mcqStatisticsRepository.findTotalAttempt(paging);
StatisticsResponse statisticsResponse = new StatisticsResponse();
statisticsResponse.setData(result.get().toList());
statisticsResponse.setPageSize(pageSize);
statisticsResponse.setPageNo(pageNo);

@Override
public List<MCQStatisticsDto> retrieveMcqStatics() {
return mcqStatisticsRepository.findTotalAttempt();
return statisticsResponse;
}
}
21 changes: 16 additions & 5 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ spring:
user: ${spring.datasource.username}
password: ${spring.datasource.password}

jpa.:
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
management:
endpoints:
web:
exposure:
include: health
base-path: /v1
path-mapping:
health: /stats/actuator
endpoint:
health:
show-details: never
enabled: true
health:
mail:
enabled: false

server:
port: 85
port: 80

logging:
level:
Expand Down

0 comments on commit def0cac

Please sign in to comment.