Skip to content

Commit

Permalink
[BOOK-25]-독후감 목록 조회 (#14)
Browse files Browse the repository at this point in the history
* [BOOK-25]-feature: 독후감 목록 조회

* [BOOK-25]-refactor: 컨트롤러에서 반환 schema 오류 수정

* [BOOK-25]-refactor: 커멘트 수정 사항 반영

* [BOOK-25]-refactor: 커멘트 수정 사항 반영
  • Loading branch information
sunny2you authored Oct 11, 2024
1 parent 0372fe7 commit b83b512
Show file tree
Hide file tree
Showing 18 changed files with 242 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
import java.util.Optional;

import goorm.unit.booklog.domain.book.domain.Book;
import goorm.unit.booklog.domain.book.presentation.response.UserBookListResponse;
import goorm.unit.booklog.domain.file.domain.File;
import goorm.unit.booklog.domain.file.infrastructure.FileRepository;
import goorm.unit.booklog.domain.review.domain.ReviewRepository;
import goorm.unit.booklog.domain.review.domain.Review;
import goorm.unit.booklog.domain.user.application.UserService;
import goorm.unit.booklog.domain.user.domain.User;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

Expand All @@ -27,6 +32,8 @@
@RequiredArgsConstructor
public class BookService {
private final BookRepository bookRepository;
private final UserService userService;
private final ReviewRepository reviewRepository;

@Value("${naver.api.clientId}")
private String clientId;
Expand Down Expand Up @@ -89,4 +96,19 @@ public Book getBookById(Long id) {
return bookRepository.findById(id).orElse(null);
}

@Transactional
public UserBookListResponse getMyBookList() {
User user=userService.me();
List<Book> books=user.getBooks();
List<Review> reviews=user.getReviews();

List<BookResponse> bookResponses = new ArrayList<>();

for (Book book : books) {
BookResponse bookResponse = BookResponse.from(book);
bookResponses.add(bookResponse);
}
return UserBookListResponse.of(user.getId(), user.getName(), books.size(), reviews.size(),bookResponses);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package goorm.unit.booklog.domain.book.domain;

import goorm.unit.booklog.domain.user.domain.User;

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

public interface BookRepository {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
package goorm.unit.booklog.domain.book.infrastructure;

import goorm.unit.booklog.domain.book.domain.Book;
import goorm.unit.booklog.domain.book.domain.BookRepository;
import goorm.unit.booklog.domain.user.domain.User;
import org.springframework.stereotype.Repository;

import goorm.unit.booklog.domain.book.domain.BookRepository;
import lombok.RequiredArgsConstructor;

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

@Repository
@RequiredArgsConstructor
public class BookRepositoryImpl implements BookRepository {
private final JpaBookRepository jpaBookRepository;
public class BookRepositoryImpl implements BookRepository{
private final JpaBookRepository jpaBookRepository;
@Override
public Book save(Book book) {
return jpaBookRepository.save(book);
}

@Override
public Book save(Book book) {
return jpaBookRepository.save(book);
}
@Override
public Optional<Book> findById(Long id) {
return jpaBookRepository.findById(id);
}

@Override
public Optional<Book> findById(Long id) {
return jpaBookRepository.findById(id);
}
@Override
public Optional<Book> findByTitleAndAuthor(String title, String author) {
return jpaBookRepository.findByTitleAndAuthor(title, author);
}

@Override
public Optional<Book> findByTitleAndAuthor(String title, String author){
return jpaBookRepository.findByTitleAndAuthor(title,author);
};
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package goorm.unit.booklog.domain.book.infrastructure;

import org.springframework.data.jpa.repository.JpaRepository;

import goorm.unit.booklog.domain.book.domain.Book;
import java.util.Optional;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package goorm.unit.booklog.domain.book.presentation;

import goorm.unit.booklog.domain.book.presentation.response.UserBookListResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -42,4 +43,19 @@ public ResponseEntity<BookPageResponse> searchBooks(
BookPageResponse response = bookService.searchBooks(page, size, keyword);
return ResponseEntity.ok(response);
}

@Operation(summary = "유저 활동 내역 조회", description = " 작성한 독후감의 수와 읽은 책의 수, 표지, 제목을 반환합니다.")
@ApiResponses({
@ApiResponse(
responseCode = "200",
description = "유저 활동 내역 조회 성공",
content = @Content(schema = @Schema(implementation = BookPageResponse.class))

)
})
@GetMapping("/me")
public ResponseEntity<UserBookListResponse> getMyBookList() {
UserBookListResponse response = bookService.getMyBookList();
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package goorm.unit.booklog.domain.book.presentation.exception;

import goorm.unit.booklog.common.exception.ExceptionCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.NOT_FOUND;

@Getter
@AllArgsConstructor
public enum BookExceptionCode implements ExceptionCode {
BOOK_NOT_FOUND(NOT_FOUND, "해당 도서가 존재하지 않습니다."),;

private final HttpStatus status;
private final String message;

@Override
public String getCode() {
return this.name();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package goorm.unit.booklog.domain.book.presentation.exception;

import goorm.unit.booklog.common.exception.CustomException;

import static goorm.unit.booklog.domain.book.presentation.exception.BookExceptionCode.BOOK_NOT_FOUND;

public class BookNotFoundException extends CustomException {
public BookNotFoundException() {
super(BOOK_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public record BookPageResponse<T>(
@Schema(
description = "도서 목록",
example = "[{\"id\": 1, \"title\": \"스프링 부트와 AWS로 혼자 구현하는 웹 서비스\", \"author\": \"이한음\", \"description\": \"스프링 부트와 AWS로 혼자 구현하는 웹 서비스\", \"file\": {\"id\": 1, \"logicalName\": \"example.jpg\", \"physicalPath\": \"https://example-bucket.ncp.com/files/example.jpg\"}}]",
example = "[{\"id\": 1, \"title\": \"스프링 부트와 AWS로 혼자 구현하는 웹 서비스\", \"author\": \"이동욱\", \"description\": \"이 책은 스프링부트 입문자용 책입니다.\", \"file\": {\"id\": 1, \"logicalName\": \"example.jpg\", \"physicalPath\": \"https://example-bucket.ncp.com/files/example.jpg\"}}]",
requiredMode = REQUIRED
)
List<BookResponse> contents,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public record BookResponse(
@Schema(description = "도서 제목", example = "스프링 부트와 AWS로 혼자 구현하는 웹 서비스", requiredMode = REQUIRED)
String title,

@Schema(description = "도서 저자", example = "이한음", requiredMode = NOT_REQUIRED)
@Schema(description = "도서 저자", example = "이동욱", requiredMode = NOT_REQUIRED)
String author,

@Schema(description = "도서 설명", example = "스프링 부트와 AWS로 혼자 구현하는 웹 서비스", requiredMode = NOT_REQUIRED)
@Schema(description = "도서 설명", example = "이 책은 스프링 부트 입문자용 책입니다.", requiredMode = NOT_REQUIRED)
String description,

@Schema(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package goorm.unit.booklog.domain.book.presentation.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;

import java.util.List;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

@Builder
public record UserBookListResponse(
@Schema(description = "유저 아이디", example = "id1234", requiredMode = REQUIRED)
String userId,

@Schema(description = "유저 이름", example = "홍길동", requiredMode = REQUIRED)
String userName,

@Schema(description = "읽은 책 수", example = "1", requiredMode = REQUIRED)
Integer bookCount,

@Schema(description = "작성한 독후감 수", example = "1", requiredMode = REQUIRED)
Integer reviewCount,

@Schema(
description = "읽은 책 목록",
example = "[{\"id\": 1, \"title\": \"스프링 부트와 AWS로 혼자 구현하는 웹 서비스\", \"author\": \"이한음\", \"description\": \"스프링 부트와 AWS로 혼자 구현하는 웹 서비스\", \"file\": {\"id\": 1, \"logicalName\": \"example.jpg\", \"physicalPath\": \"https://example-bucket.ncp.com/files/example.jpg\"}}]",
requiredMode = REQUIRED
)
List<BookResponse> bookResponses
) {
public static UserBookListResponse of(String userId, String userName, Integer bookCount, Integer reviewCount, List<BookResponse> bookResponses) {
return UserBookListResponse.builder()
.userId(userId)
.userName(userName)
.bookCount(bookCount)
.reviewCount(reviewCount)
.bookResponses(bookResponses)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package goorm.unit.booklog.domain.review.application;

import static goorm.unit.booklog.domain.review.domain.ReviewStatus.ACTIVE;
import static goorm.unit.booklog.domain.review.domain.ReviewStatus.INACTIVE;

import goorm.unit.booklog.domain.book.domain.Book;
import goorm.unit.booklog.domain.review.presentation.response.ReviewListResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -20,6 +23,9 @@
import goorm.unit.booklog.domain.user.domain.User;
import lombok.RequiredArgsConstructor;

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

@Service
@RequiredArgsConstructor
public class ReviewService {
Expand All @@ -31,6 +37,7 @@ public class ReviewService {
@Transactional
public ReviewPersistResponse createReview(MultipartFile file, ReviewCreateRequest request) {
User user = userService.me();
Book book=bookService.getBookById(request.bookId());
File uploadedFile = null;
if (file != null) {
uploadedFile = fileService.uploadAndSaveFile(file);
Expand All @@ -40,9 +47,11 @@ public ReviewPersistResponse createReview(MultipartFile file, ReviewCreateReques
request.content(),
uploadedFile,
user,
bookService.getBookById(request.bookId())
book
);
Long id = reviewRepository.save(review).getId();
user.updateBook(book);
user.updateReview(review);
return ReviewPersistResponse.of(id);
}

Expand Down Expand Up @@ -74,4 +83,18 @@ public void deleteReview(Long id) {
private Review getReviewById(Long id) {
return reviewRepository.findById(id).orElseThrow(ReviewNotFoundException::new);
}

@Transactional
public ReviewListResponse getReviewListByBook(Long bookId) {
Book book=bookService.getBookById(bookId);
List<Review> reviews=reviewRepository.findAllByBook(book);
List<ReviewResponse> reviewResponses=new ArrayList<>();
for(Review review:reviews) {
if(review.getStatus().equals(ACTIVE)) {
ReviewResponse reviewResponse=ReviewResponse.of(review);
reviewResponses.add(reviewResponse);
}
}
return new ReviewListResponse(reviewResponses);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package goorm.unit.booklog.domain.review.domain;

import goorm.unit.booklog.domain.book.domain.Book;

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

public interface ReviewRepository {
Review save(Review review);

Optional<Review> findById(Long id);

List<Review> findAllByBook(Book book);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package goorm.unit.booklog.domain.review.infrastructure;

import goorm.unit.booklog.domain.book.domain.Book;
import goorm.unit.booklog.domain.review.domain.Review;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface JpaReviewRepository extends JpaRepository<Review,Long> {
List<Review> findAllByBook(Book book);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package goorm.unit.booklog.domain.review.infrastructure;

import goorm.unit.booklog.domain.book.domain.Book;
import goorm.unit.booklog.domain.book.infrastructure.JpaBookRepository;
import goorm.unit.booklog.domain.review.domain.Review;
import goorm.unit.booklog.domain.review.domain.ReviewRepository;
import goorm.unit.booklog.domain.user.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

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

@Repository
Expand All @@ -22,4 +26,6 @@ public Optional<Review> findById(Long id) {
return jpaReviewRepository.findById(id);
}

@Override
public List<Review> findAllByBook(Book book) { return jpaReviewRepository.findAllByBook(book);}
}
Loading

0 comments on commit b83b512

Please sign in to comment.