Skip to content

Commit

Permalink
refator : 느슨한 결합 , Service & Impl
Browse files Browse the repository at this point in the history
  • Loading branch information
dltjdgh0428 committed Mar 22, 2024
1 parent 07cb538 commit 042cb80
Show file tree
Hide file tree
Showing 12 changed files with 558 additions and 470 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public CMRespDto<?> findOneBook(@PathVariable Long id) {
//유저 책 조회
@GetMapping("/api/mypage/{socialId}")
public CMRespDto<?> findOneBookWithUser(@PathVariable Long socialId) {
List<BookDto> result = bookService.findAllBookOneUser(socialId);
List<BookDto> result = bookService.유저책조회(socialId);
return new CMRespDto<>(HttpStatus.OK, result, "유저의 책 조회 완료");
}
}
66 changes: 5 additions & 61 deletions src/main/java/com/book_everywhere/book/service/BookService.java
Original file line number Diff line number Diff line change
@@ -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<BookDto> 모든책조회();

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<BookDto> findAllBookOneUser(Long socialId) {
List<Book> 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<BookDto> 모든책조회() {
List<Book> init = bookRepository.findAll();

return init.stream().map(book -> new BookDto(
book.getTitle(),
book.getCoverImageUrl(),
book.getIsbn(),
book.getCreateAt())).toList();
}
List<BookDto> 유저책조회(Long socialId);

void 책생성(ReviewRespDto reviewRespDto);
}
Original file line number Diff line number Diff line change
@@ -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<BookDto> 유저책조회(Long socialId) {
List<Book> 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<BookDto> 모든책조회() {
List<Book> init = bookRepository.findAll();

return init.stream().map(book -> new BookDto(
book.getTitle(),
book.getCoverImageUrl(),
book.getIsbn(),
book.getCreateAt())).toList();
}

}
114 changes: 7 additions & 107 deletions src/main/java/com/book_everywhere/pin/service/PinService.java
Original file line number Diff line number Diff line change
@@ -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<PinDto> 전체지도조회();

//DTO 변환단계
@Transactional(readOnly = true)
public List<PinDto> 전체지도조회() {
List<Pin> init = pinRepository.mFindAllPin();
List<PinDto> 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<PinWithTagCountRespDto> 핀의상위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<PinDto> 태그조회(String content);

@Transactional(readOnly = true)
public List<PinDto> 나만의지도조회(Long userId) {
List<Pin> init = pinRepository.mUserMap(userId);
List<PinWithTagCountRespDto> 공유또는개인핀의상위5개태그개수와함께조회(boolean isPrivate);

List<PinDto> 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<PinDto> 태그조회(String tagContent) {
List<Pin> init = pinRepository.mFindTaggedPin(tagContent);
if (init.isEmpty()) {
throw new EntityNotFoundException(CustomErrorCode.PIN_NOT_FOUND);
}

List<PinDto> 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<PinWithTagCountRespDto> 핀의상위5개태그개수와함께조회() {
List<Pin> pins = pinRepository.findAll();
return pins.stream().map(pin -> {
PageRequest pageRequest = PageRequest.of(0, 5);
Page<Object[]> taggeds = taggedRepository.mCountByPinId(pin.getId(), pageRequest);
List<TagCountRespDto> 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<PinWithTagCountRespDto> 공유또는개인핀의상위5개태그개수와함께조회(boolean pinIsPrivate) {
List<Pin> pins = pinRepository.mFindPinByIsPrivate(pinIsPrivate);
return pins.stream().map(pin -> {
PageRequest pageRequest = PageRequest.of(0, 5);
Page<Object[]> taggeds = taggedRepository.mCountByPinId(pin.getId(),pageRequest);
List<TagCountRespDto> 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<PinDto> 나만의지도조회(Long socialId);
}
Loading

0 comments on commit 042cb80

Please sign in to comment.