-
Notifications
You must be signed in to change notification settings - Fork 0
[feat/#87] 좋아요한 기록 조회 api 구현 #114
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
04604ef
feat : 구현중
juuuuone fa49c9e
fix : 충돌 해결
juuuuone 01d5456
feat : 좋아요한 기록 조회 구현
juuuuone 0bb0fb1
refactor : 페이징 방식 리팩토링
juuuuone d00dd5c
fix : test 수정
juuuuone 5449cac
Merge branch 'develop' of https://github.com/Clokey-dev/clokey-server…
juuuuone 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
35 changes: 35 additions & 0 deletions
35
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,35 @@ | ||
| 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.dto.response.LikedHistoriesResponse; | ||
| import org.clokey.domain.like.service.LikeService; | ||
| import org.clokey.response.BaseResponse; | ||
| import org.clokey.response.SliceResponse; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.web.PageableDefault; | ||
| 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.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/likes") | ||
| @RequiredArgsConstructor | ||
| @Tag(name = "9. 좋아요 API", description = "좋아요 관련 API입니다.") | ||
| @Validated | ||
| public class LikeController { | ||
|
|
||
| private final LikeService likeService; | ||
|
|
||
| @GetMapping("/histories") | ||
| @Operation(summary = "좋아요한 기록 조회", description = "사용자가 좋아요한 기록을 조회합니다.") | ||
| public BaseResponse<SliceResponse<LikedHistoriesResponse.LikedHistoryPreview>> | ||
| getLikedHistories(@PageableDefault(size = 10) Pageable pageable) { | ||
| SliceResponse<LikedHistoriesResponse.LikedHistoryPreview> response = | ||
| likeService.getLikedHistories(pageable); | ||
| return BaseResponse.onSuccess(GlobalBaseSuccessCode.OK, response); | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
clokey-api/src/main/java/org/clokey/domain/like/dto/response/LikedHistoriesResponse.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,19 @@ | ||
| package org.clokey.domain.like.dto.response; | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import java.util.List; | ||
|
|
||
| @Schema(description = "좋아요 히스토리 조회 결과 (Slice 기반)") | ||
| public record LikedHistoriesResponse( | ||
| @Schema(description = "히스토리 미리보기 목록") List<LikedHistoryPreview> historyPreviews, | ||
| @Schema(description = "마지막 페이지 여부", example = "false") boolean isLast) { | ||
|
|
||
| @Schema(description = "히스토리 미리보기 DTO") | ||
| public record LikedHistoryPreview( | ||
| @Schema(description = "히스토리 ID", example = "30") Long id, | ||
| @Schema( | ||
| description = "히스토리 대표 이미지 URL", | ||
| example = | ||
| "https://clokeybucket.s3.ap-northeast-2.amazonaws.com/example.jpg") | ||
| String imageUrl) {} | ||
| } |
9 changes: 8 additions & 1 deletion
9
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,13 @@ | ||
| package org.clokey.domain.like.repository; | ||
|
|
||
| import org.clokey.like.entity.MemberLike; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.Slice; | ||
| 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") | ||
| Slice<MemberLike> findLikedHistoriesByMemberId(Long memberId, 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.LikedHistoriesResponse; | ||
| import org.clokey.response.SliceResponse; | ||
| import org.springframework.data.domain.Pageable; | ||
|
|
||
| public interface LikeService { | ||
| SliceResponse<LikedHistoriesResponse.LikedHistoryPreview> getLikedHistories(Pageable pageable); | ||
| } |
69 changes: 69 additions & 0 deletions
69
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,69 @@ | ||
| package org.clokey.domain.like.service; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.clokey.domain.history.repository.HistoryImageRepository; | ||
| import org.clokey.domain.like.dto.response.LikedHistoriesResponse; | ||
| import org.clokey.domain.like.repository.MemberLikeRepository; | ||
| 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.Pageable; | ||
| import org.springframework.data.domain.Slice; | ||
| 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; | ||
|
|
||
| @Override | ||
| @Transactional(readOnly = true) | ||
| public SliceResponse<LikedHistoriesResponse.LikedHistoryPreview> getLikedHistories( | ||
| Pageable pageable) { | ||
|
|
||
| final Member currentMember = memberUtil.getCurrentMember(); | ||
|
|
||
| Slice<MemberLike> likes = | ||
| memberLikeRepository.findLikedHistoriesByMemberId(currentMember.getId(), pageable); | ||
|
|
||
| if (likes == null || likes.getContent().isEmpty()) { | ||
| return new SliceResponse<>(List.of(), true); | ||
| } | ||
|
|
||
| List<Long> historyIds = | ||
| likes.getContent().stream().map(like -> like.getHistory().getId()).toList(); | ||
|
|
||
| Map<Long, String> imageMap = findFirstImagesByHistoryIds(historyIds); | ||
|
|
||
| List<LikedHistoriesResponse.LikedHistoryPreview> previews = | ||
| likes.getContent().stream() | ||
| .map( | ||
| like -> | ||
| new LikedHistoriesResponse.LikedHistoryPreview( | ||
| like.getHistory().getId(), | ||
| imageMap.get(like.getHistory().getId()))) | ||
| .toList(); | ||
|
|
||
| return new SliceResponse<>(previews, likes.isLast()); | ||
| } | ||
|
|
||
| private Map<Long, String> findFirstImagesByHistoryIds(List<Long> historyIds) { | ||
| if (historyIds.isEmpty()) return Map.of(); | ||
|
|
||
| List<Object[]> rows = historyImageRepository.getFirstImageUrlsWithHistoryId(historyIds); | ||
|
|
||
| return rows.stream() | ||
| .collect( | ||
| Collectors.toMap( | ||
| row -> ((Number) row[0]).longValue(), row -> (String) row[1])); | ||
| } | ||
| } |
107 changes: 107 additions & 0 deletions
107
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,107 @@ | ||
| package org.clokey.domain.like.controller; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
| 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.LikedHistoriesResponse; | ||
| 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.data.domain.Pageable; | ||
| 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<LikedHistoriesResponse.LikedHistoryPreview> previews = | ||
| List.of( | ||
| new LikedHistoriesResponse.LikedHistoryPreview( | ||
| 1L, "https://img.com/img1.jpg"), | ||
| new LikedHistoriesResponse.LikedHistoryPreview( | ||
| 2L, "https://img.com/img2.jpg")); | ||
|
|
||
| SliceResponse<LikedHistoriesResponse.LikedHistoryPreview> sliceResponse = | ||
| new SliceResponse<>(previews, true); | ||
|
|
||
| given(likeService.getLikedHistories(any(Pageable.class))).willReturn(sliceResponse); | ||
|
|
||
| ResultActions perform = | ||
| mockMvc.perform( | ||
| get("/likes/histories") | ||
| .param("page", "0") | ||
| .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].imageUrl") | ||
| .value("https://img.com/img1.jpg")) | ||
| .andExpect(jsonPath("$.result.content[1].id").value(2L)) | ||
| .andExpect( | ||
| jsonPath("$.result.content[1].imageUrl") | ||
| .value("https://img.com/img2.jpg")) | ||
| .andExpect(jsonPath("$.result.isLast").value(true)); | ||
| } | ||
|
|
||
| @Test | ||
| void 마지막_페이지가_아닌_경우_isLast를_false로_응답한다() throws Exception { | ||
| // given | ||
| List<LikedHistoriesResponse.LikedHistoryPreview> previews = | ||
| List.of( | ||
| new LikedHistoriesResponse.LikedHistoryPreview( | ||
| 1L, "https://img.com/img1.jpg"), | ||
| new LikedHistoriesResponse.LikedHistoryPreview( | ||
| 2L, "https://img.com/img2.jpg")); | ||
|
|
||
| SliceResponse<LikedHistoriesResponse.LikedHistoryPreview> sliceResponse = | ||
| new SliceResponse<>(previews, false); | ||
|
|
||
| given(likeService.getLikedHistories(any(Pageable.class))).willReturn(sliceResponse); | ||
|
|
||
| // when | ||
| ResultActions perform = | ||
| mockMvc.perform( | ||
| get("/likes/histories").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].imageUrl") | ||
| .value("https://img.com/img1.jpg")) | ||
| .andExpect(jsonPath("$.result.content[1].id").value(2L)) | ||
| .andExpect( | ||
| jsonPath("$.result.content[1].imageUrl") | ||
| .value("https://img.com/img2.jpg")) | ||
| .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.
저희 프로젝트는 통일된 커서 기반 페이징을 사용하고 있어서 다른 Paging이 쓰이고 있는 부분들을 참고하시면 좋을 것 같아요.
ex) 기록 목록 조회, 댓글 목록 조회 , 알림 목록 조회 , 옷 목록 조회 등등...
현재의 pageable 입력값은 offset 기반 페이징에 더 적합한 것 같아요.
그래서 기존 처럼 DTO 프로젝션 + 커서 페이징 참고해서 가져다 쓰시면 될 것 같습니다!