-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat/#137] 기록에 좋아요한 유저 목록을 조회 #138
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
Open
juuuuone
wants to merge
2
commits into
develop
Choose a base branch
from
feat/#137-get-liked-users
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
40 changes: 40 additions & 0 deletions
40
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,40 @@ | ||
| package org.clokey.domain.like.controller; | ||
|
|
||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.Parameter; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.clokey.code.GlobalBaseSuccessCode; | ||
| import org.clokey.domain.like.dto.response.LikedMembersResponse; | ||
| import org.clokey.domain.like.service.LikeService; | ||
| import org.clokey.global.annotation.PageSize; | ||
| import org.clokey.response.BaseResponse; | ||
| import org.clokey.response.SliceResponse; | ||
| import org.springframework.validation.annotation.Validated; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/likes") | ||
| @RequiredArgsConstructor | ||
| @Tag(name = "9. 좋아요 API", description = "좋아요 관련 API입니다.") | ||
| @Validated | ||
| public class LikeController { | ||
|
|
||
| private final LikeService likeService; | ||
|
|
||
| @GetMapping("/users") | ||
| @Operation(summary = "좋아요한 유저 조회", description = "내 기록을 좋아요한 유저를 조회합니다") | ||
| public BaseResponse<SliceResponse<LikedMembersResponse.LikedMemberPreview>> getLikedMembers( | ||
| @Parameter(description = "기록 ID") @RequestParam Long historyId, | ||
| @Parameter(description = "이전 페이지의 좋아요 ID (첫 요청 시 생략)") @RequestParam(required = false) | ||
| Long lastLikeId, | ||
| @Parameter(description = "페이지당 조회할 개수") @RequestParam @PageSize Integer size) { | ||
| SliceResponse<LikedMembersResponse.LikedMemberPreview> response = | ||
| likeService.getLikedMembers(historyId, lastLikeId, size); | ||
|
|
||
| return BaseResponse.onSuccess(GlobalBaseSuccessCode.OK, response); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
clokey-api/src/main/java/org/clokey/domain/like/dto/response/LikedMembersResponse.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,18 @@ | ||
| package org.clokey.domain.like.dto.response; | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import java.util.List; | ||
|
|
||
| @Schema(description = "좋아요 유저 조회 결과") | ||
| public record LikedMembersResponse( | ||
| @Schema(description = "유저 미리보기 목록") List<LikedMemberPreview> memberPreviews, | ||
| @Schema(description = "마지막 페이지 여부", example = "false") boolean isLast) { | ||
|
|
||
| @Schema(description = "유저 미리보기 DTO") | ||
| public record LikedMemberPreview( | ||
| @Schema(description = "유저 ID", example = "30") Long id, | ||
| @Schema(description = "클로키 ID", example = "@Clokey_USER1") String codiveId, | ||
| @Schema(description = "프로필 이미지 URL") String imageUrl, | ||
| @Schema(description = "닉네임") String nickname, | ||
| @Schema(description = "팔로우 여부") boolean followStatus) {} | ||
| } |
27 changes: 26 additions & 1 deletion
27
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,31 @@ | ||
| package org.clokey.domain.like.repository; | ||
|
|
||
| import java.util.List; | ||
| 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); | ||
| } |
9 changes: 9 additions & 0 deletions
9
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,9 @@ | ||
| package org.clokey.domain.like.service; | ||
|
|
||
| import org.clokey.domain.like.dto.response.LikedMembersResponse; | ||
| import org.clokey.response.SliceResponse; | ||
|
|
||
| public interface LikeService { | ||
| SliceResponse<LikedMembersResponse.LikedMemberPreview> getLikedMembers( | ||
| Long historyId, Long lastLikedId, Integer size); | ||
| } |
72 changes: 72 additions & 0 deletions
72
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,72 @@ | ||
| package org.clokey.domain.like.service; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.clokey.domain.history.repository.HistoryImageRepository; | ||
| import org.clokey.domain.like.dto.response.LikedMembersResponse; | ||
| import org.clokey.domain.like.repository.MemberLikeRepository; | ||
| import org.clokey.domain.member.repository.FollowRepository; | ||
| import org.clokey.global.util.MemberUtil; | ||
| import org.clokey.like.entity.MemberLike; | ||
| import org.clokey.member.entity.Member; | ||
| import org.clokey.response.SliceResponse; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.Sort; | ||
| 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 HistoryImageRepository historyImageRepository; | ||
| private final FollowRepository followRepository; | ||
|
|
||
| @Override | ||
| public SliceResponse<LikedMembersResponse.LikedMemberPreview> getLikedMembers( | ||
| Long historyId, Long lastLikeId, Integer size) { | ||
|
|
||
| Member currentMember = memberUtil.getCurrentMember(); | ||
| Pageable pageable = PageRequest.of(0, size + 1, Sort.by(Sort.Direction.DESC, "id")); | ||
|
|
||
| List<MemberLike> likes = | ||
| memberLikeRepository.findLikeMembersByHistoryId(historyId, lastLikeId, pageable); | ||
|
|
||
| boolean isLast = likes.size() <= size; | ||
|
|
||
| if (!isLast) { | ||
| likes = likes.subList(0, size); | ||
| } | ||
|
|
||
| if (likes.isEmpty()) { | ||
| return new SliceResponse<>(List.of(), true); | ||
| } | ||
|
|
||
| List<Member> members = likes.stream().map(MemberLike::getMember).toList(); | ||
| List<Long> memberIds = members.stream().map(Member::getId).toList(); | ||
|
|
||
| Set<Long> followedIdSet = | ||
| new HashSet<>( | ||
| followRepository.findFollowedMemberIds(currentMember.getId(), memberIds)); | ||
|
|
||
| List<LikedMembersResponse.LikedMemberPreview> previews = | ||
| members.stream() | ||
| .map( | ||
| member -> | ||
| new LikedMembersResponse.LikedMemberPreview( | ||
| member.getId(), | ||
| member.getClokeyId(), | ||
| member.getProfileImageUrl(), | ||
| member.getNickname(), | ||
| followedIdSet.contains(member.getId()))) | ||
| .toList(); | ||
|
|
||
| return new SliceResponse<>(previews, isLast); | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
clokey-api/src/main/java/org/clokey/domain/member/repository/FollowRepository.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,12 +1,23 @@ | ||
| package org.clokey.domain.member.repository; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import org.clokey.member.entity.Follow; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
|
|
||
| public interface FollowRepository extends JpaRepository<Follow, Long>, FollowRepositoryCustom { | ||
|
|
||
| boolean existsByFollowFrom_IdAndFollowTo_Id(Long fromMemberId, Long toMemberId); | ||
|
|
||
| @Query( | ||
| """ | ||
| SELECT f.followTo.id | ||
| FROM Follow f | ||
| WHERE f.followFrom.id = :fromMemberId | ||
| AND f.followTo.id IN :toMemberIds | ||
| """) | ||
| List<Long> findFollowedMemberIds(Long fromMemberId, List<Long> toMemberIds); | ||
|
|
||
| Optional<Follow> findByFollowFrom_IdAndFollowTo_Id(Long fromMemberId, Long toMemberId); | ||
| } |
122 changes: 122 additions & 0 deletions
122
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,122 @@ | ||
| package org.clokey.domain.like.controller; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyInt; | ||
| import static org.mockito.BDDMockito.given; | ||
| 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 java.util.List; | ||
| import org.clokey.domain.like.dto.response.LikedMembersResponse; | ||
| import org.clokey.domain.like.service.LikeService; | ||
| import org.clokey.response.SliceResponse; | ||
| 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 | ||
| List<LikedMembersResponse.LikedMemberPreview> previews = | ||
| List.of( | ||
| new LikedMembersResponse.LikedMemberPreview( | ||
| 1L, "codive1", "https://img.com/img1.jpg", "nickname1", true), | ||
| new LikedMembersResponse.LikedMemberPreview( | ||
| 2L, "codive2", "https://img.com/img2.jpg", "nickname2", false)); | ||
|
|
||
| SliceResponse<LikedMembersResponse.LikedMemberPreview> sliceResponse = | ||
| new SliceResponse<>(previews, true); | ||
|
|
||
| given(likeService.getLikedMembers(any(), any(), anyInt())).willReturn(sliceResponse); | ||
|
|
||
| ResultActions perform = | ||
| mockMvc.perform( | ||
| get("/likes/users") | ||
| .param("historyId", "1") | ||
| .param("size", "10") | ||
| .contentType(MediaType.APPLICATION_JSON)); | ||
|
|
||
| // then | ||
| perform.andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.code").value("COMMON200")) | ||
| .andExpect(jsonPath("$.message").value("성공입니다.")) | ||
| .andExpect(jsonPath("$.result.content[0].id").value(1L)) | ||
| .andExpect(jsonPath("$.result.content[0].codiveId").value("codive1")) | ||
| .andExpect( | ||
| jsonPath("$.result.content[0].imageUrl") | ||
| .value("https://img.com/img1.jpg")) | ||
| .andExpect(jsonPath("$.result.content[0].nickname").value("nickname1")) | ||
| .andExpect(jsonPath("$.result.content[0].followStatus").value(true)) | ||
| .andExpect(jsonPath("$.result.content[1].id").value(2L)) | ||
| .andExpect(jsonPath("$.result.content[1].codiveId").value("codive2")) | ||
| .andExpect( | ||
| jsonPath("$.result.content[1].imageUrl") | ||
| .value("https://img.com/img2.jpg")) | ||
| .andExpect(jsonPath("$.result.content[1].nickname").value("nickname2")) | ||
| .andExpect(jsonPath("$.result.content[1].followStatus").value(false)) | ||
| .andExpect(jsonPath("$.result.isLast").value(true)); | ||
| } | ||
|
|
||
| @Test | ||
| void 마지막_페이지가_아닌_경우_isLast를_false로_응답한다() throws Exception { | ||
| // given | ||
| List<LikedMembersResponse.LikedMemberPreview> previews = | ||
| List.of( | ||
| new LikedMembersResponse.LikedMemberPreview( | ||
| 1L, "codive1", "https://img.com/img1.jpg", "nickname1", true), | ||
| new LikedMembersResponse.LikedMemberPreview( | ||
| 2L, "codive2", "https://img.com/img2.jpg", "nickname2", false)); | ||
|
|
||
| SliceResponse<LikedMembersResponse.LikedMemberPreview> sliceResponse = | ||
| new SliceResponse<>(previews, false); | ||
|
|
||
| given(likeService.getLikedMembers(any(), any(), anyInt())).willReturn(sliceResponse); | ||
|
|
||
| // when | ||
| ResultActions perform = | ||
| mockMvc.perform( | ||
| get("/likes/users") | ||
| .param("historyId", "1") | ||
| .param("size", "10") | ||
| .contentType(MediaType.APPLICATION_JSON)); | ||
|
|
||
| // then | ||
| perform.andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.code").value("COMMON200")) | ||
| .andExpect(jsonPath("$.message").value("성공입니다.")) | ||
| .andExpect(jsonPath("$.result.content[0].id").value(1L)) | ||
| .andExpect(jsonPath("$.result.content[0].codiveId").value("codive1")) | ||
| .andExpect( | ||
| jsonPath("$.result.content[0].imageUrl") | ||
| .value("https://img.com/img1.jpg")) | ||
| .andExpect(jsonPath("$.result.content[0].nickname").value("nickname1")) | ||
| .andExpect(jsonPath("$.result.content[0].followStatus").value(true)) | ||
| .andExpect(jsonPath("$.result.content[1].id").value(2L)) | ||
| .andExpect(jsonPath("$.result.content[1].codiveId").value("codive2")) | ||
| .andExpect( | ||
| jsonPath("$.result.content[1].imageUrl") | ||
| .value("https://img.com/img2.jpg")) | ||
| .andExpect(jsonPath("$.result.content[1].nickname").value("nickname2")) | ||
| .andExpect(jsonPath("$.result.content[1].followStatus").value(false)) | ||
| .andExpect(jsonPath("$.result.isLast").value(false)); | ||
| } | ||
| } | ||
| } |
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.
이 부분 N+1문제가 터질 것 같아요.
페이징은 기존에 사용하던 Projection 적용 부탁드려요(customRepository)! 성능 차이가 큽니다!