-
Notifications
You must be signed in to change notification settings - Fork 0
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: 게시물 좋아요, 공유 수 올리는 service, controller 계층 구현 #32
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
fbf969d
Merge branch 'main' of https://github.com/2024-TEAM-05/integrated-fee…
yerim123456 0ca2749
Merge branch 'main' of https://github.com/2024-TEAM-05/integrated-fee…
yerim123456 0b12786
Merge branch 'main' of https://github.com/2024-TEAM-05/integrated-fee…
yerim123456 95bd73f
fix: Custom 에러 잡지 못하고, Internal Server Error로 처리하는 문제 해결 (#22)
yerim123456 ca83d21
feat: 좋아요, 공유 수 올리는 비즈니스 로직 추가 (#22)
yerim123456 38a488f
feat: 좋아요, 공유 수 올릴 때 외부 api 호출을 위한 이벤트 정의 (#22)
yerim123456 1518188
feat: 이벤트 수신하여 외부 api 호출하기 위한 이벤트 리스너 추가 (#22)
yerim123456 1fc62d1
feat: PostRepository 추가 (#22)
yerim123456 0de28c2
feat: 좋아요, 공유 수 올리는 기능 추가 (#22)
yerim123456 49e7333
feat: 좋아요, 공유 수 올리는 api 추가 (#22)
yerim123456 e16e2f3
docs: 좋아요, 공유 수 올리는 api docs 추가 (#22)
yerim123456 c3a1b72
feat: 존재하지 않는 게시물이라는 응답 코드 추가 (#22)
yerim123456 f9ee770
feat: 비동기 처리를 위한 어노테이션 추가 (#22)
yerim123456 826d48f
feat: post mock 객체 생성 매서드 추가 (#22)
yerim123456 d3ecedb
test: postService 좋아요, 공유 수 올리는 테스트 코드 추가 (#22)
yerim123456 71a1a66
remove: 불필요한 gitkeep 파일 제거 (#22)
yerim123456 e11d3d0
fix: 비동기 호출 과정에서의 문자셋 처리 에러 처리를 위해 publisher 추가 (#22)
yerim123456 b45ec66
fix: 불필요한 @Async 어노테이션 제거 (#22)
yerim123456 55982be
test: publisher로 service 테스트 코드 수정 (#22)
yerim123456 3f09722
test: event listener, publisher 테스트 코드 추가 (#22)
yerim123456 fc734ac
refactor: 사용하지 않는 어노테이션 제거 (#22)
yerim123456 81f7236
refactor: HttpStatus가 아닌 StatusCode 사용하여 응답 메세지 변경 (#22)
yerim123456 a5d85a3
feat: Async 비동기 설정 추가 (#22)
yerim123456 aa8da8d
fix: 게시물 종류 SocialType 받도록 event 수정 및 이벤트 이름 과거형으로 수정(#22)
yerim123456 ff94cb2
fix: 게시물 SocialType 에 맞게 외부 api 호출 (#22)
yerim123456 9565027
refactor: 이벤트 관련 이름 과거형으로 수정 및 socialType 인자로 전달 (#22)
yerim123456 f9b4085
fix: socialType 인자로 받는 것, event 이름 변경한 것 맞춰 테스트 코드 수정 (#22)
yerim123456 527c627
Merge branch 'main' of https://github.com/2024-TEAM-05/integrated-fee…
yerim123456 907fb77
fix: 병합 과정 중 삭제한 비즈니스 로직 다시 추가 (#22)
yerim123456 2a7b15c
refactor: StatusCode 위치 변경에 따라 import 다시 진행 (#22)
yerim123456 6d6fc32
Merge branch 'main' into feature/post-like&share-increase
yerim123456 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
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
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
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
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
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
12 changes: 12 additions & 0 deletions
12
src/main/java/team05/integrated_feed_backend/module/post/event/LikeCountIncreasedEvent.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,12 @@ | ||
package team05.integrated_feed_backend.module.post.event; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import team05.integrated_feed_backend.common.enums.SocialMediaType; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class LikeCountIncreasedEvent { | ||
private final Long postId; | ||
private final SocialMediaType type; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/team05/integrated_feed_backend/module/post/event/ShareCountIncreasedEvent.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,12 @@ | ||
package team05.integrated_feed_backend.module.post.event; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import team05.integrated_feed_backend.common.enums.SocialMediaType; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class ShareCountIncreasedEvent { | ||
private final Long postId; | ||
private final SocialMediaType type; | ||
} |
56 changes: 56 additions & 0 deletions
56
...ain/java/team05/integrated_feed_backend/module/post/event/listener/PostEventListener.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,56 @@ | ||
package team05.integrated_feed_backend.module.post.event.listener; | ||
|
||
import org.springframework.context.event.EventListener; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import team05.integrated_feed_backend.common.enums.SocialMediaType; | ||
import team05.integrated_feed_backend.infra.sns.adapter.FacebookAdapter; | ||
import team05.integrated_feed_backend.infra.sns.adapter.InstagramAdapter; | ||
import team05.integrated_feed_backend.infra.sns.adapter.TwitterAdapter; | ||
import team05.integrated_feed_backend.module.post.event.LikeCountIncreasedEvent; | ||
import team05.integrated_feed_backend.module.post.event.ShareCountIncreasedEvent; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class PostEventListener { | ||
|
||
private final FacebookAdapter facebookAdapter; | ||
private final TwitterAdapter twitterAdapter; | ||
private final InstagramAdapter instagramAdapter; | ||
|
||
@Async | ||
@EventListener | ||
public void handleLikeCountIncreasedEvent(LikeCountIncreasedEvent event) { | ||
Long postId = event.getPostId(); | ||
SocialMediaType type = event.getType(); | ||
log.info("Asynchronously handling like count increase event for post ID: {}", postId); | ||
|
||
// 외부 API 호출 | ||
switch (type) { | ||
case FACEBOOK -> facebookAdapter.increaseLikeCount(postId); | ||
case INSTAGRAM -> instagramAdapter.increaseLikeCount(postId); | ||
case TWITTER -> twitterAdapter.increaseLikeCount(postId); | ||
default -> log.error(postId + " 게시물의 " + type + ": facebook, instagram, twitter 중 하나로 설정되어 있지 않습니다."); | ||
} | ||
} | ||
|
||
@Async | ||
@EventListener | ||
public void handleShareCountIncreasedEvent(ShareCountIncreasedEvent event) { | ||
Long postId = event.getPostId(); | ||
SocialMediaType type = event.getType(); | ||
log.info("Asynchronously handling share count increase event for post ID: {}", postId); | ||
|
||
// 외부 API 호출 | ||
switch (type) { | ||
case FACEBOOK -> facebookAdapter.increaseShareCount(postId); | ||
case INSTAGRAM -> instagramAdapter.increaseShareCount(postId); | ||
case TWITTER -> twitterAdapter.increaseShareCount(postId); | ||
default -> log.error(postId + " 게시물의 " + type + ": facebook, instagram, twitter 중 하나로 설정되어 있지 않습니다."); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...n/java/team05/integrated_feed_backend/module/post/event/publisher/PostEventPublisher.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,31 @@ | ||
package team05.integrated_feed_backend.module.post.event.publisher; | ||
|
||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import team05.integrated_feed_backend.common.enums.SocialMediaType; | ||
import team05.integrated_feed_backend.module.post.event.LikeCountIncreasedEvent; | ||
import team05.integrated_feed_backend.module.post.event.ShareCountIncreasedEvent; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class PostEventPublisher { | ||
|
||
private final ApplicationEventPublisher eventPublisher; | ||
|
||
@Async | ||
public void publishLikeCountIncreasedEvent(Long postId, SocialMediaType type) { | ||
eventPublisher.publishEvent(new LikeCountIncreasedEvent(postId, type)); | ||
log.info("LikeCountIncreasedEvent published asynchronously for post ID: {}", postId); | ||
} | ||
|
||
@Async | ||
public void publishShareCountIncreasedEvent(Long postId, SocialMediaType type) { | ||
eventPublisher.publishEvent(new ShareCountIncreasedEvent(postId, type)); | ||
log.info("ShareCountIncreasedEvent published asynchronously for post ID: {}", postId); | ||
} | ||
} |
Empty file.
11 changes: 11 additions & 0 deletions
11
src/main/java/team05/integrated_feed_backend/module/post/repository/PostRepository.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,11 @@ | ||
package team05.integrated_feed_backend.module.post.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import team05.integrated_feed_backend.module.post.entity.Post; | ||
|
||
@Repository | ||
public interface PostRepository extends JpaRepository<Post, Long> { | ||
|
||
} |
Empty file.
47 changes: 47 additions & 0 deletions
47
src/main/java/team05/integrated_feed_backend/module/post/service/PostService.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,47 @@ | ||
package team05.integrated_feed_backend.module.post.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import team05.integrated_feed_backend.common.code.StatusCode; | ||
import team05.integrated_feed_backend.exception.custom.DataNotFoundException; | ||
import team05.integrated_feed_backend.module.post.entity.Post; | ||
import team05.integrated_feed_backend.module.post.event.publisher.PostEventPublisher; | ||
import team05.integrated_feed_backend.module.post.repository.PostRepository; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class PostService { | ||
|
||
private final PostRepository postRepository; | ||
private final PostEventPublisher postEventPublisher; | ||
|
||
@Transactional | ||
public void increaseLikeCount(Long postId) { | ||
// 게시물 존재 여부 확인 | ||
Post post = postRepository.findById(postId) | ||
.orElseThrow(() -> new DataNotFoundException(StatusCode.POST_NOT_EXIST)); | ||
|
||
// 내부 DB count 올리기 | ||
post.increaseLikeCount(); | ||
|
||
// 이벤트 비동기 발행 | ||
postEventPublisher.publishLikeCountIncreasedEvent(postId, post.getType()); | ||
|
||
} | ||
|
||
@Transactional | ||
public void increaseShareCount(Long postId) { | ||
// 게시물 존재 여부 확인 | ||
Post post = postRepository.findById(postId) | ||
.orElseThrow(() -> new DataNotFoundException(StatusCode.POST_NOT_EXIST)); | ||
|
||
// 내부 db count 올리기 | ||
post.increaseShareCount(); | ||
|
||
// 이벤트 비동기 발행 | ||
postEventPublisher.publishShareCountIncreasedEvent(postId, post.getType()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/java/team05/integrated_feed_backend/MockEntityFactory.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,23 @@ | ||
package team05.integrated_feed_backend; | ||
|
||
import java.util.ArrayList; | ||
|
||
import team05.integrated_feed_backend.common.enums.SocialMediaType; | ||
import team05.integrated_feed_backend.module.post.entity.Post; | ||
|
||
public class MockEntityFactory { | ||
|
||
// Post 생성 | ||
public static Post createMockPost() { | ||
return Post.builder() | ||
.title("Sample Post Title") | ||
.content("This is a sample post content.") | ||
.type(SocialMediaType.FACEBOOK) | ||
.viewCount(100L) | ||
.likeCount(10L) | ||
.shareCount(5L) | ||
.postHashtags(new ArrayList<>()) | ||
.build(); | ||
} | ||
|
||
} |
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.
💭 잘은 모르지만 저희
like
로 단수형일까요?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.
넵! 저번에 테크스택 작성할 때 그렇게 작성했어서 우선 저렇게 해두었습니다! 혹시 복수형으로 변경하는 게 더 좋을까요?
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.
엇 아니요! 제가 잘 몰라서 여쭤본거라 편하게 해주셔도 좋을 것 같아요☺️