Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.umc.finly.domain.analysis.association.converter.ConvictionScoreConverter;
import com.umc.finly.domain.analysis.association.dto.response.ConvictionScoreResDTO;
import com.umc.finly.domain.analysis.association.entity.ConvictionScoreResult;
import com.umc.finly.domain.analysis.association.enums.Status;
import com.umc.finly.domain.analysis.association.exception.code.ConvictionScoreErrorCode;
import com.umc.finly.domain.analysis.association.exception.ConvictionScoreException;
import com.umc.finly.domain.analysis.association.repository.ConvictionScoreResultRepository;
Expand All @@ -14,6 +15,7 @@

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ConvictionScoreServiceImpl implements ConvictionScoreService {

private final ConvictionScoreResultRepository convictionScoreResultRepository;
Expand All @@ -29,10 +31,16 @@ public void calculateAndSave(Long memberId, LocalDate start, LocalDate end) {
@Override
public ConvictionScoreResDTO getConvictionScore(Long memberId) {
// 가장 최신 매수 확신도 결과 조회
ConvictionScoreResult currentResult = convictionScoreResultRepository.findFirstByMemberIdOrderByEndDateDesc(memberId)
.orElseThrow(() -> new ConvictionScoreException(ConvictionScoreErrorCode.CONVICTION_SCORE_NOT_FOUND));

return ConvictionScoreConverter.toConvictionScoreResDTO(currentResult.getConvictionScore());
return convictionScoreResultRepository.findFirstByMemberIdOrderByEndDateDesc(memberId)
.map(currentResult ->
ConvictionScoreConverter.toConvictionScoreResDTO(currentResult.getConvictionScore())
)
// 결과 데이터가 없을 경우 기본값 반환
.orElseGet(() -> ConvictionScoreResDTO.builder()
.convictionScore(0)
.status(Status.LOW)
.phrase("분석을 위해 데이터를 모으는 중이에요")
.build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.umc.finly.domain.analysis.association.converter.FearIndexConverter;
import com.umc.finly.domain.analysis.association.dto.response.FearIndexResDTO;
import com.umc.finly.domain.analysis.association.entity.FearIndexResult;
import com.umc.finly.domain.analysis.association.enums.ChangeDirection;
import com.umc.finly.domain.analysis.association.exception.code.FearIndexErrorCode;
import com.umc.finly.domain.analysis.association.exception.FearIndexException;
import com.umc.finly.domain.analysis.association.repository.FearIndexResultRepository;
Expand All @@ -29,20 +30,24 @@ public void calculateAndSave(Long memberId, LocalDate startDate, LocalDate endDa

@Override
public FearIndexResDTO getFearIndex(Long memberId) {
// 1. 가장 최근에 저장된 분석 결과 조회
FearIndexResult currentResult = fearIndexResultRepository.findFirstByMemberIdOrderByEndDateDesc(memberId)
.orElseThrow(() -> new FearIndexException(FearIndexErrorCode.FEAR_INDEX_NOT_FOUND));

// 2. 해당 결과의 시작일(startDate)을 기준으로 그보다 이전의 마지막 데이터 조회 (변화량 비교용)
int prevScore = fearIndexResultRepository.findFirstByMemberIdAndEndDateBeforeOrderByEndDateDesc(
memberId, currentResult.getStartDate())
.map(FearIndexResult::getFearIndex)
.orElse(currentResult.getFearIndex()); // 이전 기록 없으면 현재와 동일 처리

// 3. 컨버터를 통해 DTO로 변환하여 반환
return FearIndexConverter.toFearIndexResDTO(
currentResult.getFearIndex(),
prevScore
);
// 가장 최근 분석 결과 조회
return fearIndexResultRepository.findFirstByMemberIdOrderByEndDateDesc(memberId)
.map(currentResult -> {
// [결과 데이터가 있는 경우]
// 이전 데이터 조회 로직 진행
int prevScore = fearIndexResultRepository.findFirstByMemberIdAndEndDateBeforeOrderByEndDateDesc(
memberId, currentResult.getStartDate())
.map(FearIndexResult::getFearIndex)
.orElse(currentResult.getFearIndex()); // 이전 기록 없으면 현재와 동일 처리

return FearIndexConverter.toFearIndexResDTO(currentResult.getFearIndex(), prevScore);
})
// [결과 데이터가 없는 경우]
.orElseGet(() -> FearIndexResDTO.builder()
.fearIndex(0)
.changeDirection(ChangeDirection.SAME)
.changeValue(0)
.phrase("분석을 위해 데이터를 모으는 중이에요")
.build());
}
}