-
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 #105 from Seasoning-Today/refactor/reorganize-arti…
…cle-like-services 기록장 좋아요 서비스 로직 분리 및 기록장 좋아요 취소 통합 테스트 작성
- Loading branch information
Showing
7 changed files
with
290 additions
and
71 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
67 changes: 0 additions & 67 deletions
67
src/main/java/today/seasoning/seasoning/article/service/ArticleLikeService.java
This file was deleted.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
src/main/java/today/seasoning/seasoning/article/service/CancelArticleLikeService.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,24 @@ | ||
package today.seasoning.seasoning.article.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import today.seasoning.seasoning.article.domain.ArticleLikeRepository; | ||
import today.seasoning.seasoning.common.exception.CustomException; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CancelArticleLikeService { | ||
|
||
private final ArticleLikeRepository articleLikeRepository; | ||
private final ValidateArticleLikePolicy validateArticleLikePolicy; | ||
|
||
@Transactional | ||
public void doService(Long userId, Long articleId) { | ||
if (!validateArticleLikePolicy.validate(userId, articleId)) { | ||
throw new CustomException(HttpStatus.FORBIDDEN, "권한 없음"); | ||
} | ||
articleLikeRepository.findByArticleAndUser(articleId, userId).ifPresent(articleLikeRepository::delete); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/today/seasoning/seasoning/article/service/RegisterArticleLikeService.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,52 @@ | ||
package today.seasoning.seasoning.article.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import today.seasoning.seasoning.article.domain.Article; | ||
import today.seasoning.seasoning.article.domain.ArticleLike; | ||
import today.seasoning.seasoning.article.domain.ArticleLikeRepository; | ||
import today.seasoning.seasoning.article.domain.ArticleRepository; | ||
import today.seasoning.seasoning.article.event.ArticleLikedEvent; | ||
import today.seasoning.seasoning.common.exception.CustomException; | ||
import today.seasoning.seasoning.user.domain.User; | ||
import today.seasoning.seasoning.user.domain.UserRepository; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class RegisterArticleLikeService { | ||
|
||
private final UserRepository userRepository; | ||
private final ArticleRepository articleRepository; | ||
private final ArticleLikeRepository articleLikeRepository; | ||
private final ValidateArticleLikePolicy validateArticleLikePolicy; | ||
private final ApplicationEventPublisher applicationEventPublisher; | ||
|
||
@Transactional | ||
public void doService(Long userId, Long articleId) { | ||
Article article = articleRepository.findByIdOrElseThrow(articleId); | ||
User user = userRepository.findByIdOrElseThrow(userId); | ||
User author = article.getUser(); | ||
|
||
// 사용자 권한 검증 | ||
if (!validateArticleLikePolicy.validate(userId, articleId)) { | ||
throw new CustomException(HttpStatus.FORBIDDEN, "권한 없음"); | ||
} | ||
|
||
// 중복 요청의 경우 무시 | ||
if (articleLikeRepository.findByArticleAndUser(articleId, userId).isPresent()) { | ||
return; | ||
} | ||
|
||
articleLikeRepository.save(new ArticleLike(article, user)); | ||
|
||
// 타인의 글에 좋아요를 누른 경우, 상대방에게 관련 알림 전송 | ||
if (user != author) { | ||
ArticleLikedEvent articleLikedEvent = new ArticleLikedEvent(user.getId(), author.getId(), articleId); | ||
applicationEventPublisher.publishEvent(articleLikedEvent); | ||
} | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/today/seasoning/seasoning/article/service/ValidateArticleLikePolicy.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,6 @@ | ||
package today.seasoning.seasoning.article.service; | ||
|
||
public interface ValidateArticleLikePolicy { | ||
|
||
boolean validate(Long userId, Long articleId); | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/today/seasoning/seasoning/article/service/ValidateArticleLikePolicyImpl.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,32 @@ | ||
package today.seasoning.seasoning.article.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import today.seasoning.seasoning.article.domain.Article; | ||
import today.seasoning.seasoning.article.domain.ArticleRepository; | ||
import today.seasoning.seasoning.friendship.domain.FriendshipRepository; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ValidateArticleLikePolicyImpl implements ValidateArticleLikePolicy { | ||
|
||
private final ArticleRepository articleRepository; | ||
private final FriendshipRepository friendshipRepository; | ||
|
||
@Override | ||
public boolean validate(Long userId, Long articleId) { | ||
Article article = articleRepository.findByIdOrElseThrow(articleId); | ||
Long authorId = article.getUser().getId(); | ||
|
||
// 자신의 글 | ||
if (authorId.equals(userId)) { | ||
return true; | ||
} | ||
// 공개된 친구의 글 | ||
if (article.isPublished() && friendshipRepository.existsByUserIdAndFriendId(userId, authorId)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.