Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] [REFACTOR] 논문 집권호 데이터 형식 수정 및 게시글 등록일 필드 추가 #197

Merged
merged 12 commits into from
Dec 8, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.example.backend.board.domain.entity.Board;
import org.example.backend.common.utils.TimeParsingUtils;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -16,18 +17,20 @@ public class BoardResDto {
private String content;
private String writer;
private List<String> fileList;
private LocalDateTime createDate;
private String createDate;
private int viewCount;
private String category;

@Builder
private BoardResDto(Long id, String title, String content, String writer,
List<String> fileList, LocalDateTime createDate, String category) {
List<String> fileList, String createDate, int viewCount, String category) {
this.id = id;
this.title = title;
this.content = content;
this.writer = writer;
this.fileList = fileList;
this.createDate = createDate;
this.viewCount = viewCount;
this.category = category;
}

Expand All @@ -38,7 +41,8 @@ public static BoardResDto of(Board board) {
.content(board.getContent())
.writer(board.getWriter())
.fileList(board.getFileList())
.createDate(board.getCreatedDateTime())
.createDate(TimeParsingUtils.toRelativeTimeFormat(board.getCreatedDateTime()))
.viewCount(board.getViewCount())
.category(board.getCategory().name())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class Board extends BaseEntity {
@Column(name = "file_list", length = 1000)
private List<String> fileList;

@Column(name = "created_date_time")
@Column(name = "created_date")
private LocalDateTime createdDateTime;

@Enumerated(EnumType.STRING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class TimeParsingUtils {
private static final int SEC = 60;
private static final int MIN = 60;
private static final int HOUR = 24;
private static final int DAY = 30;
private static final int MONTH = 12;

public static LocalDateTime formatterLocalDateTime(String time) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
Expand All @@ -14,4 +20,32 @@ public static String formatterString(LocalDateTime time) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
return time.format(formatter);
}

public static String toRelativeTimeFormat(LocalDateTime localDateTime) {
LocalDateTime now = LocalDateTime.now();
long diffTime = localDateTime.until(now, ChronoUnit.SECONDS);

if (diffTime < SEC) {
return diffTime + "초 전";
}
diffTime = diffTime / SEC;
if (diffTime < MIN) {
return diffTime + "분 전";
}
diffTime = diffTime / MIN;
if (diffTime < HOUR) {
return diffTime + "시간 전";
}
diffTime = diffTime / HOUR;
if (diffTime < DAY) {
return diffTime + "일 전";
}
diffTime = diffTime / DAY;
if (diffTime < MONTH) {
return diffTime + "개월 전";
}

diffTime = diffTime / MONTH;
return diffTime + "년 전";
}
}
Loading
Loading