Skip to content

Commit

Permalink
#28 [Add] API 추가 및 분리
Browse files Browse the repository at this point in the history
북마크 폴더에 접근할 시 북마크 리스트를 반환하는 메서드
  • Loading branch information
JSoi committed Aug 4, 2022
1 parent 7a4236e commit 9d987ad
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
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;
import com.mpnp.baechelin.exception.CustomException;
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;
Expand Down Expand Up @@ -36,19 +39,36 @@ 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);
}
bookmarkService.bookmarkDelete(bookmarkId, user.getUsername());
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<List<BookmarkInfoDto>> 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<BookmarkInfoDto> bookmarkList = bookmarkService.bookmarkTop(user.getUsername(), pageable);
return new ResponseEntity<>(bookmarkList, HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class BookmarkInfoDto {
private String approach;
private int bookmarkId;
private int storeId;
private String bookmark;


public BookmarkInfoDto(Bookmark bookmark){
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BookmarkInfoDto> cards;

public BookmarkPagedResponseDto(Page<Bookmark> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bookmark, Integer> {
List<Bookmark> findAllByFolderId(Folder folderId);
public interface BookmarkRepository extends CrudRepository<Bookmark, Integer> {
Page<Bookmark> findAllByFolderId(Folder folderId, Pageable pageable);
boolean existsByStoreIdAndUserId(Store store, User user);

Page<Bookmark> findAllByUserId(User user, Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -78,4 +80,15 @@ public List<BookmarkInfoDto> 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<Bookmark> pagedBookmark = bookmarkRepository.findAllByFolderId(folder, pageable);
return new BookmarkPagedResponseDto(pagedBookmark);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down

0 comments on commit 9d987ad

Please sign in to comment.