Skip to content

Commit

Permalink
Merge pull request #87 from Likelion-YeungNam-Univ/feature-report
Browse files Browse the repository at this point in the history
Report 예외 추가 및 반환값 수정
  • Loading branch information
iampingu99 authored Aug 2, 2024
2 parents 50de8a7 + 9fc4977 commit 58a5447
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public interface ReportApi {
기록이 없는 경우 빈 객체가 반환됩니다.
""")
@ApiResponse(responseCode = "200", description = "본인 증상 테스트 기록 점수 조회 성공")
@ApiResponse(responseCode = "404", description = "본인의 증상 테스트 기록이 존재하지 않는 경우",
content = @Content(mediaType = "application/json", examples = {
@ExampleObject(value = """
{
"timestamp": "2024-07-19T17:56:39.188+00:00",
"name": "TEST_HISTORY_NOT_FOUND_BY_USER",
"cause": "해당 유저의 증상 테스트 기록이 존재하지 않습니다."
}
""")}))
ResponseEntity<List<UserReportScoreResponseDto>> score(HttpServletRequest request);

@GetMapping("/user/mate/reports/score")
Expand All @@ -38,15 +47,22 @@ public interface ReportApi {
""")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "짝꿍 증상 테스트 기록 점수 조회 성공"),
@ApiResponse(responseCode = "404", description = "짝꿍이 존재하지 않는 경우",
@ApiResponse(responseCode = "404", description = "짝꿍 또는 짝꿍의 증상 테스트 기록이 존재하지 않는 경우",
content = @Content(mediaType = "application/json", examples = {
@ExampleObject(value = """
@ExampleObject(name = "MATE_NOT_FOUND", value = """
{
"timestamp": "2024-07-19T17:56:39.188+00:00",
"name": "MATE_NOT_FOUND",
"cause": "사용자의 짝꿍을 찾을 수 없습니다."
}
"""),
@ExampleObject(name = "TEST_HISTORY_NOT_FOUND_BY_USER", value = """
{
"timestamp": "2024-07-19T17:56:39.188+00:00",
"name": "TEST_HISTORY_NOT_FOUND_BY_USER",
"cause": "해당 유저의 증상 테스트 기록이 존재하지 않습니다."
}
""")
}))
})
ResponseEntity<List<UserReportScoreResponseDto>> mateScore(HttpServletRequest request);
Expand All @@ -58,6 +74,15 @@ public interface ReportApi {
기록이 없는 경우 빈 객체가 반환됩니다.
""")
@ApiResponse(responseCode = "200", description = "본인 증상 테스트 기록 요약 조회 성공")
@ApiResponse(responseCode = "404", description = "본인의 증상 테스트 기록이 존재하지 않는 경우",
content = @Content(mediaType = "application/json", examples = {
@ExampleObject(value = """
{
"timestamp": "2024-07-19T17:56:39.188+00:00",
"name": "TEST_HISTORY_NOT_FOUND_BY_USER",
"cause": "해당 유저의 증상 테스트 기록이 존재하지 않습니다."
}
""")}))
ResponseEntity<List<UserReportSummaryResponseDto>> summary(HttpServletRequest request);

@GetMapping("/user/mate/reports/summary")
Expand All @@ -68,15 +93,22 @@ public interface ReportApi {
""")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "짝꿍 증상 테스트 기록 요약 조회 성공"),
@ApiResponse(responseCode = "404", description = "짝꿍이 존재하지 않는 경우",
@ApiResponse(responseCode = "404", description = "짝꿍 또는 짝꿍의 증상 테스트 기록이 존재하지 않는 경우",
content = @Content(mediaType = "application/json", examples = {
@ExampleObject(value = """
@ExampleObject(name = "MATE_NOT_FOUND", value = """
{
"timestamp": "2024-07-19T17:56:39.188+00:00",
"name": "MATE_NOT_FOUND",
"cause": "사용자의 짝꿍을 찾을 수 없습니다."
}
"""),
@ExampleObject(name = "TEST_HISTORY_NOT_FOUND_BY_USER", value = """
{
"timestamp": "2024-07-19T17:56:39.188+00:00",
"name": "TEST_HISTORY_NOT_FOUND_BY_USER",
"cause": "해당 유저의 증상 테스트 기록이 존재하지 않습니다."
}
"""),
}))
})
ResponseEntity<List<UserReportSummaryResponseDto>> mateSummary(HttpServletRequest request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public ResponseEntity<List<UserReportScoreResponseDto>> score(HttpServletRequest
String userId = jwtProvider.getUserId(accessToken);

List<UserReport> reports = userReportService.readScore(Long.parseLong(userId));
List<UserReportScoreResponseDto> response = reports.stream().map(UserReportScoreResponseDto::fromEntity).toList();
List<UserReportScoreResponseDto> response = reports.stream()
.map(UserReportScoreResponseDto::fromEntity).toList();
return ResponseEntity.ok().body(response);
}

Expand All @@ -51,15 +52,17 @@ public ResponseEntity<List<UserReportScoreResponseDto>> mateScore(HttpServletReq
if (user.getMate() == null) throw new GlobalException(UserExceptionCode.MATE_NOT_FOUND);

List<UserReport> reports = userReportService.readScore(user.getMate().getId());
List<UserReportScoreResponseDto> response = reports.stream().map(UserReportScoreResponseDto::fromEntity).toList();
List<UserReportScoreResponseDto> response = reports.stream()
.map(UserReportScoreResponseDto::fromEntity).toList();
return ResponseEntity.ok().body(response);
}

public ResponseEntity<List<UserReportSummaryResponseDto>> summary(HttpServletRequest request) {
String accessToken = jwtProvider.getToken(request);
String userId = jwtProvider.getUserId(accessToken);

List<UserReportSummaryResponseDto> response = userReportService.readSummary(Long.parseLong(userId)).stream()
List<UserReport> reports = userReportService.readSummary(Long.parseLong(userId));
List<UserReportSummaryResponseDto> response = reports.stream()
.map(UserReportSummaryResponseDto::fromEntity).toList();
return ResponseEntity.ok().body(response);
}
Expand All @@ -71,7 +74,8 @@ public ResponseEntity<List<UserReportSummaryResponseDto>> mateSummary(HttpServle
User user = userService.read(Long.parseLong(userId));
if (user.getMate() == null) throw new GlobalException(UserExceptionCode.MATE_NOT_FOUND);

List<UserReportSummaryResponseDto> response = userReportService.readSummary(user.getMate().getId()).stream()
List<UserReport> reports = userReportService.readSummary(user.getMate().getId());
List<UserReportSummaryResponseDto> response = reports.stream()
.map(UserReportSummaryResponseDto::fromEntity).toList();
return ResponseEntity.ok().body(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
public enum ReportExceptionCode implements ExceptionCode {
TEST_HISTORY_NOT_FOUND_BY_ID(HttpStatus.NOT_FOUND, "해당 증상 테스트 기록이 존재하지 않습니다."),
ACCESS_DENIED_TO_TEST_HISTORY(HttpStatus.FORBIDDEN, "해당 증상 테스트 기록에 접근 권한이 없습니다."),
USER_REPORT_PERIOD_NOT_MET(HttpStatus.BAD_REQUEST, "마지막 증상 테스트 이후 7일이 경과해야 합니다.");
USER_REPORT_PERIOD_NOT_MET(HttpStatus.BAD_REQUEST, "마지막 증상 테스트 이후 7일이 경과해야 합니다."),
TEST_HISTORY_NOT_FOUND_BY_USER(HttpStatus.NOT_FOUND, "해당 유저의 증상 테스트 기록이 존재하지 않습니다.");

HttpStatus httpStatus;
String cause;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public interface UserReportRepository extends JpaRepository<UserReport, Long> {
*/
@Query("select ur from UserReport ur " +
"join fetch ur.reports r " +
"where ur.user.id = :userId " +
"order by ur.createdAt asc"
"where ur.user.id = :userId "
)
List<UserReport> findAllWithReportByUser(Long userId);
List<UserReport> findAllWithReportByUser(Long userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ public Optional<UserReport> readRecentByUser(User user) {
}

public List<UserReport> readScore(Long userId) {
return userReportRepository.findAllWithReportByUser(userId);
Pageable pageable = PageRequest.of(0, 5, Sort.by(Sort.Direction.DESC, "createdAt"));
List<UserReport> allWithReportByUser = userReportRepository.findAllWithReportByUser(userId, pageable);
if (allWithReportByUser.isEmpty())
throw new GlobalException(ReportExceptionCode.TEST_HISTORY_NOT_FOUND_BY_USER);
return allWithReportByUser;
}

public List<UserReport> readSummary(Long userId) {
Pageable pageable = PageRequest.of(0, 4, Sort.by(Sort.Direction.DESC, "createdAt"));
return userReportRepository.findTop4WithReportAndSolutionByUser(userId, pageable);
List<UserReport> top4WithReportAndSolutionByUser = userReportRepository.findTop4WithReportAndSolutionByUser(userId, pageable);
if (top4WithReportAndSolutionByUser.isEmpty())
throw new GlobalException(ReportExceptionCode.TEST_HISTORY_NOT_FOUND_BY_USER);
return top4WithReportAndSolutionByUser;
}

public UserReport readDetail(User user, Long id) {
Expand Down

0 comments on commit 58a5447

Please sign in to comment.