-
Notifications
You must be signed in to change notification settings - Fork 8
feat: 소식지 좋아요 기능 구현 #377
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
Gyuhyeok99
merged 19 commits into
solid-connection:develop
from
Gyuhyeok99:feat/376-liked-news-api
Jul 10, 2025
Merged
feat: 소식지 좋아요 기능 구현 #377
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1f1b2e0
feat: 소식지 좋아요 응답 dto 생성
Gyuhyeok99 e83154a
feat: 소식지 좋아요 Repository 생성
Gyuhyeok99 a1d0caf
feat: 소식지 좋아요 Service 생성
Gyuhyeok99 0b11221
feat: 소식지 좋아요 Controller 생성
Gyuhyeok99 f6a3844
test: 소식지 좋아요 테스트 코드 작성
Gyuhyeok99 f7bf68f
feat: 소식지 좋아요 취소 Service 생성
Gyuhyeok99 1ebb78f
feat: 소식지 좋아요 취소 Controller 생성
Gyuhyeok99 feec4ed
test: 소식지 좋아요 취소 테스트 코드 작성
Gyuhyeok99 9a06d62
feat: 소식지 좋아요 상태 확인 Service 생성
Gyuhyeok99 82fd744
feat: 소식지 좋아요 상태 확인 Controller 생성
Gyuhyeok99 5e57fc6
test: 소식지 좋아요 상태 확인 테스트 코드 작성
Gyuhyeok99 09e0fe7
refactor: newsId Long -> long으로 변경
Gyuhyeok99 2458c25
refactor: 좋아요 성공 및 취소 시 200 상태코드만 주도록 변경
Gyuhyeok99 c9fd16f
refactor: 좋아요 상태 확인 응답 dto에 id 제거
Gyuhyeok99 6e1aa83
style: 소식지 좋아요 상태 관련 테스트명 변경
Gyuhyeok99 c988806
refactor: return 타입 ResponseEntity<Void>로 변경
Gyuhyeok99 fdbc668
style: 테스트명에 공백 추가
Gyuhyeok99 a635ca3
refactor: 좋아요 상태 확인 함수명 isNewsLiked로 변경
Gyuhyeok99 e70bdd8
fix: 충돌 해결
Gyuhyeok99 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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/example/solidconnection/news/dto/LikedNewsResponse.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,10 @@ | ||
| package com.example.solidconnection.news.dto; | ||
|
|
||
| public record LikedNewsResponse( | ||
| boolean isLike | ||
| ) { | ||
|
|
||
| public static LikedNewsResponse of(boolean isLike) { | ||
| return new LikedNewsResponse(isLike); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/example/solidconnection/news/repository/LikedNewsRepository.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,13 @@ | ||
| package com.example.solidconnection.news.repository; | ||
|
|
||
| import com.example.solidconnection.news.domain.LikedNews; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface LikedNewsRepository extends JpaRepository<LikedNews, Long> { | ||
|
|
||
| boolean existsByNewsIdAndSiteUserId(long newsId, long siteUserId); | ||
|
|
||
| Optional<LikedNews> findByNewsIdAndSiteUserId(long newsId, long siteUserId); | ||
| } |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/example/solidconnection/news/service/NewsLikeService.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.news.service; | ||
|
|
||
| import com.example.solidconnection.common.exception.CustomException; | ||
| import com.example.solidconnection.news.domain.LikedNews; | ||
| import com.example.solidconnection.news.dto.LikedNewsResponse; | ||
| import com.example.solidconnection.news.repository.LikedNewsRepository; | ||
| import com.example.solidconnection.news.repository.NewsRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import static com.example.solidconnection.common.exception.ErrorCode.ALREADY_LIKED_NEWS; | ||
| import static com.example.solidconnection.common.exception.ErrorCode.NEWS_NOT_FOUND; | ||
| import static com.example.solidconnection.common.exception.ErrorCode.NOT_LIKED_NEWS; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Service | ||
| public class NewsLikeService { | ||
|
|
||
| private final NewsRepository newsRepository; | ||
| private final LikedNewsRepository likedNewsRepository; | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public LikedNewsResponse isNewsLiked(long siteUserId, long newsId) { | ||
| if (!newsRepository.existsById(newsId)) { | ||
| throw new CustomException(NEWS_NOT_FOUND); | ||
| } | ||
| boolean isLike = likedNewsRepository.existsByNewsIdAndSiteUserId(newsId, siteUserId); | ||
| return LikedNewsResponse.of(isLike); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void addNewsLike(long siteUserId, long newsId) { | ||
| if (!newsRepository.existsById(newsId)) { | ||
| throw new CustomException(NEWS_NOT_FOUND); | ||
| } | ||
| if (likedNewsRepository.existsByNewsIdAndSiteUserId(newsId, siteUserId)) { | ||
| throw new CustomException(ALREADY_LIKED_NEWS); | ||
| } | ||
| LikedNews likedNews = new LikedNews(newsId, siteUserId); | ||
| likedNewsRepository.save(likedNews); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void cancelNewsLike(long siteUserId, long newsId) { | ||
| if (!newsRepository.existsById(newsId)) { | ||
| throw new CustomException(NEWS_NOT_FOUND); | ||
| } | ||
| LikedNews likedNews = likedNewsRepository.findByNewsIdAndSiteUserId(newsId, siteUserId) | ||
| .orElseThrow(() -> new CustomException(NOT_LIKED_NEWS)); | ||
| likedNewsRepository.delete(likedNews); | ||
| } | ||
| } | ||
119 changes: 119 additions & 0 deletions
119
src/test/java/com/example/solidconnection/news/service/NewsLikeServiceTest.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,119 @@ | ||
| package com.example.solidconnection.news.service; | ||
|
|
||
| import com.example.solidconnection.common.exception.CustomException; | ||
| import com.example.solidconnection.news.domain.News; | ||
| import com.example.solidconnection.news.dto.LikedNewsResponse; | ||
| import com.example.solidconnection.news.fixture.NewsFixture; | ||
| import com.example.solidconnection.news.repository.LikedNewsRepository; | ||
| import com.example.solidconnection.siteuser.domain.SiteUser; | ||
| import com.example.solidconnection.siteuser.fixture.SiteUserFixture; | ||
| import com.example.solidconnection.support.TestContainerSpringBootTest; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
|
|
||
| import static com.example.solidconnection.common.exception.ErrorCode.ALREADY_LIKED_NEWS; | ||
| import static com.example.solidconnection.common.exception.ErrorCode.NOT_LIKED_NEWS; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode; | ||
|
|
||
| @TestContainerSpringBootTest | ||
| @DisplayName("소식지 좋아요 서비스 테스트") | ||
| class NewsLikeServiceTest { | ||
|
|
||
| @Autowired | ||
| private NewsLikeService newsLikeService; | ||
|
|
||
| @Autowired | ||
| private LikedNewsRepository likedNewsRepository; | ||
|
|
||
| @Autowired | ||
| private SiteUserFixture siteUserFixture; | ||
|
|
||
| @Autowired | ||
| private NewsFixture newsFixture; | ||
|
|
||
| private SiteUser user; | ||
| private News news; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| user = siteUserFixture.사용자(); | ||
| news = newsFixture.소식지(siteUserFixture.멘토(1, "mentor").getId()); | ||
| } | ||
|
|
||
| @Nested | ||
| class 소식지_좋아요_상태를_조회한다 { | ||
|
|
||
| @Test | ||
| void 좋아요한_소식지의_좋아요_상태를_조회한다() { | ||
| // given | ||
| newsLikeService.addNewsLike(user.getId(), news.getId()); | ||
|
|
||
| // when | ||
| LikedNewsResponse response = newsLikeService.isNewsLiked(user.getId(), news.getId()); | ||
|
|
||
| // then | ||
| assertThat(response.isLike()).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void 좋아요하지_않은_소식지의_좋아요_상태를_조회한다() { | ||
| // when | ||
| LikedNewsResponse response = newsLikeService.isNewsLiked(user.getId(), news.getId()); | ||
|
|
||
| // then | ||
| assertThat(response.isLike()).isFalse(); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| class 소식지_좋아요를_등록한다 { | ||
|
|
||
| @Test | ||
| void 성공적으로_좋아요를_등록한다() { | ||
| // when | ||
| newsLikeService.addNewsLike(user.getId(), news.getId()); | ||
|
|
||
| // then | ||
| assertThat(likedNewsRepository.existsByNewsIdAndSiteUserId(news.getId(), user.getId())).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void 이미_좋아요했으면_예외_응답을_반환한다() { | ||
| // given | ||
| newsLikeService.addNewsLike(user.getId(), news.getId()); | ||
|
|
||
| // when & then | ||
| assertThatCode(() -> newsLikeService.addNewsLike(user.getId(), news.getId())) | ||
| .isInstanceOf(CustomException.class) | ||
| .hasMessage(ALREADY_LIKED_NEWS.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| class 소식지_좋아요를_취소한다 { | ||
|
|
||
| @Test | ||
| void 성공적으로_좋아요를_취소한다() { | ||
| // given | ||
| newsLikeService.addNewsLike(user.getId(), news.getId()); | ||
|
|
||
| // when | ||
| newsLikeService.cancelNewsLike(user.getId(), news.getId()); | ||
|
|
||
| // then | ||
| assertThat(likedNewsRepository.existsByNewsIdAndSiteUserId(news.getId(), user.getId())).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void 좋아요하지_않았으면_예외_응답을_반환한다() { | ||
| // when & then | ||
| assertThatCode(() -> newsLikeService.cancelNewsLike(user.getId(), news.getId())) | ||
| .isInstanceOf(CustomException.class) | ||
| .hasMessage(NOT_LIKED_NEWS.getMessage()); | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.