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

게시글 응답 DTO에 마감 여부 필드 추가 #315

Merged
merged 1 commit into from
Apr 27, 2024
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
14 changes: 11 additions & 3 deletions src/main/java/balancetalk/module/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import balancetalk.module.member.domain.Member;
import balancetalk.module.report.domain.Report;
import jakarta.persistence.*;
import jakarta.validation.constraints.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.PositiveOrZero;
import jakarta.validation.constraints.Size;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Formula;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.annotations.Formula;

@Entity
@Getter
Expand Down Expand Up @@ -93,7 +97,7 @@ public long commentsCount() {
}
return comments.size();
}

@PrePersist
public void init() {
this.views = 0L;
Expand All @@ -115,4 +119,8 @@ public boolean hasDeadlineExpired() {
public void increaseViews() {
views++;
}

public boolean isClosed() {
return LocalDateTime.now().isAfter(deadline);
}
}
22 changes: 14 additions & 8 deletions src/main/java/balancetalk/module/post/dto/PostResponse.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package balancetalk.module.post.dto;

import balancetalk.module.ViewStatus;
import balancetalk.module.file.domain.File;
import balancetalk.module.member.domain.Member;
import balancetalk.module.post.domain.BalanceOption;
import balancetalk.module.post.domain.Post;
import balancetalk.module.post.domain.PostCategory;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
Expand All @@ -30,6 +32,9 @@ public class PostResponse {
@Schema(description = "투료 종료 기한", example = "2024/12/25 15:30:00")
private LocalDateTime deadline;

@Schema(description = "마감 여부", example = "true")
private Boolean isClosed;

@Schema(description = "게시글 조회수", example = "126")
private long views;

Expand Down Expand Up @@ -86,6 +91,7 @@ public static PostResponse fromEntity(Post post, Member member, boolean myLike,
.id(post.getId())
.title(post.getTitle())
.deadline(post.getDeadline())
.isClosed(post.isClosed())
.views(post.getViews())
// .viewStatus(post.getViewStatus())
.likesCount(post.likesCount())
Expand Down Expand Up @@ -140,11 +146,11 @@ private static Long getSelectedOptionId(Post post, Member member) {
.orElse(null);
}

private static int getTotalVotes (Post post){
return Optional.ofNullable(post.getOptions())
.map(options -> options.stream()
.mapToInt(option -> Optional.ofNullable(option.getVotes()).map(List::size).orElse(0))
.sum())
.orElse(0);
private static int getTotalVotes(Post post) {
return Optional.ofNullable(post.getOptions())
.map(options -> options.stream()
.mapToInt(option -> Optional.ofNullable(option.getVotes()).map(List::size).orElse(0))
.sum())
.orElse(0);
}
}