Skip to content

Commit

Permalink
#28 [update]
Browse files Browse the repository at this point in the history
최근 등록한 북마크 구현
  • Loading branch information
kokoa322 committed Jul 28, 2022
1 parent a7f8c41 commit 2eff2c0
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.mpnp.baechelin.bookmark.controller;

import com.mpnp.baechelin.bookmark.dto.BookmarkInfoDto;
import com.mpnp.baechelin.bookmark.dto.BookmarkRequestDto;
import com.mpnp.baechelin.bookmark.service.BookmarkService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
public class BookmarkController {
Expand All @@ -35,4 +41,12 @@ public ResponseEntity<?> bookmarkDelete(@PathVariable int bookmarkId,

return new ResponseEntity<>(HttpStatus.OK);
}

@GetMapping("/bookmarkTop")
public ResponseEntity<?> bookmarkTop(@AuthenticationPrincipal User user,
@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);
}
}
24 changes: 21 additions & 3 deletions src/main/java/com/mpnp/baechelin/bookmark/dto/BookmarkInfoDto.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.mpnp.baechelin.bookmark.dto;


import com.mpnp.baechelin.store.domain.StoreImage;
import com.mpnp.baechelin.bookmark.domain.Bookmark;
import lombok.*;

import java.util.List;

@Getter
@Setter
Expand All @@ -18,7 +17,26 @@ public class BookmarkInfoDto {
private String address;
private String category;
private String phoneNumber;
private String storeImageList;
private int bookmarkId;
private int storeId;
String storeImageList;


public BookmarkInfoDto(Bookmark bookmark){

this.pointAvg = Math.round(bookmark.getStoreId().getPointAvg()*10)/10.0;
this.name = bookmark.getStoreId().getName();
this.address = bookmark.getStoreId().getAddress();
this.category = bookmark.getStoreId().getCategory();
this.phoneNumber = bookmark.getStoreId().getPhoneNumber();
this.bookmarkId = bookmark.getId();
this.storeId = (int) bookmark.getStoreId().getId();

if(bookmark.getStoreId().getStoreImageList() == null) {
this.storeImageList = bookmark.getStoreId().getStoreImageList().get(0).getStoreImageUrl();

} else if(bookmark.getStoreId().getStoreImageList() != null) {
this.storeImageList = "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import com.mpnp.baechelin.bookmark.domain.Folder;
import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.user.domain.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface BookmarkRepository extends JpaRepository<Bookmark, Integer> {
List<Bookmark> findAllByFolderId(Folder folderId);
boolean existsByStoreIdAndUserId(Store store, User user);

Page<Bookmark> findAllByUserId(User user, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

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.BookmarkRequestDto;
import com.mpnp.baechelin.bookmark.repository.BookmarkRepository;
import com.mpnp.baechelin.bookmark.repository.FolderRepository;
import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.store.domain.StoreImage;
import com.mpnp.baechelin.store.repository.StoreImgRepository;
import com.mpnp.baechelin.store.repository.StoreRepository;
import com.mpnp.baechelin.user.domain.User;
import com.mpnp.baechelin.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
Expand All @@ -22,7 +29,8 @@ public class BookmarkService {
private final FolderRepository folderRepository;
private final StoreRepository storeRepository;
private final UserRepository userRepository;

private final StoreImgRepository storeImgRepository;
// private final StoreImageRepository storeImageRepository;
public void bookmark(BookmarkRequestDto bookmarkRequestDto, String socialId) {

Folder folder = folderRepository.findById(bookmarkRequestDto.getFolderId()).orElseThrow(()-> new IllegalArgumentException("폴더가 존재하지 않습니다"));
Expand All @@ -47,4 +55,36 @@ public void bookmarkDelete(int bookmarkId, String socialId) {
bookmarkRepository.deleteById(bookmarkId);

}

public List<BookmarkInfoDto> bookmarkTop(String socialId, Pageable pageable) {

User user = userRepository.findBySocialId(socialId);
Page<Bookmark> bookmarkPage = bookmarkRepository.findAllByUserId(user, pageable);


List<BookmarkInfoDto> bookmarkList = new ArrayList<>();
for(Bookmark bookmark: bookmarkPage){
Optional<Store> store = storeRepository.findById(bookmark.getStoreId().getId());
List<StoreImage> storeImageList = storeImgRepository.findAllByStoreId(store.get().getId());

BookmarkInfoDto bookmarkInfoDto = BookmarkInfoDto
.builder()
.bookmarkId(bookmark.getId())
.name(store.get().getName())
.phoneNumber(store.get().getPhoneNumber())
.category(store.get().getCategory())
.address(store.get().getAddress())
.pointAvg(Math.round(store.get().getPointAvg()*10)/10.0)
.storeId((int) store.get().getId())
.storeImageList(!storeImageList.isEmpty() ? storeImageList.get(0).getStoreImageUrl():"")
.build();

// BookmarkInfoDto bookmarkInfoDto = new BookmarkInfoDto(bookmark);
bookmarkList.add(bookmarkInfoDto);
}



return bookmarkList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import com.mpnp.baechelin.review.domain.ReviewImage;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ReviewImageRepository extends JpaRepository<ReviewImage, Integer> {

void deleteAllByReviewId(Review review);
void deleteByReviewId(Review review);

void deleteAllInBatchByReviewId(Review review);

void deleteInBatchByReviewId(Review review);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import com.mpnp.baechelin.util.AwsS3Manager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.SQLDelete;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

Expand Down Expand Up @@ -85,24 +87,23 @@ public void review(ReviewRequestDto reviewRequestDto, String socialId) throws IO
}


/** 리뷰 조회 */

public PageInfoResponseDto getReview(long storeId, String socialId, Pageable pageable) {

Store store = storeRepository.findById(storeId).orElseThrow(() -> new IllegalArgumentException("해당 가게가 없습니다"));
User myUser = userRepository.findBySocialId(socialId);
Store store = storeRepository.findById(storeId).orElseThrow(() -> new IllegalArgumentException("해당 가게가 없습니다"));
Page<Review> reviewList = reviewRepository.findAllByStoreId(store, pageable);
List<ReviewResponseDto> reviewResponseDtoList = new ArrayList<>();
User myUser = userRepository.findBySocialId(socialId);



List<ReviewResponseDto> reviewResponseDtoList = new ArrayList<>();
for(Review review: reviewList){
ReviewResponseDto reviewResponseDto = new ReviewResponseDto(review);
Optional<User> user = userRepository.findById(reviewResponseDto.getUserId());
reviewResponseDto.userInfo(user.get(), myUser);
reviewResponseDtoList.add(reviewResponseDto);
}

reviewList.isFirst();

PageInfoResponseDto pageInfoResponseDto = PageInfoResponseDto
.builder()
Expand All @@ -120,8 +121,8 @@ public PageInfoResponseDto getReview(long storeId, String socialId, Pageable pag
}



@Transactional
@Modifying
/** 리뷰 수정 */
public void reviewUpdate(ReviewRequestDto reviewRequestDto, String socialId, int reviewId) throws IOException {

Expand All @@ -145,8 +146,7 @@ public void reviewUpdate(ReviewRequestDto reviewRequestDto, String socialId, int
awsS3Manager.deleteFile(reviewImage.getReviewImageUrl().substring(reviewImage.getReviewImageUrl().indexOf("com/") + 4));

}
System.out.println(review.getId());
reviewImageRepository.deleteAllByReviewId(review);
reviewImageRepository.deleteInBatchByReviewId(review);
}


Expand All @@ -170,7 +170,7 @@ public void reviewUpdate(ReviewRequestDto reviewRequestDto, String socialId, int
// 1. 기존 태그 내용이 있다면 전체 삭제
if (oldTagList != null){
System.out.println("oldTagList != null");
tagRepository.deleteAByReviewId(review); }
tagRepository.deleteAllByReviewId(review); }
// 2. 태그 내용이 있다면 태그 수정
if (newTagList != null) {
for (String newTag : newTagList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package com.mpnp.baechelin.store.repository;

import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.store.domain.StoreImage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface StoreImgRepository extends JpaRepository<StoreImage, Integer> {

List<StoreImage> findAllByStoreId(Long storeId);

StoreImage findByStoreId(long storeId);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.mpnp.baechelin.store.repository;

import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.store.domain.StoreImage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

public interface StoreRepository extends JpaRepository<Store, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ public interface TagRepository extends JpaRepository<Tag, Integer> {

List<Tag> findAllByReviewId(Review review);

void deleteAByReviewId(Review review);
}

0 comments on commit 2eff2c0

Please sign in to comment.