Skip to content

Commit

Permalink
[BOOK-24]-독후감에서 multipart 파일을 첨부할 수 있도록 수정 (#12)
Browse files Browse the repository at this point in the history
* [BOOK-24]-refactor: 독후감 첨부 사진을 Multipart로 받을 수 있게 수정

* [BOOK-24]-refactor: 독후감 첨부 사진 url null 안뜨게 수정

* [BOOK-24]-feat: 최대 파일 사이즈 설정
  • Loading branch information
LeeHanEum authored Oct 11, 2024
1 parent 67c9d0c commit 0372fe7
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 179 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package goorm.unit.booklog.common.util;

import java.lang.reflect.Type;

import org.springframework.http.MediaType;
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class MultipartJackson2HttpMessageConverter extends AbstractJackson2HttpMessageConverter {

public MultipartJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper, MediaType.APPLICATION_OCTET_STREAM);
}

@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return false;
}

@Override
public boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) {
return false;
}

@Override
protected boolean canWrite(MediaType mediaType) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
@RequiredArgsConstructor
public class BookService {
private final BookRepository bookRepository;
private final FileRepository fileRepository;

@Value("${naver.api.clientId}")
private String clientId;
Expand Down Expand Up @@ -64,23 +63,15 @@ public BookPageResponse searchBooks(int page, int size, String keyword) {
JSONObject item = items.getJSONObject(i);

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

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

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

book = Book.create(
title,
author,
description,
file
);
if (existingBook.isEmpty()) {
File file = File.of(title, image);
book = Book.create(title, author, description, file);
bookRepository.save(book);
}
else{
Expand All @@ -89,15 +80,12 @@ public BookPageResponse searchBooks(int page, int size, String keyword) {

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) {
public Book getBookById(Long id) {
return bookRepository.findById(id).orElse(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ public static BookResponse from(Book book) {
.title(book.getTitle())
.author(book.getAuthor())
.description(book.getDescription())
.file(FileResponse.from(book.getFile()))
.file(FileResponse.of(book.getTitle(), book.getFile()))
.build();
}

public static BookResponse of (Long id, String title, String author, String description, File file) {
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.from(file))
.file(FileResponse.of(title, file.getPhysicalPath()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

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

import org.springframework.beans.factory.annotation.Value;

import goorm.unit.booklog.domain.file.domain.File;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
Expand All @@ -20,17 +18,11 @@ public record FileResponse(
@Schema(description = "파일 경로", example = "https://example.com", requiredMode = REQUIRED)
String physicalPath
) {
@Value("${cloud.ncp.object-storage.credentials.endpoint}")
private static String endpoint;

@Value("${cloud.ncp.object-storage.credentials.bucket}")
private static String bucketName;

public static FileResponse from(File file) {
return FileResponse.builder()
.id(file.getId())
.logicalName(file.getLogicalName())
.physicalPath(endpoint + "/" + bucketName + "/" + file.getPhysicalPath())
.physicalPath("https://kr.object.ncloudstorage.com/booklog-bucket/" + file.getPhysicalPath())
.build();
}

Expand All @@ -40,4 +32,11 @@ public static FileResponse of(String logicalName, String physicalPath) {
.physicalPath(physicalPath)
.build();
}

public static FileResponse of (String title, File file) {
return FileResponse.builder()
.logicalName(title + " 대표 이미지")
.physicalPath(file.getPhysicalPath())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
package goorm.unit.booklog.domain.review.application;

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

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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.application.FileService;
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.exception.ReviewNotFoundException;
import goorm.unit.booklog.domain.review.presentation.request.ReviewCreateRequest;
import goorm.unit.booklog.domain.review.presentation.request.ReviewUpdateRequest;
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;
private final FileService fileService;

@Transactional
public ReviewPersistResponse createReview(ReviewCreateRequest request) {
public ReviewPersistResponse createReview(MultipartFile file, ReviewCreateRequest request) {
User user = userService.me();
File file=File.of(request.title(), request.img());
Book book = bookService.getBook(request.bookResponse().id());
File uploadedFile = null;
if (file != null) {
uploadedFile = fileService.uploadAndSaveFile(file);
}
Review review = Review.create(
request.title(),
request.content(),
file,
uploadedFile,
user,
book
bookService.getBookById(request.bookId())
);
Long id = reviewRepository.save(review).getId();
return ReviewPersistResponse.of(id);
Expand All @@ -51,24 +53,25 @@ public ReviewResponse getReview(Long id) {
}

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

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

return ReviewResponse.of(review);
File uploadedFile = null;
if (file != null) {
uploadedFile = fileService.uploadAndSaveFile(file);
}
review.updateFile(uploadedFile);
}

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

private Review getReviewById(Long id) {
return reviewRepository.findById(id).orElseThrow(ReviewNotFoundException::new);
}
}
36 changes: 18 additions & 18 deletions src/main/java/goorm/unit/booklog/domain/review/domain/Review.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package goorm.unit.booklog.domain.review.domain;

import static goorm.unit.booklog.domain.review.domain.ReviewStatus.ACTIVE;
import static jakarta.persistence.GenerationType.IDENTITY;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -30,43 +31,42 @@ public class Review extends BaseTimeEntity {
@Column(nullable = false, length = 2000)
private String content;

@Enumerated(EnumType.STRING)
private ReviewStatus status;

@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") // 외래 키 설정
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="book_id")
private Book book;

public static Review create( String title, String content, File file,User user, 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();
.title(title)
.content(content)
.status(ACTIVE)
.file(file)
.user(user)
.book(book)
.build();
}

public void updateTitle( String title) {
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 updateFile(File file) {
this.file = file;
}

public void updateStatus(ReviewStatus status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
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
Expand Up @@ -6,8 +6,9 @@
@Getter
@AllArgsConstructor
public enum ReviewStatus {
ACTIVE("활성화된 게시물 입니다."),
INACTIVE("삭제된 게시물 입니다.");
ACTIVE("활성화", "활성화된 게시물 입니다."),
INACTIVE("비활성화", "삭제되어 보이지 않는 게시물 입니다.");

private final String description;
private String key;
private String description;
}
Loading

0 comments on commit 0372fe7

Please sign in to comment.