From 9d987ad0a349431903815634c7432d1a871d65eb Mon Sep 17 00:00:00 2001 From: JSoi Date: Fri, 5 Aug 2022 00:32:55 +0900 Subject: [PATCH] =?UTF-8?q?#28=20[Add]=20API=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EB=B0=8F=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 북마크 폴더에 접근할 시 북마크 리스트를 반환하는 메서드 --- .../controller/BookmarkController.java | 24 +++++++++++-- .../bookmark/dto/BookmarkInfoDto.java | 3 +- .../dto/BookmarkPagedResponseDto.java | 34 +++++++++++++++++++ .../repository/BookmarkRepository.java | 5 +-- .../bookmark/service/BookmarkService.java | 15 +++++++- .../bookmark/service/FolderService.java | 4 +++ 6 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/mpnp/baechelin/bookmark/dto/BookmarkPagedResponseDto.java diff --git a/src/main/java/com/mpnp/baechelin/bookmark/controller/BookmarkController.java b/src/main/java/com/mpnp/baechelin/bookmark/controller/BookmarkController.java index d2f5bbb..aa37393 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/controller/BookmarkController.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/controller/BookmarkController.java @@ -1,6 +1,8 @@ package com.mpnp.baechelin.bookmark.controller; +import com.mpnp.baechelin.bookmark.domain.Bookmark; import com.mpnp.baechelin.bookmark.dto.BookmarkInfoDto; +import com.mpnp.baechelin.bookmark.dto.BookmarkPagedResponseDto; import com.mpnp.baechelin.bookmark.dto.BookmarkRequestDto; import com.mpnp.baechelin.bookmark.service.BookmarkService; import com.mpnp.baechelin.common.SuccessResponse; @@ -8,6 +10,7 @@ import com.mpnp.baechelin.exception.ErrorCode; import lombok.RequiredArgsConstructor; import org.springframework.cache.annotation.CacheEvict; +import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.web.PageableDefault; @@ -36,9 +39,10 @@ public SuccessResponse bookmark(@RequestBody BookmarkRequestDto bookmarkRequestD bookmarkService.bookmark(bookmarkRequestDto, user.getUsername()); return new SuccessResponse("북마크를 폴더에 저장 완료"); } + @DeleteMapping("/bookmark/{bookmarkId}") public SuccessResponse bookmarkDelete(@PathVariable int bookmarkId, - @AuthenticationPrincipal User user) { + @AuthenticationPrincipal User user) { if (user == null) { throw new CustomException(ErrorCode.NO_USER_FOUND); } @@ -46,9 +50,25 @@ public SuccessResponse bookmarkDelete(@PathVariable int bookmarkId, return new SuccessResponse("북마크를 삭제 완료"); } + /** + * @param folderId 조회하고자 하는 폴더 아이디 + * @param pageable 페이징 객체 + * @param user 접근하는 유저 + * @return 페이징된 객체와 List를 안에 넣어 리턴한다 - bookmark는 항상 "Y" + */ + @GetMapping("/bookmark/{folderId}") + public BookmarkPagedResponseDto bookmarkList(@PathVariable int folderId, + @PageableDefault(size = 20, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable, + @AuthenticationPrincipal User user) { + if (user == null) { + throw new CustomException(ErrorCode.NO_USER_FOUND); + } + return bookmarkService.bookmarkList(user.getUsername(), folderId, pageable); + } + @GetMapping("/bookmarkTop") public ResponseEntity> bookmarkTop(@AuthenticationPrincipal User user, - @PageableDefault(page = 0, size = 5, sort = "id", direction = Sort.Direction.DESC) Pageable pageable) { + @PageableDefault(page = 0, size = 5, sort = "id", direction = Sort.Direction.DESC) Pageable pageable) { List bookmarkList = bookmarkService.bookmarkTop(user.getUsername(), pageable); return new ResponseEntity<>(bookmarkList, HttpStatus.OK); diff --git a/src/main/java/com/mpnp/baechelin/bookmark/dto/BookmarkInfoDto.java b/src/main/java/com/mpnp/baechelin/bookmark/dto/BookmarkInfoDto.java index f3a444d..d27e46f 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/dto/BookmarkInfoDto.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/dto/BookmarkInfoDto.java @@ -25,6 +25,7 @@ public class BookmarkInfoDto { private String approach; private int bookmarkId; private int storeId; + private String bookmark; public BookmarkInfoDto(Bookmark bookmark){ @@ -43,7 +44,7 @@ public BookmarkInfoDto(Bookmark bookmark){ this.bookmarkId = bookmark.getId(); this.storeId = (int) bookmark.getStoreId().getId(); - + this.bookmark = "Y"; if(!bookmark.getStoreId().getStoreImageList().isEmpty()) { this.storeImageList = bookmark.getStoreId().getStoreImageList().get(0).getStoreImageUrl(); diff --git a/src/main/java/com/mpnp/baechelin/bookmark/dto/BookmarkPagedResponseDto.java b/src/main/java/com/mpnp/baechelin/bookmark/dto/BookmarkPagedResponseDto.java new file mode 100644 index 0000000..c14118c --- /dev/null +++ b/src/main/java/com/mpnp/baechelin/bookmark/dto/BookmarkPagedResponseDto.java @@ -0,0 +1,34 @@ +package com.mpnp.baechelin.bookmark.dto; + +import com.mpnp.baechelin.bookmark.domain.Bookmark; +import com.mpnp.baechelin.store.domain.Store; +import com.mpnp.baechelin.store.dto.StoreCardResponseDto; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.springframework.data.domain.Page; + +import java.util.List; +import java.util.stream.Collectors; + +@Getter +@Setter +@NoArgsConstructor +public class BookmarkPagedResponseDto { + private boolean hasNextPage; + private long totalCount; + private long leftElement; + private int page; + private int totalPage; + private List cards; + + public BookmarkPagedResponseDto(Page resultStoreList) { + this.hasNextPage = resultStoreList.hasNext(); + this.totalPage = resultStoreList.getTotalPages() - 1; + this.cards = resultStoreList.get().collect(Collectors.toList()).stream().map(BookmarkInfoDto::new).collect(Collectors.toList()); + this.totalCount = resultStoreList.getTotalElements(); + this.page = resultStoreList.getNumber(); + this.leftElement = totalCount - (long) page * resultStoreList.getSize() - resultStoreList.getNumberOfElements(); + this.leftElement = this.leftElement < 0 ? 0 : this.leftElement; + } +} diff --git a/src/main/java/com/mpnp/baechelin/bookmark/repository/BookmarkRepository.java b/src/main/java/com/mpnp/baechelin/bookmark/repository/BookmarkRepository.java index 11b5dec..684e9e8 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/repository/BookmarkRepository.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/repository/BookmarkRepository.java @@ -7,11 +7,12 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.repository.CrudRepository; import java.util.List; -public interface BookmarkRepository extends JpaRepository { - List findAllByFolderId(Folder folderId); +public interface BookmarkRepository extends CrudRepository { + Page findAllByFolderId(Folder folderId, Pageable pageable); boolean existsByStoreIdAndUserId(Store store, User user); Page findAllByUserId(User user, Pageable pageable); diff --git a/src/main/java/com/mpnp/baechelin/bookmark/service/BookmarkService.java b/src/main/java/com/mpnp/baechelin/bookmark/service/BookmarkService.java index ce1e313..871e2f3 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/service/BookmarkService.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/service/BookmarkService.java @@ -3,6 +3,7 @@ import com.mpnp.baechelin.bookmark.domain.Bookmark; import com.mpnp.baechelin.bookmark.domain.Folder; import com.mpnp.baechelin.bookmark.dto.BookmarkInfoDto; +import com.mpnp.baechelin.bookmark.dto.BookmarkPagedResponseDto; import com.mpnp.baechelin.bookmark.dto.BookmarkRequestDto; import com.mpnp.baechelin.bookmark.repository.BookmarkRepository; import com.mpnp.baechelin.bookmark.repository.FolderRepository; @@ -17,12 +18,14 @@ import lombok.RequiredArgsConstructor; import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import javax.transaction.Transactional; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -32,7 +35,6 @@ public class BookmarkService { private final FolderRepository folderRepository; private final StoreRepository storeRepository; private final UserRepository userRepository; - private final StoreImgRepository storeImgRepository; private final StoreService storeService; @Transactional @@ -78,4 +80,15 @@ public List bookmarkTop(String socialId, Pageable pageable) { } return bookmarkList; } + + @Transactional + public BookmarkPagedResponseDto bookmarkList(String socialId, int folderId, Pageable pageable) { + User user = userRepository.findBySocialId(socialId); + if (user == null) { + throw new CustomException(ErrorCode.NO_USER_FOUND); + } + Folder folder = folderRepository.findById(folderId).orElseThrow(()-> new CustomException(ErrorCode.NO_FOLDER_FOUND)); + Page pagedBookmark = bookmarkRepository.findAllByFolderId(folder, pageable); + return new BookmarkPagedResponseDto(pagedBookmark); + } } diff --git a/src/main/java/com/mpnp/baechelin/bookmark/service/FolderService.java b/src/main/java/com/mpnp/baechelin/bookmark/service/FolderService.java index 121e26d..4e4151f 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/service/FolderService.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/service/FolderService.java @@ -1,8 +1,11 @@ package com.mpnp.baechelin.bookmark.service; +import com.mpnp.baechelin.bookmark.domain.Bookmark; import com.mpnp.baechelin.bookmark.domain.Folder; +import com.mpnp.baechelin.bookmark.dto.BookmarkInfoDto; import com.mpnp.baechelin.bookmark.dto.FolderRequestDto; import com.mpnp.baechelin.bookmark.dto.FolderResponseDto; +import com.mpnp.baechelin.bookmark.repository.BookmarkRepository; import com.mpnp.baechelin.bookmark.repository.FolderRepository; import com.mpnp.baechelin.exception.CustomException; @@ -16,6 +19,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor