Skip to content
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

Refactor: 메시지함 페이징 정보 응답 포맷 개선 #101

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import slvtwn.khu.toyouserver.common.response.ResponseType;
Expand All @@ -12,6 +13,8 @@
import slvtwn.khu.toyouserver.domain.StickerSide;
import slvtwn.khu.toyouserver.domain.User;
import slvtwn.khu.toyouserver.dto.CoverRequest;
import slvtwn.khu.toyouserver.dto.CursorPageInfo;
import slvtwn.khu.toyouserver.dto.RollingPaperPagedResponse;
import slvtwn.khu.toyouserver.dto.RollingPaperRequest;
import slvtwn.khu.toyouserver.dto.RollingPaperResponse;
import slvtwn.khu.toyouserver.exception.ToyouException;
Expand Down Expand Up @@ -63,13 +66,19 @@ public RollingPaperResponse findById(Long userId, Long rollingPaperId) {
return RollingPaperResponse.from(rollingPaper);
}

public List<RollingPaperResponse> findReceivedRollingPapers(Long userId, Long groupId,
Long targetId, Integer limit) {
public RollingPaperPagedResponse findReceivedRollingPapers(Long userId, Long groupId,
Long targetId, Integer limit) {
List<Long> memberIds = getMemberIds(userId, groupId);
PageRequest pageRequest = PageRequest.ofSize(limit);
return rollingPaperRepository.findAllByMembersAfterCursor(memberIds, targetId, pageRequest).stream()
Slice<RollingPaper> rollingPapers = rollingPaperRepository.findAllByMembersAfterCursor(memberIds, targetId,
pageRequest);
Long nextCursorId = rollingPapers.hasNext() ?
rollingPapers.getContent().get(rollingPapers.getContent().size() - 1).getId() : null;
CursorPageInfo cursorPageInfo = CursorPageInfo.from(rollingPapers, nextCursorId);
List<RollingPaperResponse> responses = rollingPapers.stream()
.map(RollingPaperResponse::from)
.toList();
return RollingPaperPagedResponse.from(cursorPageInfo, responses);
}

private List<Sticker> parseStickers(RollingPaperRequest request, RollingPaper rollingPaper) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package slvtwn.khu.toyouserver.dto;

import org.springframework.data.domain.Slice;

public record CursorPageInfo(Long nextCursorId, int numberOfElements, boolean hasNext) {
public static CursorPageInfo from(Slice<?> data, Long lastCursor) {
return new CursorPageInfo(lastCursor, data.getNumberOfElements(), data.hasNext());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package slvtwn.khu.toyouserver.dto;

import java.util.List;

public record RollingPaperPagedResponse(CursorPageInfo cursorPageInfo, List<RollingPaperResponse> contents) {

public static RollingPaperPagedResponse from(CursorPageInfo cursorPageInfo, List<RollingPaperResponse> contents) {
return new RollingPaperPagedResponse(cursorPageInfo, contents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

import java.util.List;
import slvtwn.khu.toyouserver.domain.RollingPaper;
import slvtwn.khu.toyouserver.domain.Sticker;

public record RollingPaperResponse(String coverImageUrl, String title, String content, Long themeId,
public record RollingPaperResponse(Long id, String coverImageUrl, String title, String content, Long themeId,
List<StickerResponse> stickers) {

public static RollingPaperResponse from(RollingPaper rollingPaper) {
List<StickerResponse> stickerResponses = rollingPaper.getStickers().stream()
.map(StickerResponse::from)
.toList();
public static RollingPaperResponse from(RollingPaper rollingPaper) {
List<StickerResponse> stickerResponses = rollingPaper.getStickers().stream()
.map(StickerResponse::from)
.toList();

return new RollingPaperResponse(rollingPaper.getCoverImageUrl(), rollingPaper.getTitle(),
rollingPaper.getContent(), rollingPaper.getThemeId(), stickerResponses);
}
return new RollingPaperResponse(rollingPaper.getId(), rollingPaper.getCoverImageUrl(), rollingPaper.getTitle(),
rollingPaper.getContent(), rollingPaper.getThemeId(), stickerResponses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
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;
import org.springframework.data.repository.query.Param;
Expand All @@ -10,7 +11,7 @@
public interface RollingPaperRepository extends JpaRepository<RollingPaper, Long> {

@Query("SELECT r FROM RollingPaper r WHERE r.member.id IN :memberIds AND r.id >= :targetId ORDER BY r.id DESC")
List<RollingPaper> findAllByMembersAfterCursor(@Param("memberIds") List<Long> memberIds,
@Param("targetId") Long targetId,
Pageable pageable);
Slice<RollingPaper> findAllByMembersAfterCursor(@Param("memberIds") List<Long> memberIds,
@Param("targetId") Long targetId,
Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package slvtwn.khu.toyouserver.presentation;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -12,6 +11,7 @@
import slvtwn.khu.toyouserver.common.authentication.UserAuthentication;
import slvtwn.khu.toyouserver.common.response.ToyouResponse;
import slvtwn.khu.toyouserver.dto.CoverRequest;
import slvtwn.khu.toyouserver.dto.RollingPaperPagedResponse;
import slvtwn.khu.toyouserver.dto.RollingPaperRequest;
import slvtwn.khu.toyouserver.dto.RollingPaperResponse;

Expand All @@ -28,10 +28,10 @@ public ToyouResponse<RollingPaperResponse> findById(@UserAuthentication Long use
}

@GetMapping("/rollingpapers")
public ToyouResponse<List<RollingPaperResponse>> findReceivedRollingPapers(@UserAuthentication Long userId,
@RequestParam(required = false) Long groupId,
@RequestParam(defaultValue = "0") Long targetId,
@RequestParam(defaultValue = "10") int limit) {
public ToyouResponse<RollingPaperPagedResponse> findReceivedRollingPapers(@UserAuthentication Long userId,
@RequestParam(required = false) Long groupId,
@RequestParam(defaultValue = "0") Long targetId,
@RequestParam(defaultValue = "10") int limit) {
return ToyouResponse.from(rollingPaperService.findReceivedRollingPapers(userId, groupId, targetId, limit));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import slvtwn.khu.toyouserver.domain.Member;
import slvtwn.khu.toyouserver.domain.RollingPaper;
import slvtwn.khu.toyouserver.domain.User;
import slvtwn.khu.toyouserver.dto.RollingPaperPagedResponse;
import slvtwn.khu.toyouserver.dto.RollingPaperRequest;
import slvtwn.khu.toyouserver.dto.RollingPaperResponse;

Expand Down Expand Up @@ -120,11 +121,11 @@ class RollingPaperServiceTest {
entityManager.persist(rollingPaper);

// when
List<RollingPaperResponse> response = rollingPaperService.findReceivedRollingPapers(user.getId(), group.getId(),
RollingPaperPagedResponse response = rollingPaperService.findReceivedRollingPapers(user.getId(), group.getId(),
0L, 10);

// then
assertThat(response).usingRecursiveComparison()
assertThat(response.contents()).usingRecursiveComparison()
.isEqualTo(List.of(RollingPaperResponse.from(rollingPaper)));
}

Expand All @@ -148,13 +149,13 @@ class RollingPaperServiceTest {
entityManager.persist(anotherRollingPaper);

// when
List<RollingPaperResponse> responseWithoutGroupId = rollingPaperService.findReceivedRollingPapers(
RollingPaperPagedResponse responseWithoutGroupId = rollingPaperService.findReceivedRollingPapers(
user.getId(),
null,
0L, 10);

// then
assertThat(responseWithoutGroupId).usingRecursiveComparison()
assertThat(responseWithoutGroupId.contents()).usingRecursiveComparison()
.isEqualTo(List.of(
RollingPaperResponse.from(anotherRollingPaper),
RollingPaperResponse.from(rollingPaper)));
Expand All @@ -181,13 +182,13 @@ class RollingPaperServiceTest {
entityManager.persist(anotherRollingPaper);

// when
List<RollingPaperResponse> responseWithGroupId = rollingPaperService.findReceivedRollingPapers(
RollingPaperPagedResponse responseWithGroupId = rollingPaperService.findReceivedRollingPapers(
user.getId(),
group1.getId(),
0L, 10);

// then
assertThat(responseWithGroupId).usingRecursiveComparison()
assertThat(responseWithGroupId.contents()).usingRecursiveComparison()
.isEqualTo(List.of(RollingPaperResponse.from(rollingPaper)));
}

Expand All @@ -214,7 +215,7 @@ class RollingPaperServiceTest {
entityManager.persist(rollingPaper3);

// when
List<RollingPaperResponse> response = rollingPaperService.findReceivedRollingPapers(
RollingPaperPagedResponse response = rollingPaperService.findReceivedRollingPapers(
user.getId(),
group1.getId(),
0L, 10);
Expand All @@ -225,7 +226,7 @@ class RollingPaperServiceTest {
RollingPaperResponse.from(rollingPaper1)
);

assertThat(response).usingRecursiveComparison()
assertThat(response.contents()).usingRecursiveComparison()
.isEqualTo(expectedResponse);
}

Expand All @@ -248,7 +249,7 @@ class RollingPaperServiceTest {
entityManager.persist(rollingPaper3);

// when
List<RollingPaperResponse> response = rollingPaperService.findReceivedRollingPapers(
RollingPaperPagedResponse response = rollingPaperService.findReceivedRollingPapers(
user.getId(),
group.getId(),
0L, 10);
Expand All @@ -260,7 +261,7 @@ class RollingPaperServiceTest {
RollingPaperResponse.from(rollingPaper1)
);

assertThat(response).usingRecursiveComparison()
assertThat(response.contents()).usingRecursiveComparison()
.isEqualTo(expectedResponse);
}
}