-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
✨ [FEAT] 시험 다시 풀기 시작 API #562
- Loading branch information
Showing
15 changed files
with
258 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/swm_nm/morandi/domain/codeSubmit/dto/SubmitDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package swm_nm.morandi.domain.codeSubmit.dto; | ||
|
||
public interface SubmitDto { | ||
|
||
String getBojProblemId(); | ||
String getLanguageId(); | ||
|
||
String getSourceCode(); | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/java/swm_nm/morandi/domain/testInfo/repository/TestRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
package swm_nm.morandi.domain.testInfo.repository; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.EntityGraph; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import swm_nm.morandi.domain.testDuring.dto.TestStatus; | ||
import swm_nm.morandi.domain.testInfo.entity.Tests; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface TestRepository extends JpaRepository<Tests, Long>{ | ||
//Paging하여 테스트 기록을 가져옴 | ||
Page<Tests> findAllTestsByMember_MemberIdAndTestStatus(Long memberId, TestStatus testStatus, Pageable pageable); | ||
//1년동안의 테스트 기록을 가져와서 레이팅 반환에 사용함 | ||
List<Tests> findAllTestsByMember_MemberIdAndTestStatusAndTestDateAfterOrderByTestDateAsc(Long memberId, TestStatus testStatus, LocalDateTime oneYearAgo); | ||
Long countByMember_MemberIdAndTestStatus(Long memberId, TestStatus testStatus); | ||
|
||
Optional<Tests> findTestByTestIdAndMember_MemberId(Long testId, Long memberId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/swm_nm/morandi/domain/testRetry/controller/TestRetryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package swm_nm.morandi.domain.testRetry.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.*; | ||
import swm_nm.morandi.domain.testInfo.dto.TestDto; | ||
import swm_nm.morandi.domain.testRetry.request.RetryTestRequest; | ||
import swm_nm.morandi.domain.testRetry.response.TestRetryResponse; | ||
import swm_nm.morandi.domain.testRetry.service.TestRetryService; | ||
|
||
@RestController | ||
@RequestMapping("/tests") | ||
@RequiredArgsConstructor | ||
@Tag(name = "TestRetryController", description = "테스트 다시 풀기와 관련된 컨트롤러") | ||
public class TestRetryController { | ||
|
||
private final TestRetryService testRetryService; | ||
|
||
@PostMapping("/retry") | ||
@ResponseStatus(HttpStatus.OK) | ||
@Operation(summary = "테스트 다시 풀기", description = "테스트 다시 풀기") | ||
public TestRetryResponse retryTest(@RequestBody RetryTestRequest retryTestRequest) { | ||
return testRetryService.retryTest(retryTestRequest); | ||
} | ||
|
||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/swm_nm/morandi/domain/testRetry/request/RetryTestRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package swm_nm.morandi.domain.testRetry.request; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter @Setter | ||
public class RetryTestRequest { | ||
private Long testId; | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/swm_nm/morandi/domain/testRetry/response/RetryAttemptProblemResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package swm_nm.morandi.domain.testRetry.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import swm_nm.morandi.domain.common.Language; | ||
|
||
@Builder | ||
@Getter | ||
@Slf4j | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class RetryAttemptProblemResponse { | ||
private Long bojProblemId; | ||
private String pythonCode; | ||
private String javaCode; | ||
private String cppCode; | ||
private Language lastAccessCode; | ||
|
||
public void initialRetryAttemptProblemResponse(String code, Language language, Long bojProblemId) { | ||
switch (language) { | ||
case Python -> this.pythonCode = code; | ||
case Cpp -> this.cppCode = code; | ||
case Java -> this.javaCode = code; | ||
} | ||
this.bojProblemId = bojProblemId; | ||
this.lastAccessCode = language; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/swm_nm/morandi/domain/testRetry/response/TestRetryResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package swm_nm.morandi.domain.testRetry.response; | ||
|
||
import lombok.*; | ||
import swm_nm.morandi.domain.common.Language; | ||
import swm_nm.morandi.domain.testDuring.dto.TempCodeDto; | ||
import swm_nm.morandi.domain.testStart.dto.BojProblemDto; | ||
import swm_nm.morandi.domain.testStart.dto.TestCodeDto; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
public class TestRetryResponse { | ||
private Long testId; | ||
private List<RetryAttemptProblemResponse> retryAttemptProblems; | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/swm_nm/morandi/domain/testRetry/service/TestRetryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package swm_nm.morandi.domain.testRetry.service; | ||
|
||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.cache.annotation.CachePut; | ||
import org.springframework.data.redis.core.HashOperations; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Service; | ||
import swm_nm.morandi.domain.testDuring.dto.TempCodeDto; | ||
import swm_nm.morandi.domain.testDuring.dto.factory.TempCodeFactory; | ||
import swm_nm.morandi.domain.testInfo.entity.AttemptProblem; | ||
import swm_nm.morandi.domain.testInfo.entity.Tests; | ||
import swm_nm.morandi.domain.testInfo.repository.TestRepository; | ||
import swm_nm.morandi.domain.testRecord.repository.AttemptProblemRepository; | ||
import swm_nm.morandi.domain.testRetry.request.RetryTestRequest; | ||
import swm_nm.morandi.domain.testRetry.response.RetryAttemptProblemResponse; | ||
import swm_nm.morandi.domain.testRetry.response.TestRetryResponse; | ||
import swm_nm.morandi.global.exception.MorandiException; | ||
import swm_nm.morandi.global.exception.errorcode.TestErrorCode; | ||
import swm_nm.morandi.global.utils.SecurityUtils; | ||
import swm_nm.morandi.redis.utils.RedisKeyGenerator; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class TestRetryService { | ||
|
||
private final TestRepository testRepository; | ||
|
||
private final RedisKeyGenerator redisKeyGenerator; | ||
|
||
private final RedisTemplate<String, Object> redisTemplate; | ||
|
||
private final TempCodeFactory tempCodeFactory; | ||
private final Long expireTime = 60000L; | ||
|
||
public TestRetryResponse retryTest(RetryTestRequest retryTestRequest) { | ||
Tests test = testRepository.findTestByTestIdAndMember_MemberId(retryTestRequest.getTestId(), SecurityUtils.getCurrentMemberId()) | ||
.orElseThrow(() -> new MorandiException(TestErrorCode.TEST_NOT_FOUND)); | ||
|
||
|
||
List<RetryAttemptProblemResponse> attemptProblemResponses = | ||
test.getAttemptProblems().stream() | ||
.map(attemptProblem -> { | ||
RetryAttemptProblemResponse attemptProblemResponse = tempCodeFactory.getRetryAttemptProblemResponse(); | ||
attemptProblemResponse.initialRetryAttemptProblemResponse(attemptProblem.getSubmitCode(), | ||
attemptProblem.getSubmitLanguage(), | ||
attemptProblem.getProblem().getBojProblemId()); | ||
return attemptProblemResponse; | ||
}).toList(); | ||
|
||
|
||
|
||
saveCodeToRedis(retryTestRequest.getTestId(), attemptProblemResponses); | ||
|
||
return TestRetryResponse.builder() | ||
.testId(retryTestRequest.getTestId()) | ||
.retryAttemptProblems(attemptProblemResponses) | ||
.build(); | ||
|
||
} | ||
|
||
private void saveCodeToRedis(Long testId, List<RetryAttemptProblemResponse> attemptProblemResponses) { | ||
String retryTestTempCodeKey = redisKeyGenerator.generateRetryTestTempCodeKey(testId); | ||
|
||
HashOperations<String, String, RetryAttemptProblemResponse> hashOps = redisTemplate.opsForHash(); | ||
int problemCount = attemptProblemResponses.size(); | ||
IntStream.rangeClosed(1, problemCount).forEach(problemNumber -> | ||
hashOps.put(retryTestTempCodeKey, | ||
String.valueOf(problemNumber), | ||
attemptProblemResponses.get(problemNumber-1)) | ||
); | ||
redisTemplate.expire(retryTestTempCodeKey, expireTime, TimeUnit.MINUTES); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters