-
Notifications
You must be signed in to change notification settings - Fork 0
feat : 좋아요 api 구현 #174
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
Merged
feat : 좋아요 api 구현 #174
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
5 changes: 5 additions & 0 deletions
5
clokey-api/src/main/java/org/clokey/domain/history/repository/HistoryRepository.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 |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| package org.clokey.domain.history.repository; | ||
|
|
||
| import java.util.Optional; | ||
| import org.clokey.history.entity.History; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
|
|
||
| public interface HistoryRepository extends JpaRepository<History, Long> { | ||
| @Query("SELECT h FROM History h JOIN FETCH h.member m WHERE h.id = :id") | ||
| Optional<History> findByIdWithMember(Long id); | ||
|
|
||
| boolean existsByMemberId(Long memberId); | ||
| } |
29 changes: 29 additions & 0 deletions
29
clokey-api/src/main/java/org/clokey/domain/like/controller/LikeController.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,29 @@ | ||
| package org.clokey.domain.like.controller; | ||
|
|
||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.clokey.code.GlobalBaseSuccessCode; | ||
| import org.clokey.domain.like.service.LikeService; | ||
| import org.clokey.response.BaseResponse; | ||
| import org.springframework.validation.annotation.Validated; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/likes") | ||
| @RequiredArgsConstructor | ||
| @Tag(name = "09. 좋아요 API", description = "좋아요 관련 API입니다.") | ||
| @Validated | ||
| public class LikeController { | ||
|
|
||
| private final LikeService likeService; | ||
|
|
||
| @PostMapping | ||
| @Operation(operationId = "Like_toggleLike", summary = "좋아요 생성", description = "기록에 좋아요를 추가합니다") | ||
| public BaseResponse<Void> toggleLike(@RequestParam("historyId") Long historyId) { | ||
|
|
||
| likeService.toggleLike(historyId); | ||
|
|
||
| return BaseResponse.onSuccess(GlobalBaseSuccessCode.NO_CONTENT, null); | ||
| } | ||
| } |
30 changes: 29 additions & 1 deletion
30
clokey-api/src/main/java/org/clokey/domain/like/repository/MemberLikeRepository.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 |
|---|---|---|
| @@ -1,6 +1,34 @@ | ||
| package org.clokey.domain.like.repository; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import org.clokey.like.entity.MemberLike; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
|
|
||
| public interface MemberLikeRepository extends JpaRepository<MemberLike, Long> {} | ||
| public interface MemberLikeRepository extends JpaRepository<MemberLike, Long> { | ||
|
|
||
| @Query( | ||
| """ | ||
| SELECT ml | ||
| FROM MemberLike ml | ||
| WHERE ml.member.id = :memberId | ||
| AND (:lastLikeId IS NULL OR ml.id < :lastLikeId) | ||
| ORDER BY ml.id DESC | ||
| """) | ||
| List<MemberLike> findLikedHistoriesByMemberId( | ||
| Long memberId, Long lastLikeId, Pageable pageable); | ||
|
|
||
| @Query( | ||
| """ | ||
| SELECT ml | ||
| FROM MemberLike ml | ||
| WHERE ml.history.id = :historyId | ||
| AND (:lastLikeId IS NULL OR ml.id < :lastLikeId) | ||
| ORDER BY ml.id DESC | ||
| """) | ||
| List<MemberLike> findLikeMembersByHistoryId(Long historyId, Long lastLikeId, Pageable pageable); | ||
|
|
||
| Optional<MemberLike> findByMemberIdAndHistoryId(Long memberId, Long historyId); | ||
| } |
5 changes: 5 additions & 0 deletions
5
clokey-api/src/main/java/org/clokey/domain/like/service/LikeService.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,5 @@ | ||
| package org.clokey.domain.like.service; | ||
|
|
||
| public interface LikeService { | ||
| void toggleLike(Long historyId); | ||
| } |
60 changes: 60 additions & 0 deletions
60
clokey-api/src/main/java/org/clokey/domain/like/service/LikeServiceImpl.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,60 @@ | ||
| package org.clokey.domain.like.service; | ||
|
|
||
| import java.util.Optional; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.clokey.domain.history.exception.HistoryErrorCode; | ||
| import org.clokey.domain.history.repository.HistoryRepository; | ||
| import org.clokey.domain.like.repository.MemberLikeRepository; | ||
| import org.clokey.domain.member.repository.BlockRepository; | ||
| import org.clokey.exception.BaseCustomException; | ||
| import org.clokey.global.util.MemberUtil; | ||
| import org.clokey.history.entity.History; | ||
| import org.clokey.like.entity.MemberLike; | ||
| import org.clokey.member.entity.Member; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional(readOnly = true) | ||
| public class LikeServiceImpl implements LikeService { | ||
|
|
||
| private final MemberUtil memberUtil; | ||
|
|
||
| private final MemberLikeRepository memberLikeRepository; | ||
| private final HistoryRepository historyRepository; | ||
| private final BlockRepository blockRepository; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public void toggleLike(Long historyId) { | ||
| final Member currentMember = memberUtil.getCurrentMember(); | ||
| final History history = | ||
| historyRepository | ||
| .findByIdWithMember(historyId) | ||
| .orElseThrow( | ||
| () -> new BaseCustomException(HistoryErrorCode.HISTORY_NOT_FOUND)); | ||
|
|
||
| final Member historyOwner = history.getMember(); | ||
|
|
||
| if (isBlockedByOrBlocking(currentMember.getId(), historyOwner.getId())) { | ||
| return; | ||
| } | ||
|
|
||
| Optional<MemberLike> existingLike = | ||
| memberLikeRepository.findByMemberIdAndHistoryId(currentMember.getId(), historyId); | ||
|
|
||
| if (existingLike.isPresent()) { | ||
| memberLikeRepository.delete(existingLike.get()); | ||
| } else { | ||
| MemberLike newLike = MemberLike.createMemberLike(currentMember, history); | ||
| memberLikeRepository.save(newLike); | ||
| } | ||
| } | ||
|
|
||
| private boolean isBlockedByOrBlocking(Long fromId, Long toId) { | ||
| return blockRepository.existsByBlockerIdAndBlockedIdOrBlockerIdAndBlockedId( | ||
| fromId, toId, | ||
| toId, fromId); | ||
| } | ||
| } | ||
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
51 changes: 51 additions & 0 deletions
51
clokey-api/src/test/java/org/clokey/domain/like/controller/LikeControllerTest.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,51 @@ | ||
| package org.clokey.domain.like.controller; | ||
|
|
||
| import static org.mockito.BDDMockito.willDoNothing; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.clokey.domain.like.service.LikeService; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
| import org.springframework.test.web.servlet.ResultActions; | ||
|
|
||
| @WebMvcTest(LikeController.class) | ||
| @AutoConfigureMockMvc(addFilters = false) | ||
| public class LikeControllerTest { | ||
| @Autowired private MockMvc mockMvc; | ||
| @Autowired private ObjectMapper objectMapper; | ||
|
|
||
| @MockitoBean private LikeService likeService; | ||
|
|
||
| @Nested | ||
| class 좋아요_요청_시 { | ||
| @Test | ||
| void 유효한_요청이면_성공코드를_반환한다() throws Exception { | ||
| // given | ||
| long historyId = 1L; | ||
|
|
||
| willDoNothing().given(likeService).toggleLike(historyId); | ||
|
|
||
| // when | ||
| ResultActions perform = | ||
| mockMvc.perform( | ||
| post("/likes") | ||
| .param("historyId", String.valueOf(historyId)) | ||
| .contentType(MediaType.APPLICATION_JSON)); | ||
|
|
||
| // then | ||
| perform.andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.isSuccess").value(true)) | ||
| .andExpect(jsonPath("$.code").value("COMMON204")) | ||
| .andExpect(jsonPath("$.message").value("요청 성공 및 반환값 없음")); | ||
| } | ||
| } | ||
| } |
120 changes: 120 additions & 0 deletions
120
clokey-api/src/test/java/org/clokey/domain/like/service/LikeServiceTest.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,120 @@ | ||
| package org.clokey.domain.like.service; | ||
|
|
||
| import static org.assertj.core.api.Assertions.*; | ||
| import static org.mockito.BDDMockito.given; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
| import org.clokey.IntegrationTest; | ||
| import org.clokey.TransactionUtil; | ||
| import org.clokey.domain.history.repository.HistoryImageRepository; | ||
| import org.clokey.domain.history.repository.HistoryRepository; | ||
| import org.clokey.domain.history.repository.SituationRepository; | ||
| import org.clokey.domain.like.repository.MemberLikeRepository; | ||
| import org.clokey.domain.member.repository.BlockRepository; | ||
| import org.clokey.domain.member.repository.FollowRepository; | ||
| import org.clokey.domain.member.repository.MemberRepository; | ||
| import org.clokey.global.util.MemberUtil; | ||
| import org.clokey.history.entity.History; | ||
| import org.clokey.history.entity.Situation; | ||
| import org.clokey.like.entity.MemberLike; | ||
| import org.clokey.member.entity.Block; | ||
| import org.clokey.member.entity.Member; | ||
| import org.clokey.member.entity.OauthInfo; | ||
| import org.clokey.member.enums.OauthProvider; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
|
|
||
| public class LikeServiceTest extends IntegrationTest { | ||
|
|
||
| @Autowired private LikeService likeService; | ||
| @Autowired private MemberLikeRepository memberLikeRepository; | ||
| @Autowired private MemberRepository memberRepository; | ||
| @Autowired private SituationRepository situationRepository; | ||
| @Autowired private HistoryRepository historyRepository; | ||
| @Autowired private HistoryImageRepository historyImageRepository; | ||
| @Autowired private FollowRepository followRepository; | ||
| @Autowired private BlockRepository blockRepository; | ||
|
|
||
| @Autowired private TransactionUtil transactionUtil; | ||
| @MockitoBean private MemberUtil memberUtil; | ||
|
|
||
| @Nested | ||
| class 기록에_좋아요를_할_때 { | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| Member member1 = | ||
| Member.createMember( | ||
| "testEmail1", | ||
| "testClokeyId1", | ||
| "testNickName1", | ||
| OauthInfo.createOauthInfo("testOauthId1", OauthProvider.KAKAO)); | ||
|
|
||
| Member member2 = | ||
| Member.createMember( | ||
| "testEmail2", | ||
| "testClokeyId2", | ||
| "testNickName2", | ||
| OauthInfo.createOauthInfo("testOauthId2", OauthProvider.KAKAO)); | ||
|
|
||
| memberRepository.saveAll(List.of(member1, member2)); | ||
| given(memberUtil.getCurrentMember()).willReturn(member1); | ||
|
|
||
| Situation situation1 = Situation.createSituation("testSituation1"); | ||
| situationRepository.save(situation1); | ||
|
|
||
| History history1 = | ||
| History.createHistory( | ||
| LocalDate.of(2024, 12, 25), | ||
| "content1", | ||
| memberRepository.findById(2L).orElseThrow(), | ||
| situationRepository.findById(1L).orElseThrow()); | ||
| historyRepository.save(history1); | ||
| } | ||
|
|
||
| @Test | ||
| void 좋아요를_누르면_좋아요를_추가한다() { | ||
| // when | ||
| likeService.toggleLike(1L); | ||
|
|
||
| // then | ||
| assertThat(memberLikeRepository.findByMemberIdAndHistoryId(1L, 1L).isPresent()) | ||
| .isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void 기록에_이미_좋아요가_있으면_좋아요를_취소한다() { | ||
| memberLikeRepository.save( | ||
| MemberLike.createMemberLike( | ||
| memberRepository.findById(1L).orElseThrow(), | ||
| historyRepository.findById(1L).orElseThrow())); | ||
|
|
||
| // when | ||
| likeService.toggleLike(1L); | ||
|
|
||
| // then | ||
| assertThat(memberLikeRepository.findByMemberIdAndHistoryId(1L, 1L).isPresent()) | ||
| .isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void 차단된_회원이면_좋아요를_추가하지_않는다() { | ||
| // given | ||
| blockRepository.save( | ||
| Block.createBlock( | ||
| memberRepository.findById(2L).orElseThrow(), | ||
| memberRepository.findById(1L).orElseThrow())); | ||
|
|
||
| // when | ||
| likeService.toggleLike(1L); | ||
|
|
||
| // then | ||
| assertThat(memberLikeRepository.findByMemberIdAndHistoryId(1L, 1L).isPresent()) | ||
| .isFalse(); | ||
| } | ||
| } | ||
| } |
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.
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.
여기 lazy loading 돼있으니까 n+1문제 땜에
history랑 member랑 fetch join해서 한번에 갖고 오는게 좋겠습니다!!