From bbe99d990e286296ea0ebbd24e30693cbe0f9230 Mon Sep 17 00:00:00 2001 From: miiiinju1 Date: Tue, 12 Sep 2023 16:39:00 +0900 Subject: [PATCH] =?UTF-8?q?:pencil2:=20[FIX]=20Redis=20=ED=82=A4=20attempt?= =?UTF-8?q?ProblemId=20->=20test=20=EB=AC=B8=EC=A0=9C=20=EB=B2=88=ED=98=B8?= =?UTF-8?q?=20#260?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/repository/AttemptProblemRepository.java | 2 +- .../controller/TempCodeController.java | 4 ++-- .../morandi/testMemberTempCode/dto/TempCodeDto.java | 2 +- .../testMemberTempCode/service/TempCodeService.java | 13 ++++++++----- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/java/swm_nm/morandi/member/repository/AttemptProblemRepository.java b/src/main/java/swm_nm/morandi/member/repository/AttemptProblemRepository.java index 5acb3095..e0a84f7e 100644 --- a/src/main/java/swm_nm/morandi/member/repository/AttemptProblemRepository.java +++ b/src/main/java/swm_nm/morandi/member/repository/AttemptProblemRepository.java @@ -9,7 +9,7 @@ public interface AttemptProblemRepository extends JpaRepository { List findAllByMember_MemberId(Long memberId); List findAllByTest_TestId(Long testId); - List findAllByTest(Test test); + List findAllByTestOrderByAttemptProblemIdAsc(Test test); List findAttemptProblemsByTest_TestId(Long testId); //List findAttemptProblemsByTest_TestIdOrderByAttemptProblemIdAsc(Long testId); diff --git a/src/main/java/swm_nm/morandi/testMemberTempCode/controller/TempCodeController.java b/src/main/java/swm_nm/morandi/testMemberTempCode/controller/TempCodeController.java index 79d5e578..f3cea149 100644 --- a/src/main/java/swm_nm/morandi/testMemberTempCode/controller/TempCodeController.java +++ b/src/main/java/swm_nm/morandi/testMemberTempCode/controller/TempCodeController.java @@ -29,9 +29,9 @@ public void saveTempCode(@RequestBody TempCodeDto tempCodeDto) { @Operation(summary = "저장된 코드를 확인합니다", description = "테스트 중일 때, 문제 번호를 바꿀 때 코드 정보를\n" + "testId와 attemptProblemId를 이용하여 가져온다. \n") public ResponseEntity getTempCode(@RequestParam String testId, - @RequestParam String attemptProblemId){ + @RequestParam String problemNumber){ - String key =String.format("tests:%s:problems:%s",testId, attemptProblemId); + String key =String.format("testId:%s:problemNumber:%s",testId, problemNumber); return new ResponseEntity<>(tempCodeService.getTempCode(key), HttpStatus.OK); diff --git a/src/main/java/swm_nm/morandi/testMemberTempCode/dto/TempCodeDto.java b/src/main/java/swm_nm/morandi/testMemberTempCode/dto/TempCodeDto.java index e2be0347..de55d0c1 100644 --- a/src/main/java/swm_nm/morandi/testMemberTempCode/dto/TempCodeDto.java +++ b/src/main/java/swm_nm/morandi/testMemberTempCode/dto/TempCodeDto.java @@ -10,6 +10,6 @@ @NoArgsConstructor public class TempCodeDto { public String testId; - public String attemptProblemId; + public String problemNumber; public String code; } diff --git a/src/main/java/swm_nm/morandi/testMemberTempCode/service/TempCodeService.java b/src/main/java/swm_nm/morandi/testMemberTempCode/service/TempCodeService.java index 533b9bc5..9af8d8f1 100644 --- a/src/main/java/swm_nm/morandi/testMemberTempCode/service/TempCodeService.java +++ b/src/main/java/swm_nm/morandi/testMemberTempCode/service/TempCodeService.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; @Service @RequiredArgsConstructor @@ -27,11 +28,13 @@ public class TempCodeService { // key를 생성 후, 시작 시점에 tempCode를 저장하여 TTL을 설정한다 // 그래서 시험이 끝나는 시간을 보장할 수 있음 public void initTempCodeCacheWhenTestStart(Test test){ - List attemptProblems = attemptProblemRepository.findAllByTest(test); + List attemptProblems = attemptProblemRepository.findAllByTestOrderByAttemptProblemIdAsc(test); LocalDateTime now = LocalDateTime.now(); + AtomicInteger i = new AtomicInteger(1); attemptProblems.forEach(attemptProblem-> { - String key = generateKey(test, attemptProblem); + String key = generateKey(test, i.getAndIncrement()); + //끝나는 시간 LocalDateTime endTime = now.plusMinutes(test.getTestTime()); Duration duration = Duration.between(now, endTime); @@ -76,10 +79,10 @@ public TempCode getTempCode(String key) { ); } - public String generateKey(Test test, AttemptProblem attemptProblem) { - return String.format("tests:%s:problems:%s",test.getTestId(), attemptProblem.getAttemptProblemId()); + public String generateKey(Test test, int problemNumber) { + return String.format("testId:%s:problemNumber:%s",test.getTestId(), problemNumber); } public String generateKey(TempCodeDto tempCodeDto) { - return String.format("tests:%s:problems:%s", tempCodeDto.testId, tempCodeDto.attemptProblemId); + return String.format("testId:%s:problemNumber:%s", tempCodeDto.testId, tempCodeDto.getProblemNumber()); } }