-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from 2024-TEAM-05/feature/post-like&share-incr…
…ease feat: 게시물 좋아요, 공유 수 올리는 service, controller 계층 구현
- Loading branch information
Showing
18 changed files
with
505 additions
and
0 deletions.
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.