Skip to content

Commit

Permalink
add: added correction count
Browse files Browse the repository at this point in the history
  • Loading branch information
thguss committed Nov 24, 2024
1 parent 98e3fb1 commit f74a9f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import com.smeem.application.port.input.dto.response.diary.RetrieveDiariesResponse;
import com.smeem.application.port.input.dto.response.diary.RetrieveDiaryResponse;
import com.smeem.application.port.input.dto.response.diary.WriteDiaryResponse;
import com.smeem.application.port.output.cache.CachePort;
import com.smeem.application.port.output.persistence.*;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -31,6 +33,7 @@ public class DiaryService implements DiaryUseCase {
private final MemberBadgePort memberBadgePort;
private final TopicPort topicPort;
private final SmeemProperties smeemProperties;
private final CachePort cachePort;

@Transactional
public WriteDiaryResponse writeDiary(long memberId, WriteDiaryRequest request) {
Expand Down Expand Up @@ -68,7 +71,20 @@ public RetrieveDiaryResponse retrieveDiary(long diaryId) {
Topic topic = diary.getTopicId() != null ? topicPort.findById(diary.getTopicId()) : null;
Member member = memberPort.findById(diary.getMemberId());
List<Correction> corrections = correctionPort.findByDiary(diaryId);
return RetrieveDiaryResponse.of(diary, topic, member, corrections);
int correctionCount = getCorrectionCount(member.getId());
return RetrieveDiaryResponse.of(diary, topic, member, corrections, correctionCount);
}

private int getCorrectionCount(long memberId) {
LocalDate now = LocalDate.now();
String today = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String key = "correction:" + today + ":" + memberId;
return cachePort.getInt(key)
.orElseGet(() -> {
int count = correctionPort.countDistinctByMemberAndDate(memberId, now);
cachePort.setInt(key, count);
return count;
});
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ public record RetrieveDiaryResponse(
@Schema(description = "일기 작성자 닉네임")
String username,
@Schema(description = "코칭 결과 정보")
List<Correction> corrections
List<Correction> corrections,
@Schema(description = "코칭 횟수")
int correctionCount,
@Schema(description = "코칭 최대 횟수")
int correctionMaxCount
) {
public static RetrieveDiaryResponse of(
@NotNull Diary diary,
Topic topic,
@NotNull Member member,
@NotNull List<Correction> corrections
@NotNull List<Correction> corrections,
int correctionCount
) {
return RetrieveDiaryResponse.builder()
.diaryId(diary.getId())
Expand All @@ -41,6 +46,8 @@ public static RetrieveDiaryResponse of(
.createdAt(SmeemConverter.toString(diary.getCreatedAt()))
.username(member.getUsername())
.corrections(corrections)
.correctionCount(correctionCount)
.correctionMaxCount(1)
.build();
}
}
Expand Down

0 comments on commit f74a9f7

Please sign in to comment.