-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from Nawabali-project/feature/bookmarkRefactor
FIX : 유저 북마크 조회 시 무한 스크롤로 구현
- Loading branch information
Showing
12 changed files
with
190 additions
and
79 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
44 changes: 44 additions & 0 deletions
44
src/main/java/com/nawabali/nawabali/dto/querydsl/BookmarkDslDto.java
This file contains 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,44 @@ | ||
package com.nawabali.nawabali.dto.querydsl; | ||
|
||
import lombok.*; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public class BookmarkDslDto { | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public static class UserBookmarkDto { | ||
|
||
private Long userId; | ||
|
||
private Long postId; | ||
|
||
private String nickname; | ||
|
||
private String contents; | ||
|
||
private String category; | ||
|
||
private String district; | ||
|
||
private Double latitude; | ||
|
||
private Double longitude; | ||
|
||
private LocalDateTime createdAt; | ||
|
||
private List<String> imageUrls; | ||
|
||
private Long likesCount; | ||
|
||
private Long localLikesCount; | ||
|
||
private int commentCount; | ||
|
||
} | ||
} |
This file contains 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
51 changes: 38 additions & 13 deletions
51
...a/com/nawabali/nawabali/repository/querydsl/bookmark/BookmarkDslRepositoryCustomImpl.java
This file contains 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,40 +1,65 @@ | ||
package com.nawabali.nawabali.repository.querydsl.bookmark; | ||
|
||
import com.nawabali.nawabali.domain.QBookMark; | ||
import com.nawabali.nawabali.domain.QPost; | ||
import com.nawabali.nawabali.domain.User; | ||
import com.nawabali.nawabali.domain.*; | ||
import com.nawabali.nawabali.domain.image.PostImage; | ||
import com.nawabali.nawabali.dto.BookMarkDto; | ||
import com.nawabali.nawabali.dto.querydsl.BookmarkDslDto; | ||
import com.querydsl.core.types.Projections; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import jakarta.persistence.EntityManager; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.data.domain.SliceImpl; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class BookmarkDslRepositoryCustomImpl implements BookmarkDslRepositoryCustom{ | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
public List<BookMarkDto.UserBookmarkDto> getUserBookmarks(User user) { | ||
public Slice<BookmarkDslDto.UserBookmarkDto> getUserBookmarks(User user, Pageable pageable) { | ||
QBookMark bookMark = QBookMark.bookMark; | ||
QPost post = QPost.post; | ||
QUser quser = QUser.user; | ||
|
||
List<BookMarkDto.UserBookmarkDto> bookmarks = queryFactory | ||
.select(Projections.fields(BookMarkDto.UserBookmarkDto.class, | ||
bookMark.id.as("bookmarkId"), | ||
bookMark.post.id.as("postId"), | ||
bookMark.user.id.as("userId") )) | ||
.from(bookMark) | ||
List<BookMark> bookmarks = queryFactory | ||
.selectFrom(bookMark) | ||
.where(bookMark.user.id.eq(user.getId())) | ||
.join(bookMark.post, post) | ||
.join(bookMark.post, post).fetchJoin() | ||
.join(post.user ,quser).fetchJoin() | ||
.orderBy(bookMark.createdAt.desc()) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()+1) | ||
.fetch(); | ||
|
||
return bookmarks; | ||
} | ||
boolean hasNext = bookmarks.size() > pageable.getPageSize(); | ||
if(hasNext) { | ||
bookmarks.remove(bookmarks.size() - 1); | ||
} | ||
|
||
List<BookmarkDslDto.UserBookmarkDto> responseDtos = bookmarks.stream() | ||
.map(newBookmark -> BookmarkDslDto.UserBookmarkDto.builder() | ||
.userId(newBookmark.getUser().getId()) | ||
.postId(newBookmark.getPost().getId()) | ||
.nickname(newBookmark.getUser().getNickname()) | ||
.contents(newBookmark.getPost().getContents()) | ||
.category(newBookmark.getPost().getCategory().name()) | ||
.district(newBookmark.getPost().getTown().getDistrict()) | ||
.latitude(newBookmark.getPost().getTown().getLatitude()) | ||
.longitude(newBookmark.getPost().getTown().getLongitude()) | ||
.createdAt(newBookmark.getCreatedAt()) | ||
.imageUrls(newBookmark.getPost().getImages().stream().map(PostImage::getImgUrl).collect(Collectors.toList())) | ||
.commentCount(newBookmark.getPost().getComments().size()) | ||
.build()) | ||
.collect(Collectors.toList()); | ||
|
||
return new SliceImpl<>(responseDtos, pageable, hasNext); | ||
} | ||
|
||
} |
Oops, something went wrong.