Skip to content

Commit

Permalink
Merge pull request #354 from SWM-NM/feat/#347
Browse files Browse the repository at this point in the history
[FIX] 최근 4개 테스트 정보 제공
  • Loading branch information
aj4941 authored Sep 23, 2023
2 parents f98bbf4 + 6c7d8ab commit 8379a40
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ dependencies {

implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.13'


implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public ResponseEntity<MemberInfoDto> memberInfo() {
}
@PostMapping("/edit")
@Operation(summary = "사용자 정보 수정", description = "사용자의 닉네임 또는 백준 아이디를 수정합니다.")
public ResponseEntity<MemberInfoDto> editProfile(@RequestBody MemberInfoDto memberInfoDto) throws IOException {
// String thumbPhoto = memberService.editThumbPhoto(memberId, profileRequestDto.getThumbPhotoFile());
public ResponseEntity<MemberInfoDto> editProfile(@RequestBody MemberInfoDto memberInfoDto) {
memberEditService.editProfile(memberInfoDto.getIntroduceInfo(), memberInfoDto.getBojId());
return new ResponseEntity(memberInfoDto, HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class TestInfoController {

private final TestTypeInfoService testTypeInfoService;
@GetMapping("/latest")
@Operation(summary = "최근에 본 테스트 목록", description = "메인 페이지에서 최근에 본 테스트 5개를 제공합니다.")
@Operation(summary = "최근에 본 테스트 목록", description = "마이 페이지에서 최근에 본 테스트 4개를 제공합니다.")
public ResponseEntity<List<TestRecordDto>> getLatestTestDtos() {
return new ResponseEntity<>(latestTestInfoService.getTestRecordDtosLatest(), HttpStatus.OK);
}
Expand All @@ -47,7 +47,7 @@ public ResponseEntity<List<TestTypeDto>> getRandomDefenseTestTypeDtos() {
return new ResponseEntity<>(randomDefenseInfoService.getRandomDefenseTestTypeDtos(), HttpStatus.OK);
}
@GetMapping("/{testTypeId}")
@Operation(summary = "테스트 상세 정보", description = "사용자가 원하는 테스트를 눌렀을 때 상세 테스트 정보를 제공합니다.")
@Operation(summary = "테스트 상세 정보", description = "사용자가 원하는 테스트 유형을 눌렀을 때 상세 테스트 정보를 제공합니다.")
public ResponseEntity<TestTypeDto> getTestTypeInfo(@PathVariable Long testTypeId) {
return new ResponseEntity<>(testTypeInfoService.getTestTypeDto(testTypeId), HttpStatus.OK);
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/swm_nm/morandi/domain/testInfo/entity/Tests.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package swm_nm.morandi.domain.testInfo.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.*;
import swm_nm.morandi.domain.common.BaseEntity;
import swm_nm.morandi.domain.member.entity.Member;
import swm_nm.morandi.domain.testDuring.dto.TestStatus;
Expand All @@ -20,6 +21,9 @@
public class Tests extends BaseEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long testId;

@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime testDate; // 테스트 시작 시간
private Long testTime; // minutes
private Integer problemCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import java.time.LocalDateTime;
import java.util.List;
public interface TestRepository extends JpaRepository<Tests, Long> {
List<Tests> findAllByMember_MemberIdOrderByTestDateDesc(Long memberId, Pageable pageable);

@Query("SELECT t FROM Tests t " +
"WHERE t.member.memberId = :memberId " +
"AND t.testStatus = 'COMPLETED'" +
" ORDER BY t.testDate DESC")
List<Tests> findDataRecent4(Long memberId, Pageable pageable);

//1년동안의 테스트 기록을 가져온다.
//이 때 테스트 날짜와 테스트 점수만 최근 날짜부터 가져온다.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
package swm_nm.morandi.domain.testInfo.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import swm_nm.morandi.domain.testExit.dto.AttemptProblemDto;
import swm_nm.morandi.domain.testInfo.entity.AttemptProblem;
import swm_nm.morandi.domain.testRecord.dto.TestRecordDto;
import swm_nm.morandi.domain.testInfo.entity.Tests;
import swm_nm.morandi.domain.testRecord.mapper.TestRecordMapper;
import swm_nm.morandi.domain.testInfo.repository.TestRepository;
import swm_nm.morandi.domain.testRecord.repository.AttemptProblemRepository;
import swm_nm.morandi.global.utils.SecurityUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class LatestTestInfoService {

private final TestRepository testRepository;

private final AttemptProblemRepository attemptProblemRepository;

public List<TestRecordDto> getTestRecordDtosLatest() {
Long memberId = SecurityUtils.getCurrentMemberId();
Pageable pageable = PageRequest.of(0, 5);
List<Tests> recentTests = testRepository.findAllByMember_MemberIdOrderByTestDateDesc(memberId, pageable);
List<TestRecordDto> testRecordDtos = recentTests.stream().map(TestRecordMapper::convertToDto).collect(Collectors.toList());
Pageable pageable = PageRequest.of(0, 4);
List<Tests> recentTests = testRepository.findDataRecent4(memberId, pageable);
List<TestRecordDto> testRecordDtos = new ArrayList<>();
recentTests.forEach(recentTest -> {
List<AttemptProblemDto> attemptProblemDtos = getAttemptProblemDtos(recentTest);
TestRecordDto testRecordDto = TestRecordMapper.convertToDto(recentTest, attemptProblemDtos);
testRecordDtos.add(testRecordDto);
});
getTestRecordDtos(testRecordDtos);
return testRecordDtos;
}
private List<AttemptProblemDto> getAttemptProblemDtos(Tests test) {
List<AttemptProblemDto> attemptProblemDtos = new ArrayList<>();
Long testId = test.getTestId();
List<AttemptProblem> attemptProblems = attemptProblemRepository.findAttemptProblemsByTest_TestId(testId);
long index = 1;
for (AttemptProblem attemptProblem : attemptProblems) {
AttemptProblemDto attemptProblemDto = AttemptProblemDto.getAttemptProblemDto(attemptProblem);
attemptProblemDto.setTestProblemId(index++);
attemptProblemDtos.add(attemptProblemDto);
}
return attemptProblemDtos;
}

private static void getTestRecordDtos(List<TestRecordDto> testRecordDtos) {
while (testRecordDtos.size() < 4) {
testRecordDtos.add(TestRecordMapper.dummyDto());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package swm_nm.morandi.domain.testRecord.dto;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.*;
import swm_nm.morandi.domain.problem.dto.DifficultyLevel;
import swm_nm.morandi.domain.testExit.dto.AttemptProblemDto;
Expand All @@ -13,12 +17,16 @@
@Builder
@ToString
public class TestRecordDto {
private Long testId;
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime testDate;
private Long testTime;
private Integer problemCount;
private DifficultyLevel startDifficulty;
private DifficultyLevel endDifficulty;
private String testTypename;
private Long testRating;
private Long originRating;
private List<AttemptProblemDto> attemptProblemDto;
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
package swm_nm.morandi.domain.testRecord.mapper;

import swm_nm.morandi.domain.problem.dto.DifficultyLevel;
import swm_nm.morandi.domain.testExit.dto.AttemptProblemDto;
import swm_nm.morandi.domain.testInfo.entity.Tests;
import swm_nm.morandi.domain.testRecord.dto.TestRecordDto;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class TestRecordMapper {
public static TestRecordDto convertToDto(Tests test) {
public static TestRecordDto convertToDto(Tests test, List<AttemptProblemDto> attemptProblemDtos) {

TestRecordDto testRecordDto = TestRecordDto.builder()
.testId(test.getTestId())
.testDate(test.getTestDate())
.testTime(test.getTestTime())
.problemCount(test.getProblemCount())
.startDifficulty(test.getStartDifficulty())
.endDifficulty(test.getEndDifficulty())
.testTypename(test.getTestTypename())
.testRating(test.getTestRating())
.originRating(test.getOriginRating())
.attemptProblemDto(attemptProblemDtos)
.build();

return testRecordDto;
}

public static TestRecordDto dummyDto() {
TestRecordDto testRecordDto = TestRecordDto.builder()
.testId(0L)
.testDate(LocalDateTime.now())
.testTime(0L)
.problemCount(0)
.startDifficulty(DifficultyLevel.B5)
.endDifficulty(DifficultyLevel.B5)
.testTypename("테스트가 없습니다.")
.testRating(0L)
.originRating(0L)
.attemptProblemDto(new ArrayList<>())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import swm_nm.morandi.global.exception.MorandiException;
import swm_nm.morandi.global.exception.errorcode.TestErrorCode;

import java.util.ArrayList;
import java.util.List;

@Service
Expand All @@ -25,29 +26,24 @@ public class TestDetailsService {
private final AttemptProblemRepository attemptProblemRepository;

public TestRecordDto getTestRecordDtoByTestId(Long testId) {

//TODO
//여기를 DB두번접근하지말고
// attemptProblemList에서 testId가 뭐 X인
// AttemptProblemList를 가져오도록 쿼리 짜면 DB 한 번으로 개선될 듯?

Tests test = testRepository.findById(testId).orElseThrow(()-> new MorandiException(TestErrorCode.TEST_NOT_FOUND));
TestRecordDto testRecordDto = TestRecordMapper.convertToDto(test);
List<AttemptProblem> attemptProblems
= attemptProblemRepository.findAllByTest_TestId(testId);
List<AttemptProblemDto> attemptProblemDtos = new ArrayList<>();
if (!attemptProblems.isEmpty()) {
long index = 1;
for (AttemptProblem attemptProblem : attemptProblems) {
AttemptProblemDto attemptProblemDto =
AttemptProblemDto.builder()
.testProblemId(index++)
.bojProblemId(attemptProblem.getProblem().getBojProblemId())
.isSolved(attemptProblem.getIsSolved())
.executionTime(attemptProblem.getExecutionTime())
.build();
testRecordDto.getAttemptProblemDto().add(attemptProblemDto);
AttemptProblemDto attemptProblemDto = AttemptProblemDto.getAttemptProblemDto(attemptProblem);
attemptProblemDto.setTestProblemId(index++);
attemptProblemDtos.add(attemptProblemDto);
}
}
TestRecordDto testRecordDto = TestRecordMapper.convertToDto(test, attemptProblemDtos);
return testRecordDto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@
@Slf4j
public class GetProblemsService {


private final TypeProblemListRepository typeProblemListRepository;

private final AlgorithmProblemListRepository algorithmProblemListRepository;

public void getProblemsByTestType(TestType testType, List<BojProblem> bojProblems) {
List<TypeProblemList> typeProblemLists = typeProblemListRepository.findByTestType_TestTypeId(testType.getTestTypeId());
List<Problem> problems = typeProblemLists.stream().map(TypeProblemList::getProblem).collect(Collectors.toList());
Expand Down

0 comments on commit 8379a40

Please sign in to comment.