Skip to content

Commit

Permalink
[update] Book 관련 오류 수정 및 개선 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
hmj committed Jan 26, 2024
1 parent 9a75135 commit f9950bd
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


@RestController
@RequestMapping(value = "/book")
@RequestMapping(value = "/books")
@RequiredArgsConstructor
public class BookController {
private final BookService bookService;

/**
* 도서 추가
*/
@PostMapping("/insertBook")
@PostMapping("/add")
public ResponseEntity addBook(@RequestBody @Valid BookRequestDto bookRequestDto) {
// todo : valid 체크 예외 처리 코드 추가
Book book = Book.toEntity(bookRequestDto);
Book book = toEntity(bookRequestDto);
Book saveBook = bookService.addBookInfo(book);
BookResponseDto response = Book.toResponseDto(saveBook);

Expand Down
23 changes: 18 additions & 5 deletions src/main/java/com/book/app/modules/books/dto/BookRequestDto.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
package com.book.app.modules.books.dto;

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

@Data
public class BookRequestDto {
@NotBlank(message = "도서 제목을 입력해주세요.")
private String bookTitle;
private String title;

@NotBlank(message = "저자를 입력해주세요.")
private String author;

@NotBlank(message = "출판사를 입력해주세요.")
private String publisher;

@NotBlank(message = "도서 제목을 입력해주세요.")
private String bookImg;
@NotBlank(message = "도서 이미지를 등록해주세요.")
private String img;

@NotBlank(message = "진행 상태를 선택해주세요.")
private String status;
@NotNull(message = "진행 상태를 선택해주세요.")
private Book.BookStatus 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();
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.book.app.modules.books.dto;

import com.book.app.modules.books.entity.Book;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Timestamp;
import java.time.LocalDateTime;

@Builder
@Data
@Builder
public class BookResponseDto {
private Long bookId;
private String bookTitle;
private String title;
private String author;
private String publisher;
private String bookImg;
private String img;
private String status;
private String createdBy;
private Timestamp createDt;
private LocalDateTime createDt;

}
55 changes: 27 additions & 28 deletions src/main/java/com/book/app/modules/books/entity/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@
import com.book.app.modules.global.entity.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;

import java.sql.Timestamp;

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Book extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private Long bookId;

@Column(nullable = false)
private String bookTitle;
private String title;

@Column(nullable = false)
private String author;
Expand All @@ -31,41 +27,44 @@ public class Book extends BaseTimeEntity {
private String publisher;

@Column(nullable = false)
private String bookImg;

@Column(updatable = false)
@CreatedDate
private Timestamp createDt;
private String img;

@Column(nullable = false)
private String createdBy;

@Column(nullable = false)
private String status;
@Enumerated(EnumType.STRING)
private BookStatus status;


public enum BookStatus {
BEFORE_PROGRESS("진행예정"),
IN_PROGRESS("진행중"),
COMPLETED("진행완료");

private String description;

BookStatus(String description) {
this.description = description;
}

@Override
public String toString() {
return description;
}

@Column
private String finishDt;

public static Book toEntity(BookRequestDto dto) {
return Book.builder()
.bookTitle(dto.getBookTitle())
.author(dto.getAuthor())
.publisher(dto.getPublisher())
.bookImg(dto.getBookImg())
.createdBy(dto.getCreatedBy())
.status(dto.getStatus())
.build();
}


public static BookResponseDto toResponseDto(Book book) {
return BookResponseDto.builder()
.bookId(book.getBookId())
.bookTitle(book.getBookTitle())
.title(book.getTitle())
.author(book.getAuthor())
.publisher(book.getPublisher())
.bookImg(book.getBookImg())
.img(book.getImg())
.createdBy(book.getCreatedBy())
.status(book.getStatus())
.status(book.getStatus().toString())
.createDt(book.getCreateDt())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
@EntityListeners(AuditingEntityListener.class)
public class BaseTimeEntity {
@CreatedDate
private LocalDateTime localDateTime;
private LocalDateTime createDt;

@LastModifiedDate
private LocalDateTime modifiedDate;
private LocalDateTime finishDt;
}
24 changes: 14 additions & 10 deletions src/test/java/com/book/app/modules/books/BookControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.book.app.modules.books;

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 org.junit.jupiter.api.Assertions;
Expand All @@ -9,6 +10,7 @@
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;



@DataJpaTest
public class BookControllerTest {

Expand All @@ -19,24 +21,26 @@ public class BookControllerTest {
@DisplayName("도서 등록 테스트")
public void insertBookTest() {
//given
Book book = new Book();
book.setBookTitle("책 제목");
book.setBookImg("이미지경로");
book.setAuthor("저자");
book.setPublisher("출판사");
book.setStatus("진행중");
book.setCreatedBy("작성자");
BookRequestDto baseBook = new BookRequestDto();
baseBook.setTitle("책 제목");
baseBook.setImg("이미지경로");
baseBook.setAuthor("저자");
baseBook.setPublisher("출판사");
baseBook.setStatus(Book.BookStatus.IN_PROGRESS);
baseBook.setCreatedBy("작성자");

Book book = baseBook.toEntity(baseBook);

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

//then
Assertions.assertAll(
() -> Assertions.assertEquals("책 제목", response.getBookTitle()),
() -> Assertions.assertEquals("이미지경로", response.getBookImg()),
() -> Assertions.assertEquals("책 제목", response.getTitle()),
() -> Assertions.assertEquals("이미지경로", response.getImg()),
() -> Assertions.assertEquals("저자", response.getAuthor()),
() -> Assertions.assertEquals("출판사", response.getPublisher()),
() -> Assertions.assertEquals("진행중", response.getStatus()),
() -> Assertions.assertEquals(Book.BookStatus.IN_PROGRESS, response.getStatus()),
() -> Assertions.assertEquals("작성자", response.getCreatedBy()),
() -> Assertions.assertFalse(response.getBookId().describeConstable().isEmpty())
);
Expand Down

0 comments on commit f9950bd

Please sign in to comment.