Skip to content

Commit

Permalink
[BOOK-16]-독후감 쓰기 수정 삭제 조회 API (#11)
Browse files Browse the repository at this point in the history
* Add review functionality

* Add review functionality

* [BOOK-16]-refactor: 독후감 생성 및 조회 api 수정

* [BOOK-16]-refactor: 코멘트 수정 사항 반영

* [BOOK-16]-refactor: 수정 기능 관련 수정

* [BOOK-16]-refactor: 추가 코멘트 사항 수정

---------

Co-authored-by: sunny2you <[email protected]>
  • Loading branch information
sungchaewon and sunny2you authored Oct 11, 2024
1 parent 06726c0 commit 67c9d0c
Show file tree
Hide file tree
Showing 23 changed files with 565 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
"/favicon.ico",
"/index.html",
"/api/v1/users/signup",
"/api/v1/auth/login"
"/api/v1/auth/login",
"/api/v1/users/duplication",
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import goorm.unit.booklog.domain.book.domain.Book;
import goorm.unit.booklog.domain.file.domain.File;
import goorm.unit.booklog.domain.file.infrastructure.FileRepository;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -23,6 +27,7 @@
@RequiredArgsConstructor
public class BookService {
private final BookRepository bookRepository;
private final FileRepository fileRepository;

@Value("${naver.api.clientId}")
private String clientId;
Expand Down Expand Up @@ -57,18 +62,43 @@ public BookPageResponse searchBooks(int page, int size, String keyword) {
List<BookResponse> bookResponses = new ArrayList<>();
for (int i = 0; i < items.length(); i++) {
JSONObject item = items.getJSONObject(i);
BookResponse bookResponse = BookResponse.of(
(long)(i + 1),
item.getString("title"),
item.getString("author"),
item.getString("description"),
item.getString("link")
);

Book book;
Long fileId;
String title = item.getString("title");
String author = item.getString("author");
String description=item.getString("description");
String link=item.getString("link");

Optional<Book> existingBook = bookRepository.findByTitleAndAuthor(title, author);

if (!existingBook.isPresent()) {
File file = File.of(title, link);

book = Book.create(
title,
author,
description,
file
);
bookRepository.save(book);
}
else{
book= existingBook.get();
}

BookResponse bookResponse = BookResponse.from(book);
bookResponses.add(bookResponse);

}

int total = jsonResponse.getInt("total");
return BookPageResponse.of(bookResponses, PageableResponse.of(PageRequest.of(page, size), (long)total));

}

public Book getBook(Long id) {
return bookRepository.findById(id).orElse(null);
}

}
8 changes: 4 additions & 4 deletions src/main/java/goorm/unit/booklog/domain/book/domain/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import jakarta.persistence.CascadeType;

@Getter
@Entity
Expand All @@ -38,19 +39,18 @@ public class Book extends BaseTimeEntity {
@Column(nullable = false)
private String author;

@Column(nullable = false, length = 1000)
@Column(nullable = false, length = 2000)
private String description;

@OneToOne
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "file_id")
private File file;

@ManyToMany(mappedBy = "books")
private List<User> users = new ArrayList<>();

public static Book create(Long id, String title, String author, String description, File file) {
public static Book create(String title, String author, String description, File file) {
return Book.builder()
.id(id)
.title(title)
.author(author)
.description(description)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
package goorm.unit.booklog.domain.book.domain;


import java.util.Optional;

public interface BookRepository {

Book save(Book book);

Optional<Book> findById(Long id);

Optional<Book> findByTitleAndAuthor(String title, String author);

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

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

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

import java.util.Optional;

@Repository
@RequiredArgsConstructor
public class BookRepositoryImpl implements BookRepository {
private final JpaBookRepository jpaBookRepository;

@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> findByTitleAndAuthor(String title, String author){
return jpaBookRepository.findByTitleAndAuthor(title,author);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import org.springframework.data.jpa.repository.JpaRepository;

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

public interface JpaBookRepository extends JpaRepository<Book, String> {

public interface JpaBookRepository extends JpaRepository<Book, Long> {
Optional<Book> findByTitleAndAuthor(String title, String author);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

import goorm.unit.booklog.domain.book.domain.Book;
import goorm.unit.booklog.domain.file.domain.File;
import goorm.unit.booklog.domain.file.presentation.response.FileResponse;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
Expand All @@ -28,6 +29,7 @@ public record BookResponse(
requiredMode = NOT_REQUIRED)
FileResponse file
) {

public static BookResponse from(Book book) {
return BookResponse.builder()
.id(book.getId())
Expand All @@ -38,13 +40,13 @@ public static BookResponse from(Book book) {
.build();
}

public static BookResponse of (Long id, String title, String author, String description, String physicalPath) {
public static BookResponse of (Long id, String title, String author, String description, File file) {
return BookResponse.builder()
.id(id)
.title(title)
.author(author)
.description(description)
.file(FileResponse.of(title, physicalPath))
.file(FileResponse.from(file))
.build();
}
}
4 changes: 4 additions & 0 deletions src/main/java/goorm/unit/booklog/domain/file/domain/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public static File of(String logicalName, String filePath) {
.physicalPath(filePath)
.build();
}

public void updateFile(String filePath) {
this.physicalPath = filePath;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package goorm.unit.booklog.domain.review.application;

import goorm.unit.booklog.domain.book.application.BookService;
import goorm.unit.booklog.domain.book.domain.Book;
import goorm.unit.booklog.domain.book.infrastructure.BookRepositoryImpl;
import goorm.unit.booklog.domain.file.domain.File;
import goorm.unit.booklog.domain.review.domain.Review;
import goorm.unit.booklog.domain.review.domain.ReviewRepository;
import goorm.unit.booklog.domain.review.domain.ReviewStatus;
import goorm.unit.booklog.domain.review.presentation.request.ReviewCreateRequest;
import goorm.unit.booklog.domain.review.presentation.response.ReviewPersistResponse;
import goorm.unit.booklog.domain.review.presentation.response.ReviewResponse;
import goorm.unit.booklog.domain.user.application.UserService;
import goorm.unit.booklog.domain.user.domain.User;
import goorm.unit.booklog.domain.review.presentation.exception.ReviewNotFoundException;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



@Service
@RequiredArgsConstructor
public class ReviewService {
private final ReviewRepository reviewRepository;
private final BookRepositoryImpl bookRepository;
private final UserService userService;
private final BookService bookService;

@Transactional
public ReviewPersistResponse createReview(ReviewCreateRequest request) {
User user = userService.me();
File file=File.of(request.title(), request.img());
Book book = bookService.getBook(request.bookResponse().id());
Review review = Review.create(
request.title(),
request.content(),
file,
user,
book
);
Long id = reviewRepository.save(review).getId();
return ReviewPersistResponse.of(id);
}

@Transactional
public ReviewResponse getReview(Long id) {
Review review= reviewRepository.findById(id).orElseThrow(ReviewNotFoundException::new);
return ReviewResponse.of(review);
}

@Transactional
public ReviewResponse updateReview(Long id, ReviewCreateRequest request) {
Review review = reviewRepository.findById(id)
.orElseThrow(ReviewNotFoundException::new);
Book book = bookService.getBook(request.bookResponse().id());
review.updateTitle(request.title());
review.updateContent(request.content());
review.updateBook(book);

File file=review.getFile();
file.updateFile(request.img());

return ReviewResponse.of(review);
}

@Transactional
public void deleteReview(Long id) {
Review review = reviewRepository.findById(id).orElseThrow(ReviewNotFoundException::new);
review.updateStatus(ReviewStatus.INACTIVE);
}

}
75 changes: 75 additions & 0 deletions src/main/java/goorm/unit/booklog/domain/review/domain/Review.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package goorm.unit.booklog.domain.review.domain;

import static jakarta.persistence.GenerationType.IDENTITY;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import goorm.unit.booklog.common.domain.BaseTimeEntity;
import goorm.unit.booklog.domain.book.domain.Book;
import goorm.unit.booklog.domain.file.domain.File;
import goorm.unit.booklog.domain.user.domain.User;
import jakarta.persistence.*;
import lombok.*;

@Getter
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor(access= AccessLevel.PROTECTED)
public class Review extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

@Column(nullable = false)
private String title;

@Column(nullable = false, length = 2000)
private String content;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "file_id")
private File file;

@Enumerated(EnumType.STRING)
@Column(nullable = false, length = 10)
private ReviewStatus status = ReviewStatus.ACTIVE; // 기본값 설정

@ManyToOne
@JoinColumn(name = "user_id",referencedColumnName = "id") // 외래 키 설정
private User user;

@ManyToOne
@JoinColumn(name="book_id")
private Book book;

public static Review create( String title, String content, File file,User user, Book book ) {
return Review.builder()
.title(title)
.content(content)
.file(file)
.user(user)
.status(ReviewStatus.ACTIVE)
.book(book)
.build();
}

public void updateTitle( String title) {
this.title = title;
}

public void updateContent(String content) {
this.content = content;
}

public void updateBook( Book book ) {
this.book = book;
}

public void updateStatus(ReviewStatus status) {
this.status = status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package goorm.unit.booklog.domain.review.domain;

import java.util.Optional;

public interface ReviewRepository {

Review save(Review review);

Optional<Review> findById(Long id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package goorm.unit.booklog.domain.review.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum ReviewStatus {
ACTIVE("활성화된 게시물 입니다."),
INACTIVE("삭제된 게시물 입니다.");

private final String description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package goorm.unit.booklog.domain.review.infrastructure;

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

public interface JpaReviewRepository extends JpaRepository<Review,Long> {
}
Loading

0 comments on commit 67c9d0c

Please sign in to comment.