Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last hong #23

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.book.app.modules.books.dto.BookRequestDto.toEntity;

@Slf4j
@RestController
Expand All @@ -30,12 +29,11 @@ public class BookController {
*/
@PostMapping("/add")
public ResponseEntity addBook(@RequestBody @Valid BookRequestDto bookRequestDto) {

// todo : valid 체크 예외 처리 코드 추가
log.info("book-add request = {}", bookRequestDto);
Book book = toEntity(bookRequestDto);
Book saveBook = bookService.addBookInfo(book);

Book saveBook = bookService.addBookInfo(bookRequestDto);
BookResponseDto response = Book.toResponseDto(saveBook);
log.info("book-add response = {}", response);

return new ResponseEntity<>(Response.success(Domain.book, response),
HttpStatus.CREATED);
Expand Down
15 changes: 3 additions & 12 deletions src/main/java/com/book/app/modules/books/dto/BookRequestDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.book.app.modules.books.entity.Book;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

@Data
Expand All @@ -19,19 +18,11 @@ public class BookRequestDto {
@NotBlank(message = "도서 이미지를 등록해주세요.")
private String img;

@NotNull(message = "진행 상태를 선택해주세요.")
private Book.BookStatus status;
private String status;

private String createdBy;

public static Book toEntity(BookRequestDto dto) {
return Book.builder()
.title(dto.getTitle())
.author(dto.getAuthor())
.publisher(dto.getPublisher())
.img(dto.getImg())
.createdBy(dto.getCreatedBy())
.status(dto.getStatus())
.build();
public Book toEntity() {
return Book.of(this.title, this.author, this.publisher, this.img, this.createdBy, this.status);
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/book/app/modules/books/entity/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public class Book {
@Enumerated(EnumType.STRING)
private BookStatus status;

public Book(String title, String author, String publisher, String img, String createdBy, String status) {
this.title = title;
this.author = author;
this.publisher = publisher;
this.img = img;
this.createdBy = createdBy;
this.status = BookStatus.valueOf(status);
}

public enum BookStatus {
BEFORE_PROGRESS("진행예정"),
IN_PROGRESS("진행중"),
Expand Down Expand Up @@ -86,4 +95,8 @@ public static BookResponseDto toResponseDto(Book book) {
.createDt(book.getCreatedDt())
.build();
}

public static Book of(String title, String author, String publisher, String img, String createdBy, String status) {
return new Book(title, author, publisher, img, createdBy, status);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.book.app.modules.books.service;

import com.book.app.modules.books.dto.BookRequestDto;
import com.book.app.modules.books.entity.Book;
import jakarta.validation.Valid;

public interface BookService {

/**
* 도서 등록
* @param book
* @return
*/
Book addBookInfo(Book book);
Book addBookInfo(@Valid BookRequestDto book);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.book.app.modules.books.service.impl;

import com.book.app.modules.books.dto.BookRequestDto;
import com.book.app.modules.books.entity.Book;
import com.book.app.modules.books.repository.BookRepository;
import com.book.app.modules.books.service.BookService;
Expand All @@ -14,7 +15,8 @@ public class BookServiceImpl implements BookService {
private final BookRepository bookRepository;

@Override
public Book addBookInfo(Book book) {
public Book addBookInfo(BookRequestDto bookRequestDto) {
Book book = bookRequestDto.toEntity();
/* 동일한 책이 있는지 체크 로직 추가 ? */
return bookRepository.save(book);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void insertBookTest() {
baseBook.setStatus(Book.BookStatus.IN_PROGRESS);
baseBook.setCreatedBy("작성자");

Book book = baseBook.toEntity(baseBook);
Book book = baseBook.toEntity();

//when
Book response = bookRepository.save(book);
Expand Down