diff --git a/src/main/java/com/book_everywhere/book/controller/BookController.java b/src/main/java/com/book_everywhere/book/controller/BookController.java index 52655a1..680caf2 100644 --- a/src/main/java/com/book_everywhere/book/controller/BookController.java +++ b/src/main/java/com/book_everywhere/book/controller/BookController.java @@ -32,7 +32,7 @@ public CMRespDto findOneBook(@PathVariable Long id) { //유저 책 조회 @GetMapping("/api/mypage/{socialId}") public CMRespDto findOneBookWithUser(@PathVariable Long socialId) { - List result = bookService.findAllBookOneUser(socialId); + List result = bookService.유저책조회(socialId); return new CMRespDto<>(HttpStatus.OK, result, "유저의 책 조회 완료"); } } diff --git a/src/main/java/com/book_everywhere/book/service/BookService.java b/src/main/java/com/book_everywhere/book/service/BookService.java index d7ffcb6..7d775cf 100644 --- a/src/main/java/com/book_everywhere/book/service/BookService.java +++ b/src/main/java/com/book_everywhere/book/service/BookService.java @@ -1,72 +1,16 @@ package com.book_everywhere.book.service; -import com.book_everywhere.book.entity.Book; -import com.book_everywhere.book.repository.BookRepository; import com.book_everywhere.book.dto.BookDto; -import com.book_everywhere.book.dto.BookRespDto; import com.book_everywhere.review.dto.ReviewRespDto; -import com.book_everywhere.exception.customs.CustomErrorCode; -import com.book_everywhere.exception.customs.EntityNotFoundException; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import java.util.List; -@Service -@RequiredArgsConstructor -public class BookService { +public interface BookService { + List 모든책조회(); - private final BookRepository bookRepository; + BookDto 단일책조회(Long id); - //등록 혹은 수정 - @Transactional - public void 책생성(ReviewRespDto reviewRespDto) { - BookRespDto bookRespDto = reviewRespDto.getBookRespDto(); - Book book = bookRepository.mFindBookIsbn(bookRespDto.getIsbn()); - if (book == null) { - Book newBook = bookRespDto.toEntity(); - bookRepository.save(newBook); - } - } - - //조회 - //특정 유저의 모든 책 목록 조회 - @Transactional - public List findAllBookOneUser(Long socialId) { - List init = bookRepository.mFindBookByUserId(socialId); - if(init.isEmpty()) { - throw new EntityNotFoundException(CustomErrorCode.BOOK_NOT_FOUND); - } - return init.stream().map(book -> new BookDto( - book.getTitle(), - book.getCoverImageUrl(), - book.getIsbn(), - book.getCreateAt())).toList(); - } - - - //책 한권 조회 - @Transactional - public BookDto 단일책조회(Long id) { - Book init = bookRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(CustomErrorCode.BOOK_NOT_FOUND)); - return new BookDto( - init.getTitle(), - init.getCoverImageUrl(), - init.getIsbn(), - init.getCreateAt()); - } - - //등록된 모든 책 조회 - @Transactional - public List 모든책조회() { - List init = bookRepository.findAll(); - - return init.stream().map(book -> new BookDto( - book.getTitle(), - book.getCoverImageUrl(), - book.getIsbn(), - book.getCreateAt())).toList(); - } + List 유저책조회(Long socialId); + void 책생성(ReviewRespDto reviewRespDto); } diff --git a/src/main/java/com/book_everywhere/book/service/BookServiceImpl.java b/src/main/java/com/book_everywhere/book/service/BookServiceImpl.java new file mode 100644 index 0000000..32a6fdd --- /dev/null +++ b/src/main/java/com/book_everywhere/book/service/BookServiceImpl.java @@ -0,0 +1,72 @@ +package com.book_everywhere.book.service; + +import com.book_everywhere.book.entity.Book; +import com.book_everywhere.book.repository.BookRepository; +import com.book_everywhere.book.dto.BookDto; +import com.book_everywhere.book.dto.BookRespDto; +import com.book_everywhere.review.dto.ReviewRespDto; +import com.book_everywhere.exception.customs.CustomErrorCode; +import com.book_everywhere.exception.customs.EntityNotFoundException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +@Transactional +@RequiredArgsConstructor +public class BookServiceImpl implements BookService { + + private final BookRepository bookRepository; + + //등록 혹은 수정 + public void 책생성(ReviewRespDto reviewRespDto) { + BookRespDto bookRespDto = reviewRespDto.getBookRespDto(); + Book book = bookRepository.mFindBookIsbn(bookRespDto.getIsbn()); + if (book == null) { + Book newBook = bookRespDto.toEntity(); + bookRepository.save(newBook); + } + } + + //조회 + //특정 유저의 모든 책 목록 조회 + + public List 유저책조회(Long socialId) { + List init = bookRepository.mFindBookByUserId(socialId); + if (init.isEmpty()) { + throw new EntityNotFoundException(CustomErrorCode.BOOK_NOT_FOUND); + } + return init.stream().map(book -> new BookDto( + book.getTitle(), + book.getCoverImageUrl(), + book.getIsbn(), + book.getCreateAt())).toList(); + } + + + //책 한권 조회 + + public BookDto 단일책조회(Long id) { + Book init = bookRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(CustomErrorCode.BOOK_NOT_FOUND)); + return new BookDto( + init.getTitle(), + init.getCoverImageUrl(), + init.getIsbn(), + init.getCreateAt()); + } + + //등록된 모든 책 조회 + + public List 모든책조회() { + List init = bookRepository.findAll(); + + return init.stream().map(book -> new BookDto( + book.getTitle(), + book.getCoverImageUrl(), + book.getIsbn(), + book.getCreateAt())).toList(); + } + +} diff --git a/src/main/java/com/book_everywhere/pin/service/PinService.java b/src/main/java/com/book_everywhere/pin/service/PinService.java index 0799199..98e2363 100644 --- a/src/main/java/com/book_everywhere/pin/service/PinService.java +++ b/src/main/java/com/book_everywhere/pin/service/PinService.java @@ -1,121 +1,21 @@ package com.book_everywhere.pin.service; -import com.book_everywhere.pin.entity.Pin; -import com.book_everywhere.pin.repository.PinRepository; -import com.book_everywhere.tag.repository.TaggedRepository; import com.book_everywhere.pin.dto.PinDto; -import com.book_everywhere.pin.dto.PinRespDto; import com.book_everywhere.pin.dto.PinWithTagCountRespDto; import com.book_everywhere.review.dto.ReviewRespDto; -import com.book_everywhere.tag.dto.TagCountRespDto; -import com.book_everywhere.exception.customs.CustomErrorCode; -import com.book_everywhere.exception.customs.EntityNotFoundException; -import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import java.util.List; -@RequiredArgsConstructor -@Service -public class PinService { - private final PinRepository pinRepository; - private final TaggedRepository taggedRepository; +public interface PinService { + List 전체지도조회(); - //DTO 변환단계 - @Transactional(readOnly = true) - public List 전체지도조회() { - List init = pinRepository.mFindAllPin(); - List resultDto = init.stream() - .map(pin -> new PinDto(pin.getId(), pin.getPlaceId(), pin.getLatitude(), pin.getLongitude(), pin.getTitle(), pin.getAddress(), pin.getUrl(), pin.getCreateAt())) - .toList(); + void 핀생성(ReviewRespDto reviewRespDto); - return resultDto; - } + List 핀의상위5개태그개수와함께조회(); - @Transactional - public void 핀생성(ReviewRespDto reviewRespDto) { - PinRespDto pinRespDto = reviewRespDto.getPinRespDto(); - Pin pined = pinRepository.mFindPinByAddress(reviewRespDto.getPinRespDto().getAddress()); - if (pined == null) { - Pin pin = pinRespDto.toEntity(); - pinRepository.save(pin); - } - } + List 태그조회(String content); - @Transactional(readOnly = true) - public List 나만의지도조회(Long userId) { - List init = pinRepository.mUserMap(userId); + List 공유또는개인핀의상위5개태그개수와함께조회(boolean isPrivate); - List resultDto = init.stream() - .map(pin -> new PinDto(pin.getId(), pin.getPlaceId(), pin.getLatitude(), pin.getLongitude(), pin.getTitle(), pin.getAddress(), pin.getUrl(), pin.getCreateAt())) - .toList(); - - return resultDto; - } - - @Transactional(readOnly = true) - public List 태그조회(String tagContent) { - List init = pinRepository.mFindTaggedPin(tagContent); - if (init.isEmpty()) { - throw new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND); - } - - List resultDto = init.stream() - .map(pin -> new PinDto(pin.getId(), pin.getPlaceId(), pin.getLatitude(), pin.getLongitude(), pin.getTitle(), pin.getAddress(), pin.getUrl(), pin.getCreateAt())) - .toList(); - - return resultDto; - } - - @Transactional(readOnly = true) - public List 핀의상위5개태그개수와함께조회() { - List pins = pinRepository.findAll(); - return pins.stream().map(pin -> { - PageRequest pageRequest = PageRequest.of(0, 5); - Page taggeds = taggedRepository.mCountByPinId(pin.getId(), pageRequest); - List tagCountRespDtos = taggeds.stream() - .map(tagged -> new TagCountRespDto( - (String) tagged[0], - (Integer) tagged[1])).toList(); - - return new PinWithTagCountRespDto( - pin.getId(), - pin.getPlaceId(), - pin.getLatitude(), - pin.getLongitude(), - pin.getTitle(), - pin.getAddress(), - pin.getUrl(), - pin.getCreateAt(), - tagCountRespDtos - ); - }).toList(); - } - @Transactional(readOnly = true) - public List 공유또는개인핀의상위5개태그개수와함께조회(boolean pinIsPrivate) { - List pins = pinRepository.mFindPinByIsPrivate(pinIsPrivate); - return pins.stream().map(pin -> { - PageRequest pageRequest = PageRequest.of(0, 5); - Page taggeds = taggedRepository.mCountByPinId(pin.getId(),pageRequest); - List tagCountRespDtos = taggeds.stream() - .map(tagged -> new TagCountRespDto( - (String) tagged[0], - (Integer) tagged[1])).toList(); - - return new PinWithTagCountRespDto( - pin.getId(), - pin.getPlaceId(), - pin.getLatitude(), - pin.getLongitude(), - pin.getTitle(), - pin.getAddress(), - pin.getUrl(), - pin.getCreateAt(), - tagCountRespDtos - ); - }).toList(); - } + List 나만의지도조회(Long socialId); } diff --git a/src/main/java/com/book_everywhere/pin/service/PinServiceImpl.java b/src/main/java/com/book_everywhere/pin/service/PinServiceImpl.java new file mode 100644 index 0000000..269df34 --- /dev/null +++ b/src/main/java/com/book_everywhere/pin/service/PinServiceImpl.java @@ -0,0 +1,119 @@ +package com.book_everywhere.pin.service; + +import com.book_everywhere.pin.entity.Pin; +import com.book_everywhere.pin.repository.PinRepository; +import com.book_everywhere.tag.repository.TaggedRepository; +import com.book_everywhere.pin.dto.PinDto; +import com.book_everywhere.pin.dto.PinRespDto; +import com.book_everywhere.pin.dto.PinWithTagCountRespDto; +import com.book_everywhere.review.dto.ReviewRespDto; +import com.book_everywhere.tag.dto.TagCountRespDto; +import com.book_everywhere.exception.customs.CustomErrorCode; +import com.book_everywhere.exception.customs.EntityNotFoundException; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@RequiredArgsConstructor +@Transactional +@Service +public class PinServiceImpl implements PinService { + private final PinRepository pinRepository; + private final TaggedRepository taggedRepository; + + //DTO 변환단계 + public List 전체지도조회() { + List init = pinRepository.mFindAllPin(); + List resultDto = init.stream() + .map(pin -> new PinDto(pin.getId(), pin.getPlaceId(), pin.getLatitude(), pin.getLongitude(), pin.getTitle(), pin.getAddress(), pin.getUrl(), pin.getCreateAt())) + .toList(); + + return resultDto; + } + + public void 핀생성(ReviewRespDto reviewRespDto) { + PinRespDto pinRespDto = reviewRespDto.getPinRespDto(); + Pin pined = pinRepository.mFindPinByAddress(reviewRespDto.getPinRespDto().getAddress()); + if (pined == null) { + Pin pin = pinRespDto.toEntity(); + pinRepository.save(pin); + } + } + + public List 나만의지도조회(Long userId) { + List init = pinRepository.mUserMap(userId); + + List resultDto = init.stream() + .map(pin -> new PinDto(pin.getId(), pin.getPlaceId(), pin.getLatitude(), pin.getLongitude(), pin.getTitle(), pin.getAddress(), pin.getUrl(), pin.getCreateAt())) + .toList(); + + return resultDto; + } + + + public List 태그조회(String tagContent) { + List init = pinRepository.mFindTaggedPin(tagContent); + if (init.isEmpty()) { + throw new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND); + } + + List resultDto = init.stream() + .map(pin -> new PinDto(pin.getId(), pin.getPlaceId(), pin.getLatitude(), pin.getLongitude(), pin.getTitle(), pin.getAddress(), pin.getUrl(), pin.getCreateAt())) + .toList(); + + return resultDto; + } + + + public List 핀의상위5개태그개수와함께조회() { + List pins = pinRepository.findAll(); + return pins.stream().map(pin -> { + PageRequest pageRequest = PageRequest.of(0, 5); + Page taggeds = taggedRepository.mCountByPinId(pin.getId(), pageRequest); + List tagCountRespDtos = taggeds.stream() + .map(tagged -> new TagCountRespDto( + (String) tagged[0], + (Integer) tagged[1])).toList(); + + return new PinWithTagCountRespDto( + pin.getId(), + pin.getPlaceId(), + pin.getLatitude(), + pin.getLongitude(), + pin.getTitle(), + pin.getAddress(), + pin.getUrl(), + pin.getCreateAt(), + tagCountRespDtos + ); + }).toList(); + } + + public List 공유또는개인핀의상위5개태그개수와함께조회(boolean pinIsPrivate) { + List pins = pinRepository.mFindPinByIsPrivate(pinIsPrivate); + return pins.stream().map(pin -> { + PageRequest pageRequest = PageRequest.of(0, 5); + Page taggeds = taggedRepository.mCountByPinId(pin.getId(), pageRequest); + List tagCountRespDtos = taggeds.stream() + .map(tagged -> new TagCountRespDto( + (String) tagged[0], + (Integer) tagged[1])).toList(); + + return new PinWithTagCountRespDto( + pin.getId(), + pin.getPlaceId(), + pin.getLatitude(), + pin.getLongitude(), + pin.getTitle(), + pin.getAddress(), + pin.getUrl(), + pin.getCreateAt(), + tagCountRespDtos + ); + }).toList(); + } +} diff --git a/src/main/java/com/book_everywhere/pin/service/VisitService.java b/src/main/java/com/book_everywhere/pin/service/VisitService.java index 8bbb113..130038c 100644 --- a/src/main/java/com/book_everywhere/pin/service/VisitService.java +++ b/src/main/java/com/book_everywhere/pin/service/VisitService.java @@ -1,45 +1,7 @@ package com.book_everywhere.pin.service; -import com.book_everywhere.pin.entity.Pin; -import com.book_everywhere.pin.repository.PinRepository; -import com.book_everywhere.pin.repository.VisitRepository; -import com.book_everywhere.auth.entity.User; -import com.book_everywhere.auth.repository.UserRepository; -import com.book_everywhere.pin.entity.Visit; import com.book_everywhere.review.dto.ReviewRespDto; -import com.book_everywhere.exception.customs.CustomErrorCode; -import com.book_everywhere.exception.customs.EntityNotFoundException; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -@RequiredArgsConstructor -@Service -public class VisitService { - private final UserRepository userRepository; - private final PinRepository pinRepository; - private final VisitRepository visitRepository; - - @Transactional - public void 독후감쓰기전방문등록또는수정(ReviewRespDto reviewRespDto) { - //review가 올라가기전 visit에 등록되어있는지 확인후 없다면 visit등록 - User user = userRepository.findBySocialId(reviewRespDto.getSocialId()).orElseThrow( - () -> new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND) - ); - Pin pin = pinRepository.mFindPinByAddress(reviewRespDto.getPinRespDto().getAddress()); - - Visit visited = visitRepository.mFindByUserAndPin(user.getId(), pin.getId()); - - if (visited == null) { - Visit visit = Visit.builder() - .user(user) - .pin(pin) - .isPinPrivate(reviewRespDto.getPinRespDto().isPrivate()) - .build(); - - visitRepository.save(visit); - } else { - visited.changeVisit(user, pin, reviewRespDto.getPinRespDto().isPrivate()); - } - } +public interface VisitService { + void 독후감쓰기전방문등록또는수정(ReviewRespDto reviewRespDto); } diff --git a/src/main/java/com/book_everywhere/pin/service/VisitServiceImpl.java b/src/main/java/com/book_everywhere/pin/service/VisitServiceImpl.java new file mode 100644 index 0000000..691a633 --- /dev/null +++ b/src/main/java/com/book_everywhere/pin/service/VisitServiceImpl.java @@ -0,0 +1,46 @@ +package com.book_everywhere.pin.service; + +import com.book_everywhere.pin.entity.Pin; +import com.book_everywhere.pin.repository.PinRepository; +import com.book_everywhere.pin.repository.VisitRepository; +import com.book_everywhere.auth.entity.User; +import com.book_everywhere.auth.repository.UserRepository; +import com.book_everywhere.pin.entity.Visit; +import com.book_everywhere.review.dto.ReviewRespDto; +import com.book_everywhere.exception.customs.CustomErrorCode; +import com.book_everywhere.exception.customs.EntityNotFoundException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@RequiredArgsConstructor +@Transactional +@Service +public class VisitServiceImpl implements VisitService{ + private final UserRepository userRepository; + private final PinRepository pinRepository; + private final VisitRepository visitRepository; + + + public void 독후감쓰기전방문등록또는수정(ReviewRespDto reviewRespDto) { + //review가 올라가기전 visit에 등록되어있는지 확인후 없다면 visit등록 + User user = userRepository.findBySocialId(reviewRespDto.getSocialId()).orElseThrow( + () -> new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND) + ); + Pin pin = pinRepository.mFindPinByAddress(reviewRespDto.getPinRespDto().getAddress()); + + Visit visited = visitRepository.mFindByUserAndPin(user.getId(), pin.getId()); + + if (visited == null) { + Visit visit = Visit.builder() + .user(user) + .pin(pin) + .isPinPrivate(reviewRespDto.getPinRespDto().isPrivate()) + .build(); + + visitRepository.save(visit); + } else { + visited.changeVisit(user, pin, reviewRespDto.getPinRespDto().isPrivate()); + } + } +} diff --git a/src/main/java/com/book_everywhere/review/service/ReviewService.java b/src/main/java/com/book_everywhere/review/service/ReviewService.java index c8fc5d4..145b9b7 100644 --- a/src/main/java/com/book_everywhere/review/service/ReviewService.java +++ b/src/main/java/com/book_everywhere/review/service/ReviewService.java @@ -1,217 +1,33 @@ package com.book_everywhere.review.service; -import com.book_everywhere.book.entity.Book; -import com.book_everywhere.book.repository.BookRepository; -import com.book_everywhere.pin.entity.Pin; -import com.book_everywhere.pin.repository.PinRepository; -import com.book_everywhere.review.entity.Review; -import com.book_everywhere.review.repository.ReviewRepository; -import com.book_everywhere.auth.entity.User; -import com.book_everywhere.auth.repository.UserRepository; -import com.book_everywhere.tag.service.TaggedService; -import com.book_everywhere.exception.customs.CustomErrorCode; -import com.book_everywhere.exception.customs.EntityNotFoundException; -import com.book_everywhere.review.dto.ReviewRespDto; -import com.book_everywhere.exception.customs.PropertyBadRequestException; import com.book_everywhere.review.dto.ReviewDto; -import lombok.RequiredArgsConstructor; -import org.springframework.security.core.annotation.AuthenticationPrincipal; +import com.book_everywhere.review.dto.ReviewRespDto; import org.springframework.security.oauth2.core.user.OAuth2User; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import java.util.List; -@RequiredArgsConstructor -@Service -public class ReviewService { - private final ReviewRepository reviewRepository; - private final BookRepository bookRepository; - private final PinRepository pinRepository; - private final UserRepository userRepository; - private final TaggedService taggedService; - - - //사용자 검증에 메소드 - //등록 - @Transactional - public Long 독후감생성하기(ReviewRespDto reviewRespDto) { - User user = userRepository.findBySocialId(reviewRespDto.getSocialId()).orElseThrow( - () -> new EntityNotFoundException(CustomErrorCode.USER_NOT_FOUND)); - Book book = bookRepository.mFindBookIsbn(reviewRespDto.getBookRespDto().getIsbn()); - if (book == null) { - throw new EntityNotFoundException(CustomErrorCode.BOOK_NOT_FOUND); - } - - Pin pin = pinRepository.mFindPinByAddress(reviewRespDto.getPinRespDto().getAddress()); - if (pin == null) { - throw new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND); - } - Review review = Review.builder() - .book(book) - .pin(pin) - .user(user) - .title(reviewRespDto.getTitle()) - .writer(reviewRespDto.getWriter()) - .content(reviewRespDto.getContent()) - .isPrivate(reviewRespDto.isPrivate()) - .isBookComplete(reviewRespDto.getBookRespDto().isComplete()) - .build(); - reviewRepository.save(review); - return review.getId(); - } - - //책에따른 모든 리뷰기능이 추가되었습니다. - @Transactional - public List 책에따른모든리뷰(Long bookId) { - List init = reviewRepository.mFindReviewsByBook(bookId); - if (init.isEmpty()) { - throw new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND); - } - - List resultDto = init.stream() - .map(review -> new ReviewDto(review.getId(), review.getWriter(), review.getTitle(), review.getContent(), review.isPrivate(), review.getUpdateAt(), review.getCreateAt())) - .toList(); - - return resultDto; - } - - - //단일 핀을 눌렀을때 독후감이 조회됩니다. - @Transactional(readOnly = true) - public List 단일핀독후감조회(Long pinId, @AuthenticationPrincipal OAuth2User oAuth2User) { - List init = reviewRepository.mFindReviewUserMap((Long) oAuth2User.getAttributes().get("id"), pinId); - if (init.isEmpty()) { - throw new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND); - } - - List resultDto = init.stream() - .map(review -> new ReviewDto(review.getId(), review.getWriter(), review.getTitle(), review.getContent(), review.isPrivate(), review.getUpdateAt(), review.getCreateAt())) - .toList(); - - return resultDto; - } +public interface ReviewService { + Long 독후감생성하기(ReviewRespDto reviewRespDto); - @Transactional(readOnly = true) - public List 유저모든독후감조회(@AuthenticationPrincipal OAuth2User oAuth2User) { - List init = reviewRepository.mFindReviewsByUser((Long) oAuth2User.getAttributes().get("id")); + List 책에따른모든리뷰(Long bookId); - List resultDto = init.stream() - .map(review -> new ReviewDto(review.getId(), review.getWriter(), review.getTitle(), review.getContent(), review.isPrivate(), review.getUpdateAt(), review.getCreateAt())) - .toList(); + List 모든독후감조회(); - return resultDto; - } + void 등록또는수정전예외처리(ReviewRespDto reviewRespDto); + ReviewDto 단일독후감조회(Long reviewId); - @Transactional(readOnly = true) - //리뷰 하나만 조회 - public ReviewDto 단일독후감조회(Long id) { - Review init = reviewRepository.findById(id).orElseThrow( - () -> new EntityNotFoundException(CustomErrorCode.REVIEW_NOT_FOUND)); - return new ReviewDto( - init.getId(), - init.getWriter(), - init.getTitle(), - init.getContent(), - init.isPrivate(), - init.getCreateAt(), - init.getUpdateAt()); - } + void 독후감수정(Long reviewId, ReviewRespDto reviewRespDto); - @Transactional(readOnly = true) - //등록된 모든 리뷰 조회 - public List 모든독후감조회() { - List init = reviewRepository.findAll(); - return init.stream().map(review -> - new ReviewDto( - review.getId(), - review.getWriter(), - review.getTitle(), - review.getContent(), - review.isPrivate(), - review.getCreateAt(), - review.getUpdateAt())).toList(); - } + void 유저독후감개수검증후책삭제(String prevBookTitle); - @Transactional(readOnly = true) - public List 모든공유독후감조회() { - List init = reviewRepository.findByIsPrivateOrderByCreateAtDesc(false); - return init.stream().map(review -> - new ReviewDto( - review.getId(), - review.getWriter(), - review.getTitle(), - review.getContent(), - review.isPrivate(), - review.getCreateAt(), - review.getUpdateAt())).toList(); - } + void 독후감개수검증후핀삭제(String prevAddress, Long socialId); - @Transactional - public void 독후감수정(Long reviewId, ReviewRespDto reviewRespDto) { - Review review = reviewRepository.findById(reviewId) - .orElseThrow(() -> new EntityNotFoundException(CustomErrorCode.REVIEW_NOT_FOUND)); - Book existBook = bookRepository.mFindBookIsbn(reviewRespDto.getBookRespDto().getIsbn()); - Pin existPin = pinRepository.mFindPinByAddress(reviewRespDto.getPinRespDto().getAddress()); - User user = userRepository.findBySocialId(reviewRespDto.getSocialId()).orElseThrow(() -> new EntityNotFoundException(CustomErrorCode.USER_NOT_FOUND)); - review.changeReview(reviewRespDto.getTitle(),reviewRespDto.getContent(),reviewRespDto.isPrivate(),reviewRespDto.getBookRespDto().isComplete(),reviewRespDto.getWriter()); - review.setUser(user); - review.setBook(existBook); - review.setPin(existPin); - reviewRepository.save(review); - } - @Transactional - public void 유저독후감개수검증후책삭제(String title) { - Book book = bookRepository.mFindBookTitle(title); - if(book == null) { - throw new EntityNotFoundException(CustomErrorCode.BOOK_NOT_FOUND); - } - List reviews = reviewRepository.mFindReviewsByBook(book.getId()); - if(reviews.isEmpty()) { - bookRepository.delete(book); - } - } + List 모든공유독후감조회(); - @Transactional - public void 독후감개수검증후핀삭제(String address, Long socialId) { - Pin pin = pinRepository.mFindPinByAddress(address); - if(pin == null) { - throw new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND); - } - List reviews = reviewRepository.mFindReviewsByPin(pin.getId()); - if(reviews.isEmpty()) { - if(!pin.getTags().isEmpty()) { - taggedService.태그삭제(address, socialId); - } - pinRepository.delete(pin); - } - } + void 독후감삭제(Long reviewId); - @Transactional - public void 독후감삭제(Long reviewId) { - Review review = reviewRepository.findById(reviewId).orElseThrow(() -> new EntityNotFoundException(CustomErrorCode.REVIEW_NOT_FOUND)); - reviewRepository.delete(review); - } + List 유저모든독후감조회(OAuth2User oAuth2User); - public void 등록또는수정전예외처리(ReviewRespDto reviewRespDto) { - if (reviewRespDto.getTitle() == null || reviewRespDto.getTitle().isEmpty()) { - throw new PropertyBadRequestException(CustomErrorCode.TITLE_IS_NOT_BLANK); - } - if (reviewRespDto.getTitle().length() > 20) { - throw new PropertyBadRequestException(CustomErrorCode.TITLE_IS_NOT_OVER_20); - } - if (reviewRespDto.getContent() == null || reviewRespDto.getContent().isEmpty()) { - throw new PropertyBadRequestException(CustomErrorCode.CONTENT_IS_NOT_BLANK); - } - if (reviewRespDto.getContent().length() > 1500) { - throw new PropertyBadRequestException(CustomErrorCode.CONTENT_IS_NOT_OVER_1500); - } - if (reviewRespDto.getBookRespDto() == null) { - throw new PropertyBadRequestException(CustomErrorCode.BOOK_IS_NOT_NULL); - } - if (reviewRespDto.getPinRespDto().getAddress() == null || reviewRespDto.getPinRespDto().getAddress().isEmpty()) { - throw new PropertyBadRequestException(CustomErrorCode.ADDRESS_IS_NOT_NULL); - } - } -} \ No newline at end of file + List 단일핀독후감조회(Long pinId, OAuth2User oAuth2User); +} diff --git a/src/main/java/com/book_everywhere/review/service/ReviewServiceImpl.java b/src/main/java/com/book_everywhere/review/service/ReviewServiceImpl.java new file mode 100644 index 0000000..90d9590 --- /dev/null +++ b/src/main/java/com/book_everywhere/review/service/ReviewServiceImpl.java @@ -0,0 +1,215 @@ +package com.book_everywhere.review.service; + +import com.book_everywhere.book.entity.Book; +import com.book_everywhere.book.repository.BookRepository; +import com.book_everywhere.pin.entity.Pin; +import com.book_everywhere.pin.repository.PinRepository; +import com.book_everywhere.review.entity.Review; +import com.book_everywhere.review.repository.ReviewRepository; +import com.book_everywhere.auth.entity.User; +import com.book_everywhere.auth.repository.UserRepository; +import com.book_everywhere.tag.service.TaggedService; +import com.book_everywhere.exception.customs.CustomErrorCode; +import com.book_everywhere.exception.customs.EntityNotFoundException; +import com.book_everywhere.review.dto.ReviewRespDto; +import com.book_everywhere.exception.customs.PropertyBadRequestException; +import com.book_everywhere.review.dto.ReviewDto; +import lombok.RequiredArgsConstructor; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.security.oauth2.core.user.OAuth2User; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@RequiredArgsConstructor +@Transactional +@Service +public class ReviewServiceImpl implements ReviewService { + private final ReviewRepository reviewRepository; + private final BookRepository bookRepository; + private final PinRepository pinRepository; + private final UserRepository userRepository; + private final TaggedService taggedService; + + + //사용자 검증에 메소드 + //등록 + + public Long 독후감생성하기(ReviewRespDto reviewRespDto) { + User user = userRepository.findBySocialId(reviewRespDto.getSocialId()).orElseThrow( + () -> new EntityNotFoundException(CustomErrorCode.USER_NOT_FOUND)); + Book book = bookRepository.mFindBookIsbn(reviewRespDto.getBookRespDto().getIsbn()); + if (book == null) { + throw new EntityNotFoundException(CustomErrorCode.BOOK_NOT_FOUND); + } + + Pin pin = pinRepository.mFindPinByAddress(reviewRespDto.getPinRespDto().getAddress()); + if (pin == null) { + throw new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND); + } + Review review = Review.builder() + .book(book) + .pin(pin) + .user(user) + .title(reviewRespDto.getTitle()) + .writer(reviewRespDto.getWriter()) + .content(reviewRespDto.getContent()) + .isPrivate(reviewRespDto.isPrivate()) + .isBookComplete(reviewRespDto.getBookRespDto().isComplete()) + .build(); + reviewRepository.save(review); + return review.getId(); + } + + //책에따른 모든 리뷰기능이 추가되었습니다. + public List 책에따른모든리뷰(Long bookId) { + List init = reviewRepository.mFindReviewsByBook(bookId); + if (init.isEmpty()) { + throw new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND); + } + + List resultDto = init.stream() + .map(review -> new ReviewDto(review.getId(), review.getWriter(), review.getTitle(), review.getContent(), review.isPrivate(), review.getUpdateAt(), review.getCreateAt())) + .toList(); + + return resultDto; + } + + + //단일 핀을 눌렀을때 독후감이 조회됩니다. + public List 단일핀독후감조회(Long pinId, @AuthenticationPrincipal OAuth2User oAuth2User) { + List init = reviewRepository.mFindReviewUserMap((Long) oAuth2User.getAttributes().get("id"), pinId); + if (init.isEmpty()) { + throw new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND); + } + + List resultDto = init.stream() + .map(review -> new ReviewDto(review.getId(), review.getWriter(), review.getTitle(), review.getContent(), review.isPrivate(), review.getUpdateAt(), review.getCreateAt())) + .toList(); + + return resultDto; + } + + + public List 유저모든독후감조회(@AuthenticationPrincipal OAuth2User oAuth2User) { + List init = reviewRepository.mFindReviewsByUser((Long) oAuth2User.getAttributes().get("id")); + + List resultDto = init.stream() + .map(review -> new ReviewDto(review.getId(), review.getWriter(), review.getTitle(), review.getContent(), review.isPrivate(), review.getUpdateAt(), review.getCreateAt())) + .toList(); + + return resultDto; + } + + + //리뷰 하나만 조회 + public ReviewDto 단일독후감조회(Long id) { + Review init = reviewRepository.findById(id).orElseThrow( + () -> new EntityNotFoundException(CustomErrorCode.REVIEW_NOT_FOUND)); + return new ReviewDto( + init.getId(), + init.getWriter(), + init.getTitle(), + init.getContent(), + init.isPrivate(), + init.getCreateAt(), + init.getUpdateAt()); + } + + + //등록된 모든 리뷰 조회 + public List 모든독후감조회() { + List init = reviewRepository.findAll(); + return init.stream().map(review -> + new ReviewDto( + review.getId(), + review.getWriter(), + review.getTitle(), + review.getContent(), + review.isPrivate(), + review.getCreateAt(), + review.getUpdateAt())).toList(); + } + + + public List 모든공유독후감조회() { + List init = reviewRepository.findByIsPrivateOrderByCreateAtDesc(false); + return init.stream().map(review -> + new ReviewDto( + review.getId(), + review.getWriter(), + review.getTitle(), + review.getContent(), + review.isPrivate(), + review.getCreateAt(), + review.getUpdateAt())).toList(); + } + + + public void 독후감수정(Long reviewId, ReviewRespDto reviewRespDto) { + Review review = reviewRepository.findById(reviewId) + .orElseThrow(() -> new EntityNotFoundException(CustomErrorCode.REVIEW_NOT_FOUND)); + Book existBook = bookRepository.mFindBookIsbn(reviewRespDto.getBookRespDto().getIsbn()); + Pin existPin = pinRepository.mFindPinByAddress(reviewRespDto.getPinRespDto().getAddress()); + User user = userRepository.findBySocialId(reviewRespDto.getSocialId()).orElseThrow(() -> new EntityNotFoundException(CustomErrorCode.USER_NOT_FOUND)); + review.changeReview(reviewRespDto.getTitle(), reviewRespDto.getContent(), reviewRespDto.isPrivate(), reviewRespDto.getBookRespDto().isComplete(), reviewRespDto.getWriter()); + review.setUser(user); + review.setBook(existBook); + review.setPin(existPin); + reviewRepository.save(review); + } + + + public void 유저독후감개수검증후책삭제(String title) { + Book book = bookRepository.mFindBookTitle(title); + if (book == null) { + throw new EntityNotFoundException(CustomErrorCode.BOOK_NOT_FOUND); + } + List reviews = reviewRepository.mFindReviewsByBook(book.getId()); + if (reviews.isEmpty()) { + bookRepository.delete(book); + } + } + + public void 독후감개수검증후핀삭제(String address, Long socialId) { + Pin pin = pinRepository.mFindPinByAddress(address); + if (pin == null) { + throw new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND); + } + List reviews = reviewRepository.mFindReviewsByPin(pin.getId()); + if (reviews.isEmpty()) { + if (!pin.getTags().isEmpty()) { + taggedService.태그삭제(address, socialId); + } + pinRepository.delete(pin); + } + } + + + public void 독후감삭제(Long reviewId) { + Review review = reviewRepository.findById(reviewId).orElseThrow(() -> new EntityNotFoundException(CustomErrorCode.REVIEW_NOT_FOUND)); + reviewRepository.delete(review); + } + + public void 등록또는수정전예외처리(ReviewRespDto reviewRespDto) { + if (reviewRespDto.getTitle() == null || reviewRespDto.getTitle().isEmpty()) { + throw new PropertyBadRequestException(CustomErrorCode.TITLE_IS_NOT_BLANK); + } + if (reviewRespDto.getTitle().length() > 20) { + throw new PropertyBadRequestException(CustomErrorCode.TITLE_IS_NOT_OVER_20); + } + if (reviewRespDto.getContent() == null || reviewRespDto.getContent().isEmpty()) { + throw new PropertyBadRequestException(CustomErrorCode.CONTENT_IS_NOT_BLANK); + } + if (reviewRespDto.getContent().length() > 1500) { + throw new PropertyBadRequestException(CustomErrorCode.CONTENT_IS_NOT_OVER_1500); + } + if (reviewRespDto.getBookRespDto() == null) { + throw new PropertyBadRequestException(CustomErrorCode.BOOK_IS_NOT_NULL); + } + if (reviewRespDto.getPinRespDto().getAddress() == null || reviewRespDto.getPinRespDto().getAddress().isEmpty()) { + throw new PropertyBadRequestException(CustomErrorCode.ADDRESS_IS_NOT_NULL); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/book_everywhere/tag/service/TaggedService.java b/src/main/java/com/book_everywhere/tag/service/TaggedService.java index a1cb4ab..907dd5c 100644 --- a/src/main/java/com/book_everywhere/tag/service/TaggedService.java +++ b/src/main/java/com/book_everywhere/tag/service/TaggedService.java @@ -1,70 +1,12 @@ package com.book_everywhere.tag.service; -import com.book_everywhere.pin.entity.Pin; -import com.book_everywhere.pin.repository.PinRepository; -import com.book_everywhere.tag.entity.Tag; -import com.book_everywhere.tag.repository.TagRepository; -import com.book_everywhere.tag.entity.Tagged; -import com.book_everywhere.tag.repository.TaggedRepository; -import com.book_everywhere.auth.entity.User; -import com.book_everywhere.auth.repository.UserRepository; import com.book_everywhere.review.dto.ReviewRespDto; import com.book_everywhere.tag.dto.TagDto; -import com.book_everywhere.exception.customs.CustomErrorCode; -import com.book_everywhere.exception.customs.EntityNotFoundException; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import java.util.List; -@RequiredArgsConstructor -@Service -public class TaggedService { - private final TagRepository tagRepository; - private final TaggedRepository taggedRepository; - private final PinRepository pinRepository; - private final UserRepository userRepository; - - @Transactional - public void 태그등록(ReviewRespDto reviewRespDto) { - List tags = reviewRespDto.getTags(); - User user = userRepository.findBySocialId(reviewRespDto.getSocialId()).orElseThrow(() -> new EntityNotFoundException(CustomErrorCode.USER_NOT_FOUND)); - - if(tags.isEmpty()) { - return; - } - for (String tagResp : tags) { - Tag tag = tagRepository.mFindTagByName(tagResp); - Pin pin = pinRepository.mFindPinByAddress(reviewRespDto.getPinRespDto().getAddress()); - - Tagged tagged = taggedRepository.mFindTagged(tag.getId(), pin.getId()); - if (tagged == null) { - Tagged newTagged = Tagged.builder() - .pin(pin) - .tag(tag) - .user(user) - .build(); - taggedRepository.save(newTagged); - } - } - } - - @Transactional(readOnly = true) - public List 모든태그조회() { - List tags = tagRepository.findAll(); - return tags.stream().map(tag -> new TagDto( - tag.getContent(), - false, - tag.getCategory().getName())).toList(); - } - - @Transactional - public void 태그삭제(String address, Long socialId) { - Pin pin = pinRepository.mFindPinByAddress(address); - if(pin == null) { - throw new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND); - } - taggedRepository.deleteAllByPinAndUser(pin.getId(), socialId); - } +public interface TaggedService { + void 태그등록(ReviewRespDto reviewRespDto); + List 모든태그조회(); + void 태그삭제(String address, Long socialId); } diff --git a/src/main/java/com/book_everywhere/tag/service/TaggedServiceImpl.java b/src/main/java/com/book_everywhere/tag/service/TaggedServiceImpl.java new file mode 100644 index 0000000..9fb1c4a --- /dev/null +++ b/src/main/java/com/book_everywhere/tag/service/TaggedServiceImpl.java @@ -0,0 +1,71 @@ +package com.book_everywhere.tag.service; + +import com.book_everywhere.pin.entity.Pin; +import com.book_everywhere.pin.repository.PinRepository; +import com.book_everywhere.tag.entity.Tag; +import com.book_everywhere.tag.repository.TagRepository; +import com.book_everywhere.tag.entity.Tagged; +import com.book_everywhere.tag.repository.TaggedRepository; +import com.book_everywhere.auth.entity.User; +import com.book_everywhere.auth.repository.UserRepository; +import com.book_everywhere.review.dto.ReviewRespDto; +import com.book_everywhere.tag.dto.TagDto; +import com.book_everywhere.exception.customs.CustomErrorCode; +import com.book_everywhere.exception.customs.EntityNotFoundException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@RequiredArgsConstructor +@Transactional +@Service +public class TaggedServiceImpl implements TaggedService { + private final TagRepository tagRepository; + private final TaggedRepository taggedRepository; + private final PinRepository pinRepository; + private final UserRepository userRepository; + + @Override + public void 태그등록(ReviewRespDto reviewRespDto) { + List tags = reviewRespDto.getTags(); + User user = userRepository.findBySocialId(reviewRespDto.getSocialId()).orElseThrow(() -> new EntityNotFoundException(CustomErrorCode.USER_NOT_FOUND)); + + if (tags.isEmpty()) { + return; + } + for (String tagResp : tags) { + Tag tag = tagRepository.mFindTagByName(tagResp); + Pin pin = pinRepository.mFindPinByAddress(reviewRespDto.getPinRespDto().getAddress()); + + Tagged tagged = taggedRepository.mFindTagged(tag.getId(), pin.getId()); + if (tagged == null) { + Tagged newTagged = Tagged.builder() + .pin(pin) + .tag(tag) + .user(user) + .build(); + taggedRepository.save(newTagged); + } + } + } + + @Override + public List 모든태그조회() { + List tags = tagRepository.findAll(); + return tags.stream().map(tag -> new TagDto( + tag.getContent(), + false, + tag.getCategory().getName())).toList(); + } + + @Override + public void 태그삭제(String address, Long socialId) { + Pin pin = pinRepository.mFindPinByAddress(address); + if (pin == null) { + throw new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND); + } + taggedRepository.deleteAllByPinAndUser(pin.getId(), socialId); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a91ef0a..8cb306c 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -14,6 +14,7 @@ spring: config: activate: on-profile: local + security: oauth2: client: