Skip to content

Commit

Permalink
Merge pull request #9 from BBack-BBoo-Team/책_등록_홍
Browse files Browse the repository at this point in the history
[서버] 도서 등록 개발
  • Loading branch information
Si-Hyeak-KANG authored Jan 30, 2024
2 parents 02764df + f9950bd commit 566e329
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'

annotationProcessor 'org.projectlombok:lombok'
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/book/app/SebadogApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableJpaAuditing
@SpringBootApplication
public class SebadogApp {

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/book/app/modules/books/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
# 도서 도메인
# 도서 도메인


## 도서 API 명세서

> `도서 등록 api` :
> https://documenter.getpostman.com/view/21357078/2s9YsRc8w3

---

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 src/main/java/com/book/app/modules/books/dto/BookRequestDto.java
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 src/main/java/com/book/app/modules/books/dto/BookResponseDto.java
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;

}
71 changes: 71 additions & 0 deletions src/main/java/com/book/app/modules/books/entity/Book.java
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();
}
}
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> {
}
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);
}
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);
}
}
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 src/test/java/com/book/app/modules/books/BookControllerTest.java
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())
);
}
}

0 comments on commit 566e329

Please sign in to comment.