Skip to content

Commit

Permalink
Merge pull request #80 from Team-PLAT/feat/#78/pagingHasnextAdd
Browse files Browse the repository at this point in the history
[FEAT] 페이징 기능에 hasnext 변수 추가
  • Loading branch information
dgh06175 authored Nov 2, 2024
2 parents 5f4e8c8 + c69a7ea commit a0fd364
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

public class PlaylistResponse {
@Getter
Expand All @@ -23,6 +26,7 @@ public static class PlayListId {
@NoArgsConstructor
public static class Playlists {
private List<PlaylistInfo> playlists;
private boolean hasNext;

@Getter
@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ public PlaylistResponse.PlayListId toPlaylistId(Long playlistId) {
.build();
}

public PlaylistResponse.Playlists toPlaylists(List<PlaylistInfo> playlistInfos) {
public PlaylistResponse.Playlists toPlaylists(List<PlaylistInfo> playlistInfos, boolean hasNext) {
return PlaylistResponse.Playlists.builder()
.playlists(playlistInfos)
.hasNext(hasNext)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -70,12 +71,12 @@ public PlaylistResponse.Playlists getPlaylists(Member member, int page, int size
Pageable pageable = PageRequest.of(page, size, Sort.by(sorts));

// TODO: 효율 개선
List<Playlist> playlists = playlistRepository.findAllByMember(member, pageable).getContent();
List<PlaylistResponse.Playlists.PlaylistInfo> playlistInfos = playlists.stream().map(playlist -> {
Page<Playlist> playlists = playlistRepository.findAllByMember(member, pageable);
List<PlaylistResponse.Playlists.PlaylistInfo> playlistInfos = playlists.getContent().stream().map(playlist -> {
List<PlaylistTrack> playlistTracks = findPlaylistTracksInPlaylist(playlist);
return playlistMapper.toPlaylistInfo(playlist, playlistTracks);
}).toList();
return playlistMapper.toPlaylists(playlistInfos);
return playlistMapper.toPlaylists(playlistInfos, playlists.hasNext());
}

@Override
Expand All @@ -85,20 +86,20 @@ public PlaylistResponse.Playlists getSearchedPlaylists(Member member, String tit
.toLowerCase();

if (refinedTitle.isEmpty() || refinedTitle.isBlank()) {
return playlistMapper.toPlaylists(Collections.emptyList());
return playlistMapper.toPlaylists(Collections.emptyList(), false);
}

Pageable pageable = PageRequest.of(page, size, Sort.by("createdAt").descending());

List<Playlist> playlists = playlistRepository.
findAllByMemberAndTitleContainingIgnoreCase(member, refinedTitle, pageable).getContent();
Page<Playlist> playlists = playlistRepository.
findAllByMemberAndTitleContainingIgnoreCase(member, refinedTitle, pageable);

// TODO: 효율 개선
List<PlaylistResponse.Playlists.PlaylistInfo> playlistInfos = playlists.stream().map(playlist -> {
List<PlaylistResponse.Playlists.PlaylistInfo> playlistInfos = playlists.getContent().stream().map(playlist -> {
List<PlaylistTrack> playlistTracks = findPlaylistTracksInPlaylist(playlist);
return playlistMapper.toPlaylistInfo(playlist, playlistTracks);
}).toList();
return playlistMapper.toPlaylists(playlistInfos);
return playlistMapper.toPlaylists(playlistInfos, playlists.hasNext());
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static class TrackMapList {
public static class TrackDetailList {
@Schema(description = "트랙 디테일 리스트")
private List<TrackDetail> trackDetails;
private boolean hasNext;
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import com.cabin.plat.domain.track.dto.TrackRequest;
import com.cabin.plat.domain.track.dto.TrackResponse;
import com.cabin.plat.domain.track.dto.TrackResponse.TrackMap;
import com.cabin.plat.domain.track.entity.*;
import com.cabin.plat.domain.track.entity.Location;
import com.cabin.plat.domain.track.entity.Track;
import com.cabin.plat.domain.track.entity.TrackLike;
import com.cabin.plat.domain.track.entity.TrackReport;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -71,9 +74,11 @@ public TrackResponse.MemberInfo toMemberInfo(Long memberId, String memberNicknam
.build();
}

public TrackResponse.TrackDetailList toTrackDetailList(List<TrackResponse.TrackDetail> trackDetails) {
public TrackResponse.TrackDetailList toTrackDetailList(List<TrackResponse.TrackDetail> trackDetails,
boolean hasNext) {
return TrackResponse.TrackDetailList.builder()
.trackDetails(trackDetails)
.hasNext(hasNext)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Optional;
import java.util.Random;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -170,14 +171,14 @@ public TrackResponse.TrackDetailList getTrackFeeds(Member member, int page, int
sorts.add(Sort.Order.desc("createdAt"));
Pageable pageable = PageRequest.of(page, size, Sort.by(sorts));

List<Track> tracks = trackRepository.findAll(pageable).getContent();
Page<Track> trackPage = trackRepository.findAll(pageable);

List<TrackResponse.TrackDetail> trackDetails = tracks.stream()
List<TrackResponse.TrackDetail> trackDetails = trackPage.getContent().stream()
.filter(track -> track.getDeletedAt() == null)
.map(track -> getTrackDetail(member, track))
.toList();

return trackMapper.toTrackDetailList(trackDetails);
return trackMapper.toTrackDetailList(trackDetails, trackPage.hasNext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,13 @@ class GetPlaylistsTests {

// then
assertThat(pagedPlaylists0.getPlaylists()).hasSize(1);
assertThat(pagedPlaylists0.isHasNext()).isTrue();
assertThat(pagedPlaylists1.getPlaylists()).hasSize(1);
assertThat(pagedPlaylists1.isHasNext()).isTrue();
assertThat(pagedPlaylists2.getPlaylists()).hasSize(1);
assertThat(pagedPlaylists2.isHasNext()).isFalse();
assertThat(pagedPlaylists3.getPlaylists()).hasSize(0);
assertThat(pagedPlaylists3.isHasNext()).isFalse();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.cabin.plat.domain.track.dto.TrackRequest;
import com.cabin.plat.domain.track.dto.TrackResponse;
import com.cabin.plat.domain.track.dto.TrackResponse.TrackDetail;
import com.cabin.plat.domain.track.dto.TrackResponse.TrackDetailList;
import com.cabin.plat.domain.track.dto.TrackResponse.TrackMap;
import com.cabin.plat.domain.track.entity.Location;
import com.cabin.plat.domain.track.entity.Track;
Expand Down Expand Up @@ -408,14 +409,17 @@ class getTrackFeedsTests {
Member member = members.get(0);

// when
List<TrackDetail> firstPageTracks = trackService.getTrackFeeds(member, 0, 4).getTrackDetails();
List<TrackDetail> secondPageTracks = trackService.getTrackFeeds(member, 1, 4).getTrackDetails();
List<TrackDetail> thirdPageTracks = trackService.getTrackFeeds(member, 2, 4).getTrackDetails();
TrackDetailList firstPageTracks = trackService.getTrackFeeds(member, 0, 4);
TrackDetailList secondPageTracks = trackService.getTrackFeeds(member, 1, 4);
TrackDetailList thirdPageTracks = trackService.getTrackFeeds(member, 2, 4);

// then
assertThat(firstPageTracks).hasSize(4);
assertThat(secondPageTracks).hasSize(2);
assertThat(thirdPageTracks).hasSize(0);
assertThat(firstPageTracks.getTrackDetails()).hasSize(4);
assertThat(firstPageTracks.isHasNext()).isTrue();
assertThat(secondPageTracks.getTrackDetails()).hasSize(2);
assertThat(secondPageTracks.isHasNext()).isFalse();
assertThat(thirdPageTracks.getTrackDetails()).hasSize(0);
assertThat(thirdPageTracks.isHasNext()).isFalse();
}

@Test
Expand Down

0 comments on commit a0fd364

Please sign in to comment.