Skip to content

Commit

Permalink
[fix] : 마이페이지 조회 페이지네이션 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
evergreenn committed Mar 27, 2024
1 parent ebade10 commit 3ee6142
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand Down Expand Up @@ -131,12 +135,10 @@ public HttpResponse<UserProfileResponse> getCurrentUserProfile() {
@GetMapping("/likes-bookmarks")
@Operation(summary = "사용자 좋아요/북마크 조회", description = "로그인한 사용자의 등록한 좋아요/북마크 조회를 수행합니다.")

public HttpResponse<List<BoomarkLikeResponseDto>> getUserBookmarksandLikes() {

List<BoomarkLikeResponseDto> bookmarkandLikes = userService.getBookmarkandLikes();


return HttpResponse.okBuild(bookmarkandLikes);
public HttpResponse<Page<BoomarkLikeResponseDto>> getUserBookmarksandLikes(@RequestParam(defaultValue = "5") int page,
@RequestParam(defaultValue = "5") int size) {
Page<BoomarkLikeResponseDto> bookmarkAndLikes = userService.getBookmarkAndLikes(page, size);
return HttpResponse.okBuild(bookmarkAndLikes);
}


Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/cona/KUsukKusuk/user/dto/PageInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.cona.KUsukKusuk.user.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class PageInfo {
private int page;
private int size;
private long totalElements;
private int totalPages;
}
40 changes: 40 additions & 0 deletions src/main/java/com/cona/KUsukKusuk/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -280,5 +284,41 @@ public List<BoomarkLikeResponseDto> getBookmarkandLikes() {

}

public Page<BoomarkLikeResponseDto> getBookmarkAndLikes(int page, int size) {
String username = getUsernameBySecurityContext();
User user = findUserByUserid(username);

List<Bookmark> bookmarks = bookmarkRepository.findByUser(user);
List<UserLike> userLikes = userLikeRepository.findByUser(user);

List<Spot> bookmarkedSpots = new ArrayList<>();
List<Spot> likedSpots = new ArrayList<>();

if (bookmarks != null) {
bookmarkedSpots = bookmarks.stream()
.map(Bookmark::getSpot)
.collect(Collectors.toList());
}

if (userLikes != null) {
likedSpots = userLikes.stream()
.map(UserLike::getSpot)
.collect(Collectors.toList());
}

List<Spot> distinctSpots = Stream.concat(bookmarkedSpots.stream(), likedSpots.stream())
.distinct()
.collect(Collectors.toList());

List<BoomarkLikeResponseDto> spotResponses = distinctSpots.stream()
.map(spot -> BoomarkLikeResponseDto.of(spot, bookmarks.contains(spot), userLikes.contains(spot)))
.collect(Collectors.toList());

int start = page * size;
int end = Math.min(start + size, spotResponses.size());

return new PageImpl<>(spotResponses.subList(start, end), PageRequest.of(page, size), spotResponses.size());
}


}

0 comments on commit 3ee6142

Please sign in to comment.