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

feat: 게시글 목록 조회 구현 #158

Merged
merged 19 commits into from
Jan 15, 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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
implementation 'com.mysql:mysql-connector-j'
implementation 'org.jsoup:jsoup:1.15.3'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.3'
runtimeOnly 'com.h2database:h2'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package in.koreatech.koin.domain.community.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import in.koreatech.koin.domain.community.dto.ArticlesResponse;
import in.koreatech.koin.domain.community.service.CommunityService;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class CommunityController {

private final CommunityService communityService;

@GetMapping("/articles")
public ResponseEntity<ArticlesResponse> getArticles(
@RequestParam Long boardId,
@RequestParam(required = false) Long page,
@RequestParam(required = false) Long limit
) {
ArticlesResponse foundArticles = communityService.getArticles(boardId, page, limit);
return ResponseEntity.ok().body(foundArticles);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package in.koreatech.koin.domain.community.dto;

import java.time.LocalDateTime;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import in.koreatech.koin.domain.community.model.Article;
import in.koreatech.koin.domain.community.model.Board;

public record ArticlesResponse(
List<InnerArticleResponse> articles,
InnerBoardResponse board,
Long totalPage
) {

public static ArticlesResponse of(List<Article> articles, Board board, Long totalPage) {
return new ArticlesResponse(
articles.stream()
.map(InnerArticleResponse::from)
.toList(),
InnerBoardResponse.from(board),
totalPage
);
}

@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
private record InnerArticleResponse(
Long id,
Long boardId,
String title,
String content,
Long userId,
String nickname,
Long hit, String ip,
Boolean isSolved,
Boolean isDeleted,
Byte commentCount,
String meta,
Boolean isNotice,
Long noticeArticleId,
String summary,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime createdAt,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updatedAt,
@JsonProperty("contentSummary") String contentSummary
) {

public static InnerArticleResponse from(Article article) {
return new InnerArticleResponse(
article.getId(),
article.getBoardId(),
article.getTitle(),
article.getContent(),
article.getUserId(),
article.getNickname(),
article.getHit(),
article.getIp(),
article.getIsSolved(),
article.getIsDeleted(),
article.getCommentCount(),
article.getMeta(),
article.getIsNotice(),
article.getNoticeArticleId(),
article.getSummary(),
article.getCreatedAt(),
article.getUpdatedAt(),
article.getContentSummary()
);
}
}

@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
public record InnerBoardResponse(
Long id,
String tag,
String name,
Boolean isAnonymous,
Long articleCount,
Boolean isDeleted,
Boolean isNotice,
Long parentId,
Long seq,
List<InnerBoardResponse> children,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime createdAt,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updatedAt
) {

public static InnerBoardResponse from(Board board) {
return new InnerBoardResponse(
board.getId(),
board.getTag(),
board.getName(),
board.getIsAnonymous(),
board.getArticleCount(),
board.getIsDeleted(),
board.getIsNotice(),
board.getParentId(),
board.getSeq(),
board.getChildren().isEmpty()
? null : board.getChildren().stream().map(InnerBoardResponse::from).toList(),
board.getCreatedAt(),
board.getUpdatedAt()
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package in.koreatech.koin.domain.community.exception;

public class ArticleNotFoundException extends RuntimeException {
private static final String DEFAULT_MESSAGE = "게시글이 존재하지 않습니다.";

public ArticleNotFoundException(String message) {
super(message);
}

public static ArticleNotFoundException withDetail(String detail) {
String message = String.format("%s %s", DEFAULT_MESSAGE, detail);
return new ArticleNotFoundException(message);
}
}
127 changes: 127 additions & 0 deletions src/main/java/in/koreatech/koin/domain/community/model/Article.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package in.koreatech.koin.domain.community.model;

import org.hibernate.annotations.Where;
import org.jsoup.Jsoup;

import in.koreatech.koin.global.common.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Lob;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name = "articles")
@Where(clause = "is_deleted=0")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Article extends BaseEntity {

private static final int SUMMARY_MIN_LENGTH = 0;
private static final int SUMMARY_MAX_LENGTH = 100;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;

@NotNull
@Column(name = "board_id", nullable = false)
private Long boardId;

@Size(max = 255)
@NotNull
@Column(name = "title", nullable = false)
private String title;

@NotNull
@Lob
@Column(name = "content", nullable = false)
private String content;

@NotNull
@Column(name = "user_id", nullable = false)
private Long userId;

@Size(max = 50)
@NotNull
@Column(name = "nickname", nullable = false, length = 50)
private String nickname;

@NotNull
@Column(name = "hit", nullable = false)
private Long hit;

@Size(max = 45)
@NotNull
@Column(name = "ip", nullable = false, length = 45)
private String ip;

@NotNull
@Column(name = "is_solved", nullable = false)
private Boolean isSolved = false;

@NotNull
@Column(name = "is_deleted", nullable = false)
private Boolean isDeleted = false;

@NotNull
@Column(name = "comment_count", nullable = false)
private Byte commentCount;

@Lob
@Column(name = "meta")
private String meta;

@NotNull
@Column(name = "is_notice", nullable = false)
private Boolean isNotice = false;

@Column(name = "notice_article_id")
private Long noticeArticleId;

@Transient
private String summary;

public String getContentSummary() {
if (content == null) {
return "";
}
String contentSummary = Jsoup.parse(content).text();
contentSummary = contentSummary.replace("&nbsp", "").strip();
if (contentSummary.length() < SUMMARY_MAX_LENGTH) {
return contentSummary;
}
return contentSummary.substring(SUMMARY_MIN_LENGTH, SUMMARY_MAX_LENGTH);
}

@Builder
private Article(Long boardId, String title, String content, Long userId, String nickname, Long hit,
String ip, Boolean isSolved, Boolean isDeleted, Byte commentCount, String meta, Boolean isNotice,
Long noticeArticleId) {
this.boardId = boardId;
this.title = title;
this.content = content;
this.userId = userId;
this.nickname = nickname;
this.hit = hit;
this.ip = ip;
this.isSolved = isSolved;
this.isDeleted = isDeleted;
this.commentCount = commentCount;
this.meta = meta;
this.isNotice = isNotice;
this.noticeArticleId = noticeArticleId;
}
}
84 changes: 84 additions & 0 deletions src/main/java/in/koreatech/koin/domain/community/model/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package in.koreatech.koin.domain.community.model;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.annotations.Where;

import in.koreatech.koin.global.common.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name = "boards")
@Where(clause = "is_deleted=0")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C

SoftDelte를 고려한 Where를 적용해주셨군요! 👍
다만 한가지 우려되는 점이 있습니다.
해당 엔티티에 SoftDelete가 되어있지 않은 엔티티만 찾는다면 Admin이라는 상황에서는 한가지 유의해야할 수도 있다고 생각이 드네요!
Admin 페이지에서는 SoftDelete되어있는 게시물 목록을 봐야한다 와 같은 요구사항이 생긴다면 해당 어노테이션을 어떻게 제어해야할지 고민을 많이 해야할거같아요

어떻게 생각하시나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FilterDef@Filter를 사용하는 방법을 찾았습니다!

@Where를 사용하면 SoftDelete 여부에 따라 한 가지 경우만 조회가 가능합니다. 하지만 위의 기능을 사용하면 두 경우를 모두 조회하는 것이 가능합니다.

Article.java

@FilterDef(name = "deletedArticleFilter", parameters = @ParamDef(name = "isDeleted", type = Boolean.class))
@Filter(name = "deletedArticleFilter", condition = "is_deleted=:isDeleted")
public class Article extends BaseEntity {
    ...
}

CommunityService.java

    private ArticlesResponse findArticlesByBoardId(Long boardId, Long page, Long limit, boolean isAdmin) {
        Session session = em.unwrap(Session.class);
        Filter filter = session.enableFilter("deletedArticleFilter");
        filter.setParameter("isDeleted", isAdmin);
        ArticlesResponse getArticlesResponse = findArticlesByBoardId(boardId, page, limit);
        session.disableFilter("deletedArticleFilter");
        return getArticlesResponse;
    }

실제로 코드를 수정하여 정상 동작을 확인했는데, 이 내용을 푸쉬하는 것이 맞을까요? admin용 조회 메서드를 함께 작성해두는 것이 문서화 등 여러모로 도움이 될 것 같으나 불필요한 코드의 추가라는 점이 우려되기도 합니다.

참고) https://www.baeldung.com/spring-jpa-soft-delete#how-to-get-the-deleted-data:~:text=4.%20How%20to%20Get%20the%20Deleted%20Data%3F

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

불필요한 코드라는 부분에서 동의합니다~
추후 해당 내용이 필요할 경우 말씀해주신 Filter 기능을 사용하면 될 것 같습니다.
해결 방식을 찾아뒀으니 해당 내용을 Discussion에 다시 게시해두고 나중에 개발할 때 확인하면 좋겠네요!
discussion에 게시 부탁드려도될까요~?

Copy link
Collaborator Author

@songsunkook songsunkook Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Board extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;

@Size(max = 10)
@NotNull
@Column(name = "tag", nullable = false, length = 10)
private String tag;

@Size(max = 50)
@NotNull
@Column(name = "name", nullable = false, length = 50)
private String name;

@NotNull
@Column(name = "is_anonymous", nullable = false)
private Boolean isAnonymous = false;

@NotNull
@Column(name = "article_count", nullable = false)
private Long articleCount;

@NotNull
@Column(name = "is_deleted", nullable = false)
private Boolean isDeleted = false;

@NotNull
@Column(name = "is_notice", nullable = false)
private Boolean isNotice = false;

@Column(name = "parent_id")
private Long parentId;

@NotNull
@Column(name = "seq", nullable = false)
private Long seq;

public List<Board> getChildren() {
return new ArrayList<>();
}

@Builder
private Board(String tag, String name, Boolean isAnonymous, Long articleCount, Boolean isDeleted,
Boolean isNotice, Long parentId, Long seq) {
this.tag = tag;
this.name = name;
this.isAnonymous = isAnonymous;
this.articleCount = articleCount;
this.isDeleted = isDeleted;
this.isNotice = isNotice;
this.parentId = parentId;
this.seq = seq;
}
}
Loading
Loading