Skip to content

Commit

Permalink
Merge pull request #598 from SWM-Morandi/dev
Browse files Browse the repository at this point in the history
🐛 Fix 문제순서 오류 해결
  • Loading branch information
miiiinju1 authored Dec 8, 2023
2 parents 5b4fca2 + 4a03f09 commit 74292b0
Showing 1 changed file with 34 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand All @@ -51,23 +52,24 @@ public CustomTestResponses generateCustomTest(Long memberId, CustomTestRequest c
validateMemberId(memberId);
List<Member> members = getMembers(customTestRequest);
List<Tests> tests = createTests(customTestRequest, members);

tests.forEach(test -> {
String testingKey = createTestingKey(test.getMember().getMemberId());
storeTestInfoInRedis(testingKey, test, customTestRequest);
List<AttemptProblem> attemptProblems = createAttemptProblems(customTestRequest, test);
attemptProblemRepository.saveAll(attemptProblems);
setupTempCode(test, customTestRequest);
});

final List<CustomTestResponse> customTestResponses = tests.stream().map(test -> CustomTestResponse
.builder()
.testTypename(test.getTestTypename())
.customTestId(test.getTestId())
.bojId(test.getMember().getBojId())
.memberId(test.getMember().getMemberId())
.build())
.collect(Collectors.toList());
List<Problem> problems = getProblemsInOrder(customTestRequest.getBojProblems());

final List<CustomTestResponse> customTestResponses =
tests.stream().map(test -> {
String testingKey = createTestingKey(test.getMember().getMemberId());
storeTestInfoInRedis(testingKey, test, customTestRequest);
List<AttemptProblem> attemptProblems = createAttemptProblems(customTestRequest, test,problems);
attemptProblemRepository.saveAll(attemptProblems);
setupTempCode(test, customTestRequest);

return CustomTestResponse
.builder()
.testTypename(test.getTestTypename())
.customTestId(test.getTestId())
.bojId(test.getMember().getBojId())
.memberId(test.getMember().getMemberId())
.build();
}).collect(Collectors.toList());

return CustomTestResponses.builder()
.customTests(customTestResponses).build();
Expand Down Expand Up @@ -99,6 +101,18 @@ private List<Tests> createTests(CustomTestRequest customTestRequest, List<Member
testRepository.saveAll(tests);
return tests;
}

private List<Problem> getProblemsInOrder(List<Long> bojProblemIds) {
List<Problem> problems = problemRepository.findAllByBojProblemIdIn(bojProblemIds);

Map<Long, Problem> idToProblemMap = problems.stream()
.collect(Collectors.toMap(Problem::getBojProblemId, problem -> problem));

return bojProblemIds.stream()
.map(idToProblemMap::get)
.collect(Collectors.toList());
}

private void clearTestInfoInRedis(List<Member> members) {
for (Member member : members) {
String testingKey = createTestingKey(member.getMemberId());
Expand All @@ -120,14 +134,15 @@ private void storeTestInfoInRedis(String testingKey, Tests test, CustomTestReque
redisTemplate.opsForValue().set(testingKey, testInfo);
}

private List<AttemptProblem> createAttemptProblems(CustomTestRequest customTestRequest, Tests test) {
final List<Problem> problems = problemRepository.findAllByBojProblemIdIn(customTestRequest.getBojProblems());

private List<AttemptProblem> createAttemptProblems(CustomTestRequest customTestRequest, Tests test, List<Problem> problems) {
return problems.stream()
.map(problem -> {
final AttemptProblem attemptProblem = AttemptProblem.builder()
.testDate(customTestRequest.getStartTime().toLocalDate())
.member(test.getMember())
.problem(problem)
.isSolved(false)
.test(test)
.build();
attemptProblem.setTest(test);
Expand Down

0 comments on commit 74292b0

Please sign in to comment.