-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: 게시글 목록 조회 구현 #158
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
549a96c
feat: 골격 작성
songsunkook 25e0647
feat: 엔티티 작성
songsunkook 2225d5b
feat: repository 작성
songsunkook 4e028f5
feat: service 작성
songsunkook 7381c4d
feat: response 작성
songsunkook 61b1757
feat: DB 조회 로직 작성
songsunkook 86556c8
feat: Jsoup 의존성 추가
songsunkook 48bf5e7
feat: 응답 내용 추가
songsunkook a0edf01
feat: 예외처리 작성
songsunkook e065e3d
refactor: 페이징 관련 정보 클래스 분리
songsunkook a8e7de3
fix: soft delete된 게시판 or 게시글은 조회 안되도록 수정
songsunkook 8fcdb31
fix: 1페이지 조회 안되는 버그 수정
songsunkook 90291fc
fix: 누락된 응답 추가 (summary)
songsunkook e781492
fix: 최신글부터 조회하도록 수정
songsunkook a774d36
test: 테스트 코드 작성
songsunkook 4372928
move: ErrorResponseWrapper 위치 변경
songsunkook 9997bd2
refactor: 코딩 컨벤션 준수
songsunkook 2baaaf6
refactor: 문자열 파싱 메서드 리팩토링
songsunkook 3399cbe
test: 예외상황 테스트 추가
songsunkook File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/in/koreatech/koin/domain/community/controller/CommunityController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
src/main/java/in/koreatech/koin/domain/community/dto/ArticlesResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/in/koreatech/koin/domain/community/exception/ArticleNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
127
src/main/java/in/koreatech/koin/domain/community/model/Article.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(" ", "").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
84
src/main/java/in/koreatech/koin/domain/community/model/Board.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
@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; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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되어있는 게시물 목록을 봐야한다
와 같은 요구사항이 생긴다면 해당 어노테이션을 어떻게 제어해야할지 고민을 많이 해야할거같아요어떻게 생각하시나요?
There was a problem hiding this comment.
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
CommunityService.java
실제로 코드를 수정하여 정상 동작을 확인했는데, 이 내용을 푸쉬하는 것이 맞을까요? admin용 조회 메서드를 함께 작성해두는 것이 문서화 등 여러모로 도움이 될 것 같으나 불필요한 코드의 추가라는 점이 우려되기도 합니다.
참고) https://www.baeldung.com/spring-jpa-soft-delete#how-to-get-the-deleted-data:~:text=4.%20How%20to%20Get%20the%20Deleted%20Data%3F
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
불필요한 코드라는 부분에서 동의합니다~
추후 해당 내용이 필요할 경우 말씀해주신 Filter 기능을 사용하면 될 것 같습니다.
해결 방식을 찾아뒀으니 해당 내용을 Discussion에 다시 게시해두고 나중에 개발할 때 확인하면 좋겠네요!
discussion에 게시 부탁드려도될까요~?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
게시 완료했습니다!