-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor#post기능추가 #18
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
The head ref may contain hidden characters: "refactor#post\uAE30\uB2A5\uCD94\uAC00"
Changes from all commits
b5c3873
c06e0cb
88b5c8f
8c46bdb
8a512be
c4c8f7e
bbf18bb
d687750
6d1c7e9
fb0a547
5af052c
832e405
b59a708
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,23 @@ | ||
| package org.juniortown.backend.like.repository; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import org.juniortown.backend.like.entity.Like; | ||
| import org.juniortown.backend.post.dto.response.IdCount; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface LikeRepository extends JpaRepository<Like, Long> { | ||
| Optional<Like> findByUserIdAndPostId(Long userId, Long postId); | ||
| Long countByPostId(Long postId); | ||
|
|
||
| @Query("SELECT l.post.id AS postId, COUNT(l) AS count " | ||
| + "FROM Like l " | ||
| + "WHERE l.post.id IN :postIds " | ||
| + "GROUP BY l.post.id") | ||
| List<IdCount> countByPostIds(@Param("postIds")List<Long>postIds); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package org.juniortown.backend.post.dto.response; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| public class HomePageResponse { | ||
| List<HomePageSection> sections; | ||
| private LocalDateTime createdAt; | ||
| @Builder | ||
| public HomePageResponse(List<HomePageSection> sections, LocalDateTime createdAt) { | ||
| this.sections = sections; | ||
| this.createdAt = createdAt; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package org.juniortown.backend.post.dto.response; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| public class HomePageSection { | ||
| private String category; | ||
| private List<PostForHomePage> items; | ||
| @Builder | ||
| public HomePageSection(String category, List<PostForHomePage> items) { | ||
| this.category = category; | ||
| this.items = items; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package org.juniortown.backend.post.dto.response; | ||
|
|
||
| public interface IdCount { | ||
| Long getPostId(); | ||
| Long getCount(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package org.juniortown.backend.post.dto.response; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| public class PostForHomePage { | ||
| private Long postId; | ||
| private String title; | ||
| private Long userId; | ||
| private String nickname; | ||
| private Long likeCount; | ||
| private Long commentCount; | ||
| private Long readCount; | ||
| private LocalDateTime createdAt; | ||
| @Builder | ||
| public PostForHomePage(Long postId, String title, Long userId, String nickname, Long likeCount, Long commentCount, | ||
| Long readCount, LocalDateTime createdAt) { | ||
| this.postId = postId; | ||
| this.title = title; | ||
| this.userId = userId; | ||
| this.nickname = nickname; | ||
| this.likeCount = likeCount; | ||
| this.commentCount = commentCount; | ||
| this.readCount = readCount; | ||
| this.createdAt = createdAt; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package org.juniortown.backend.post.entity; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public enum Category { | ||
| QUESTION(), | ||
| STUDY(), | ||
| NOTICE(), | ||
| SECOND_HAND_MARKET(), | ||
| MENTOR(); | ||
|
|
||
| public static Category from(String value) { | ||
| if (value == null) { | ||
| throw new IllegalArgumentException("Category는 null일 수 없다."); | ||
| } | ||
| try { | ||
| return Category.valueOf(value.toUpperCase()); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new IllegalArgumentException("유효하지 않은 카테고리입니다: " + value); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,13 @@ | |
|
|
||
| import org.juniortown.backend.entity.BaseTimeEntity; | ||
| import org.juniortown.backend.post.dto.request.PostCreateRequest; | ||
| import org.juniortown.backend.post.dto.request.PostEditRequest; | ||
| import org.juniortown.backend.user.entity.User; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
|
|
@@ -43,20 +46,25 @@ public class Post extends BaseTimeEntity { | |
| @Column(name = "read_count", nullable = false) | ||
| private Long readCount = 0L; | ||
|
|
||
| @Column(nullable = false) | ||
| @Enumerated(EnumType.STRING) | ||
| private Category category; | ||
|
|
||
| @Builder | ||
| public Post(String title, String content, User user) { | ||
| public Post(String title, String content, User user, Category category) { | ||
| this.title = title; | ||
| this.content = content; | ||
| this.user = user; | ||
| this.category = category; | ||
| } | ||
|
Comment on lines
+49
to
59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 카테고리 null 방지 및 인덱스 고려 필요
적용 예(선택 1: Lombok @nonnull 사용): @Getter
public class Post extends BaseTimeEntity {
@@
- @Column(nullable = false)
- @Enumerated(EnumType.STRING)
- private Category category;
+ @Column(nullable = false)
+ @Enumerated(EnumType.STRING)
+ private Category category;
@@
- public Post(String title, String content, User user, Category category) {
+ public Post(String title, String content, User user, @lombok.NonNull Category category) {
this.title = title;
this.content = content;
this.user = user;
this.category = category;
}인덱스 예(파일 범위 밖 변경 예시): // 클래스 선언 위에 추가
// import jakarta.persistence.Table;
// import jakarta.persistence.Index;
@Table(indexes = {
@Index(name = "idx_post_category_deleted_created",
columnList = "category, deleted_at, created_at")
})🤖 Prompt for AI Agents
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 실무에서는 DTO와 엔티티에서 입력을 이중으로 검증하는구나.. 일단 나는 DTO에서만 막을게 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 나중에 더 알아보고 리팩토링 해볼게 |
||
|
|
||
| public void softDelete(Clock clock) { | ||
| this.deletedAt = LocalDateTime.now(clock); | ||
| } | ||
|
|
||
| public void update(PostCreateRequest postCreateRequest, Clock clock) { | ||
| this.title = postCreateRequest.getTitle(); | ||
| this.content = postCreateRequest.getContent(); | ||
| public void update(String title, String content) { | ||
| this.title = title; | ||
| this.content = content; | ||
| } | ||
|
|
||
| public void addReadCount(Long redisReadCount) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.