Skip to content

Commit

Permalink
Merge pull request #197 from urinaner/refactor/186
Browse files Browse the repository at this point in the history
[BE] [REFACTOR] 논문 집권호 데이터 형식 수정 및 게시글 등록일 필드 추가
  • Loading branch information
urinaner authored Dec 8, 2024
2 parents 4c44837 + 71987f5 commit 328846d
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 178 deletions.
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

0 comments on commit 328846d

Please sign in to comment.