-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from BBack-BBoo-Team/책_등록_홍
[서버] 도서 등록 개발
- Loading branch information
Showing
12 changed files
with
286 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
# 도서 도메인 | ||
# 도서 도메인 | ||
|
||
|
||
## 도서 API 명세서 | ||
|
||
> `도서 등록 api` : | ||
> https://documenter.getpostman.com/view/21357078/2s9YsRc8w3 | ||
|
||
--- | ||
|
38 changes: 38 additions & 0 deletions
38
src/main/java/com/book/app/modules/books/controller/BookController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.book.app.modules.books.controller; | ||
|
||
import com.book.app.modules.books.dto.BookRequestDto; | ||
import com.book.app.modules.books.dto.BookResponseDto; | ||
import com.book.app.modules.books.entity.Book; | ||
import com.book.app.modules.books.service.BookService; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
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 = "/books") | ||
@RequiredArgsConstructor | ||
public class BookController { | ||
private final BookService bookService; | ||
|
||
/** | ||
* 도서 추가 | ||
*/ | ||
@PostMapping("/add") | ||
public ResponseEntity addBook(@RequestBody @Valid BookRequestDto bookRequestDto) { | ||
// todo : valid 체크 예외 처리 코드 추가 | ||
Book book = toEntity(bookRequestDto); | ||
Book saveBook = bookService.addBookInfo(book); | ||
BookResponseDto response = Book.toResponseDto(saveBook); | ||
|
||
return new ResponseEntity<>(response, HttpStatus.CREATED); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/book/app/modules/books/dto/BookRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 title; | ||
|
||
@NotBlank(message = "저자를 입력해주세요.") | ||
private String author; | ||
|
||
@NotBlank(message = "출판사를 입력해주세요.") | ||
private String publisher; | ||
|
||
@NotBlank(message = "도서 이미지를 등록해주세요.") | ||
private String img; | ||
|
||
@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(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/book/app/modules/books/dto/BookResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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.time.LocalDateTime; | ||
|
||
@Data | ||
@Builder | ||
public class BookResponseDto { | ||
private Long bookId; | ||
private String title; | ||
private String author; | ||
private String publisher; | ||
private String img; | ||
private String status; | ||
private String createdBy; | ||
private LocalDateTime createDt; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.book.app.modules.books.entity; | ||
|
||
import com.book.app.modules.books.dto.BookRequestDto; | ||
import com.book.app.modules.books.dto.BookResponseDto; | ||
import com.book.app.modules.global.entity.BaseTimeEntity; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Entity | ||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class Book extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column | ||
private Long bookId; | ||
|
||
@Column(nullable = false) | ||
private String title; | ||
|
||
@Column(nullable = false) | ||
private String author; | ||
|
||
@Column(nullable = false) | ||
private String publisher; | ||
|
||
@Column(nullable = false) | ||
private String img; | ||
|
||
@Column(nullable = false) | ||
private String createdBy; | ||
|
||
@Column(nullable = false) | ||
@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; | ||
} | ||
|
||
} | ||
|
||
|
||
public static BookResponseDto toResponseDto(Book book) { | ||
return BookResponseDto.builder() | ||
.bookId(book.getBookId()) | ||
.title(book.getTitle()) | ||
.author(book.getAuthor()) | ||
.publisher(book.getPublisher()) | ||
.img(book.getImg()) | ||
.createdBy(book.getCreatedBy()) | ||
.status(book.getStatus().toString()) | ||
.createDt(book.getCreateDt()) | ||
.build(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/book/app/modules/books/repository/BookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.book.app.modules.books.repository; | ||
|
||
import com.book.app.modules.books.entity.Book; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface BookRepository extends JpaRepository<Book, Long> { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/book/app/modules/books/service/BookService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.book.app.modules.books.service; | ||
|
||
import com.book.app.modules.books.entity.Book; | ||
|
||
public interface BookService { | ||
Book addBookInfo(Book book); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/book/app/modules/books/service/impl/BookServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.book.app.modules.books.service.impl; | ||
|
||
import com.book.app.modules.books.entity.Book; | ||
import com.book.app.modules.books.repository.BookRepository; | ||
import com.book.app.modules.books.service.BookService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class BookServiceImpl implements BookService { | ||
private final BookRepository bookRepository; | ||
|
||
@Override | ||
public Book addBookInfo(Book book) { | ||
/* 동일한 책이 있는지 체크 로직 추가 ? */ | ||
return bookRepository.save(book); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/book/app/modules/global/entity/BaseTimeEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.book.app.modules.global.entity; | ||
|
||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.MappedSuperclass; | ||
import lombok.Getter; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.LastModifiedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@MappedSuperclass | ||
@EntityListeners(AuditingEntityListener.class) | ||
public class BaseTimeEntity { | ||
@CreatedDate | ||
private LocalDateTime createDt; | ||
|
||
@LastModifiedDate | ||
private LocalDateTime finishDt; | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/com/book/app/modules/books/BookControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
|
||
|
||
|
||
@DataJpaTest | ||
public class BookControllerTest { | ||
|
||
@Autowired | ||
BookRepository bookRepository; | ||
|
||
@Test | ||
@DisplayName("도서 등록 테스트") | ||
public void insertBookTest() { | ||
//given | ||
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.getTitle()), | ||
() -> Assertions.assertEquals("이미지경로", response.getImg()), | ||
() -> Assertions.assertEquals("저자", response.getAuthor()), | ||
() -> Assertions.assertEquals("출판사", response.getPublisher()), | ||
() -> Assertions.assertEquals(Book.BookStatus.IN_PROGRESS, response.getStatus()), | ||
() -> Assertions.assertEquals("작성자", response.getCreatedBy()), | ||
() -> Assertions.assertFalse(response.getBookId().describeConstable().isEmpty()) | ||
); | ||
} | ||
} |