Skip to content

Commit c908bb9

Browse files
committed
feat: 진행 중인 채팅 조회 시에 timeAgo 필드 추가
1 parent 926986e commit c908bb9

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

src/main/java/shinhan/click/domain/chat/dto/response/ChatPreview.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,49 @@
22

33
import shinhan.click.domain.chat.entity.Chat;
44

5+
import java.time.LocalDateTime;
6+
import java.time.format.DateTimeFormatter;
7+
import java.time.temporal.ChronoUnit;
8+
import java.util.Locale;
9+
510
public record ChatPreview(
611
Long chatId,
712
String characterImageUrl,
813
String characterName,
914
String storyTitle,
10-
String lastChat
15+
String lastChat,
16+
String timeAgo
1117
) {
1218
public static ChatPreview from(Chat chat) {
1319
return new ChatPreview(
1420
chat.getId(),
1521
chat.getCharacter().getCharacterImageUrl(),
1622
chat.getCharacter().getName(),
1723
chat.getCharacter().getStory().getTitle(),
18-
chat.getMessages().get(chat.getMessages().size() - 1).getContent()
24+
chat.getMessages().get(chat.getMessages().size() - 1).getContent(),
25+
getTimeAgo(chat.getMessages().get(chat.getMessages().size() - 1).getCreatedAt())
1926
);
2027
}
28+
29+
private static String getTimeAgo(LocalDateTime createdAt) {
30+
LocalDateTime now = LocalDateTime.now();
31+
long days = ChronoUnit.DAYS.between(createdAt.toLocalDate(), now.toLocalDate());
32+
33+
if (days == 0) {
34+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("a h:mm")
35+
.withLocale(Locale.KOREA);
36+
return createdAt.format(formatter);
37+
} else if (days == 1) {
38+
return "1일 전";
39+
} else if (days < 30) {
40+
return days + "일 전";
41+
} else if (days < 365) {
42+
long months = days / 30;
43+
return months + "개월 전";
44+
} else {
45+
long years = days / 365;
46+
return years + "년 전";
47+
}
48+
}
49+
2150
}

0 commit comments

Comments
 (0)