From 7bb0354d95522249379d278f6c36913d2c7b965b Mon Sep 17 00:00:00 2001 From: kimnamgyu Date: Tue, 29 Oct 2024 12:47:31 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=AC=B8=EC=A0=9C=20=EB=9E=9C=EB=8D=A4?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C=EB=A5=BC=20=EC=9C=84=ED=95=B4=20=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=9C=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/syu/capsbe/domain/problem/ProblemRepository.java | 3 +++ .../domain/problem/application/ProblemService.java | 2 ++ .../domain/problem/application/ProblemServiceImpl.java | 9 +++++++++ .../domain/problem/presentation/ProblemController.java | 8 ++++++++ 4 files changed, 22 insertions(+) diff --git a/src/main/java/com/syu/capsbe/domain/problem/ProblemRepository.java b/src/main/java/com/syu/capsbe/domain/problem/ProblemRepository.java index 3ca151a..5fbd29f 100644 --- a/src/main/java/com/syu/capsbe/domain/problem/ProblemRepository.java +++ b/src/main/java/com/syu/capsbe/domain/problem/ProblemRepository.java @@ -8,6 +8,9 @@ @Repository public interface ProblemRepository extends JpaRepository { + @Query("SELECT p FROM Problem p ORDER BY RAND() LIMIT 1") + Optional getRandomProblem(); + @Query("SELECT p FROM Problem p WHERE p.problemType = :problemType ORDER BY RAND() LIMIT 1") Optional getProblemByProblemType(ProblemType problemType); diff --git a/src/main/java/com/syu/capsbe/domain/problem/application/ProblemService.java b/src/main/java/com/syu/capsbe/domain/problem/application/ProblemService.java index e81b5fe..747767f 100644 --- a/src/main/java/com/syu/capsbe/domain/problem/application/ProblemService.java +++ b/src/main/java/com/syu/capsbe/domain/problem/application/ProblemService.java @@ -6,6 +6,8 @@ public interface ProblemService { + ProblemResponseDto getRandomProblem(); + ProblemResponseDto getProblemByProblemType(String problemTypeRequest); ProblemHintResponseDto getHintByQuestion(Long problemId); diff --git a/src/main/java/com/syu/capsbe/domain/problem/application/ProblemServiceImpl.java b/src/main/java/com/syu/capsbe/domain/problem/application/ProblemServiceImpl.java index ab5055b..5323e42 100644 --- a/src/main/java/com/syu/capsbe/domain/problem/application/ProblemServiceImpl.java +++ b/src/main/java/com/syu/capsbe/domain/problem/application/ProblemServiceImpl.java @@ -21,6 +21,15 @@ public class ProblemServiceImpl implements ProblemService { private final ProblemRepository problemRepository; private final PromptService promptService; + @Override + public ProblemResponseDto getRandomProblem() { + return ProblemResponseDto.of( + problemRepository.getRandomProblem() + .orElseThrow( + () -> ProblemExistsException.of( + ProblemErrorCode.PROBLEM_IS_NOT_EXISTS))); + } + @Override public ProblemResponseDto getProblemByProblemType(String problemTypeRequest) { return ProblemResponseDto.of( diff --git a/src/main/java/com/syu/capsbe/domain/problem/presentation/ProblemController.java b/src/main/java/com/syu/capsbe/domain/problem/presentation/ProblemController.java index 9be46d9..5fcff21 100644 --- a/src/main/java/com/syu/capsbe/domain/problem/presentation/ProblemController.java +++ b/src/main/java/com/syu/capsbe/domain/problem/presentation/ProblemController.java @@ -22,6 +22,14 @@ public class ProblemController { private final ProblemService problemService; + @GetMapping("/random") + @Operation(summary = "랜덤 문제 조회", description = "문제 유형을 통해 랜덤 문제를 조회합니다. 해당 요청은 문제 풀이 시마다 호출됩니다.") + @ApiResponse(responseCode = "200", description = "문제 조회 성공") + public ProblemResponseDto getRandomProblem() { + return problemService.getRandomProblem(); + } + + @GetMapping("/{problemType}") @Operation(summary = "문제 조회", description = "문제 유형을 통해 문제를 조회합니다. 해당 요청은 문제 풀이 시마다 호출됩니다.") @ApiResponse(responseCode = "200", description = "문제 조회 성공")