-
Notifications
You must be signed in to change notification settings - Fork 8
게시글 좋아요 API 구현 #60
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
Merged
leesewon00
merged 15 commits into
solid-connection:main
from
leesewon00:feat/52-create-post-like-api
Aug 19, 2024
Merged
게시글 좋아요 API 구현 #60
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3191258
feat: 게시글 좋아요 엔티티 정의
leesewon00 41feaa3
feat: 게시글 좋아요 레포지토리 정의
leesewon00 716c1f4
feat: 좋아요 등록,삭제 레포지토리 메소드 작성
leesewon00 1a653a3
feat: 게시글 좋아요 서비스 로직 추가
leesewon00 d102f82
feat: 게시글 좋아요 컨트롤러 로직 추가
leesewon00 3f28b1a
feat: 게시글 좋아요 관련 DTO 정의
leesewon00 c2ac6b7
feat: 게시글 좋아요 관련 예외 정의
leesewon00 ad7bd6f
feat: 게시글 좋아요 레포지토리 테스트 로직 추가
leesewon00 9ba208b
feat: 게시글 좋아요 서비스 테스트 로직 추가
leesewon00 d9c60e1
feat: 게시글 좋아요 레포지토리 테스트 로직 추가
leesewon00 899a5b2
feat: 게시글 좋아요 동시성 테스트 로직 추가
leesewon00 c76be20
refactor: 특정 게시글 조회시 좋아요 여부도 반환
leesewon00 3dcd772
refactor: 게시글 좋아요 삭제 경로 수정
leesewon00 044cee0
refactor: CommentCreateRequest record 타입으로 변경 및 Long 타입 활용하여 null 값 허용
leesewon00 19f9073
refactor: 게시글 작성시 category 전체 허용하지 않도록 수정
leesewon00 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
24 changes: 0 additions & 24 deletions
24
src/main/java/com/example/solidconnection/entity/mapping/PostLike.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
53 changes: 53 additions & 0 deletions
53
src/main/java/com/example/solidconnection/post/domain/PostLike.java
This file contains hidden or 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,53 @@ | ||
| package com.example.solidconnection.post.domain; | ||
|
|
||
| import com.example.solidconnection.siteuser.domain.SiteUser; | ||
| import jakarta.persistence.*; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @NoArgsConstructor | ||
| @EqualsAndHashCode(of = "id") | ||
| public class PostLike { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "post_id") | ||
| private Post post; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "site_user_id") | ||
| private SiteUser siteUser; | ||
|
|
||
| public void setPostAndSiteUser(Post post, SiteUser siteUser) { | ||
|
|
||
| if (this.post != null) { | ||
| this.post.getPostLikeList().remove(this); | ||
| } | ||
| this.post = post; | ||
| post.getPostLikeList().add(this); | ||
|
|
||
| if (this.siteUser != null) { | ||
| this.siteUser.getPostLikeList().remove(this); | ||
| } | ||
| this.siteUser = siteUser; | ||
| siteUser.getPostLikeList().add(this); | ||
| } | ||
|
|
||
| public void resetPostAndSiteUser() { | ||
|
|
||
| if (this.post != null) { | ||
| this.post.getPostLikeList().remove(this); | ||
| } | ||
| this.post = null; | ||
|
|
||
| if (this.siteUser != null) { | ||
| this.siteUser.getPostLikeList().remove(this); | ||
| } | ||
| this.siteUser = null; | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/example/solidconnection/post/dto/PostDislikeResponse.java
This file contains hidden or 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,15 @@ | ||
| package com.example.solidconnection.post.dto; | ||
|
|
||
| import com.example.solidconnection.post.domain.Post; | ||
|
|
||
| public record PostDislikeResponse( | ||
| Long likeCount, | ||
| Boolean isLiked | ||
| ) { | ||
| public static PostDislikeResponse from(Post post) { | ||
| return new PostDislikeResponse( | ||
| post.getLikeCount(), | ||
| false | ||
| ); | ||
| } | ||
| } |
This file contains hidden or 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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/example/solidconnection/post/dto/PostLikeResponse.java
This file contains hidden or 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,17 @@ | ||
| package com.example.solidconnection.post.dto; | ||
|
|
||
| import com.example.solidconnection.post.domain.Post; | ||
|
|
||
| public record PostLikeResponse( | ||
| Long likeCount, | ||
| Boolean isLiked | ||
|
|
||
|
|
||
| ) { | ||
| public static PostLikeResponse from(Post post) { | ||
| return new PostLikeResponse( | ||
| post.getLikeCount(), | ||
| true | ||
| ); | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
src/main/java/com/example/solidconnection/post/repository/PostLikeRepository.java
This file contains hidden or 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,23 @@ | ||
| package com.example.solidconnection.post.repository; | ||
|
|
||
| import com.example.solidconnection.custom.exception.CustomException; | ||
| import com.example.solidconnection.post.domain.PostLike; | ||
| import com.example.solidconnection.post.domain.Post; | ||
| import com.example.solidconnection.siteuser.domain.SiteUser; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static com.example.solidconnection.custom.exception.ErrorCode.INVALID_POST_LIKE; | ||
|
|
||
| @Repository | ||
| public interface PostLikeRepository extends JpaRepository<PostLike, Long> { | ||
|
|
||
| Optional<PostLike> findPostLikeByPostAndSiteUser(Post post, SiteUser siteUser); | ||
|
|
||
| default PostLike getByPostAndSiteUser(Post post, SiteUser siteUser) { | ||
| return findPostLikeByPostAndSiteUser(post, siteUser) | ||
| .orElseThrow(() -> new CustomException(INVALID_POST_LIKE)); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
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.
[반영 안 해도 되는 개인적 생각]
래퍼 클래스는 "null 이 올 가능성이 있을 때" 만 사용하는 것이 좋다 생각해요. 메모리상으로도 효율적이고, 다른 개발자들에게 null 의 가능성을 알려준다는 것도 중요하다 생각해서요!
Uh oh!
There was an error while loading. Please reload this page.
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.
오,, 해당부분은 제가 크게 놓치고 있었던 내용이네요,,
래퍼클래스의 장점 때문에 무분별하게 사용하고 있었던 것 같은데 null이 올 가능성이 없을때 등 불필요한 상황에서의 래퍼클래스 사용은 지양하는게 좋다는 생각이 드네요. 감사합니다!