Skip to content

Commit

Permalink
Merge pull request #40 from kookmin-sw/BE_Feature/#39-book
Browse files Browse the repository at this point in the history
#39 feat: db에 없는 책 추가
  • Loading branch information
wjdwlghks authored Apr 26, 2024
2 parents 82fba5f + aa6ed91 commit 945521b
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.project.capstone.member.controller.dto;

import java.time.LocalDateTime;

public record AddMyBookRequest (
String isbn,
String title,
String category1d,
String category2d,
String category3d,
String author,
String publisher,
String publishDate
) {
}
package com.project.capstone.book.controller;

public record AddBookRequest(
String isbn,
String title,
String category1d,
String category2d,
String category3d,
String author,
String publisher,
String publishDate
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.project.capstone.book.controller;

import com.project.capstone.book.service.BookService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/book")
@RequiredArgsConstructor
public class BookController {

private final BookService bookService;

// 책 추가하기
@PostMapping("/add")
public ResponseEntity<?> addBook(@RequestBody AddBookRequest request) {
bookService.addBook(request);
return ResponseEntity.ok().body("도서 추가 완료");
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.project.capstone.book.domain;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.project.capstone.book.controller.AddBookRequest;
import com.project.capstone.club.domain.Club;
import com.project.capstone.content.domain.Content;
import com.project.capstone.member.controller.dto.AddMyBookRequest;
import com.project.capstone.mybook.domain.MyBook;
import com.project.capstone.quiz.domain.Quiz;
import jakarta.persistence.*;
Expand Down Expand Up @@ -53,8 +53,9 @@ public class Book {
@OneToMany(mappedBy = "book")
private List<MyBook> membersAddThisBook = new ArrayList<>();

public Book(AddMyBookRequest request) {
public Book(AddBookRequest request) {
this(null, request.isbn(), request.title(), request.category1d(), request.category2d(), request.category3d(),
request.author(), request.publisher(), request.publishDate(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.NOT_FOUND;

@AllArgsConstructor
public enum BookExceptionType implements ExceptionType {

BOOK_NOT_FOUND(NOT_FOUND, 801, "해당 책을 찾을 수 없습니다.")
BOOK_NOT_FOUND(NOT_FOUND, 801, "해당 책을 찾을 수 없습니다."),
ALREADY_EXIST_BOOK(BAD_REQUEST, 802, "이미 해당 책이 존재합니다.")
;

private final HttpStatus status;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.project.capstone.book.service;

import com.project.capstone.book.controller.AddBookRequest;
import com.project.capstone.book.domain.Book;
import com.project.capstone.book.domain.BookRepository;
import com.project.capstone.book.exception.BookException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.project.capstone.book.exception.BookExceptionType.ALREADY_EXIST_BOOK;

@Service
@RequiredArgsConstructor
@Slf4j
public class BookService {
private final BookRepository bookRepository;
public void addBook(AddBookRequest request) {
if (bookRepository.findBookByIsbn(request.isbn()).isPresent()) {
throw new BookException(ALREADY_EXIST_BOOK);
}
bookRepository.save(new Book(request));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.project.capstone.member.controller;

import com.project.capstone.auth.domain.PrincipalDetails;
import com.project.capstone.member.controller.dto.AddMyBookRequest;
import com.project.capstone.book.controller.AddBookRequest;
import com.project.capstone.member.controller.dto.MemberResponse;
import com.project.capstone.member.controller.dto.MyBookResponse;
import com.project.capstone.member.service.MemberService;
Expand Down Expand Up @@ -35,7 +35,7 @@ public ResponseEntity<List<MyBookResponse>> getMyBook(@AuthenticationPrincipal P

// 나만의 서재 추가
@PostMapping("/my-book/add")
public ResponseEntity<?> addMyBook(@AuthenticationPrincipal PrincipalDetails details, @RequestBody AddMyBookRequest request) {
public ResponseEntity<?> addMyBook(@AuthenticationPrincipal PrincipalDetails details, @RequestBody AddBookRequest request) {
memberService.addMyBook(details.getUserId(), request);
return ResponseEntity.ok().body("추가 완료");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.project.capstone.member.service;

import com.project.capstone.book.controller.AddBookRequest;
import com.project.capstone.book.domain.Book;
import com.project.capstone.book.domain.BookRepository;
import com.project.capstone.book.exception.BookException;
import com.project.capstone.book.exception.BookExceptionType;
import com.project.capstone.member.controller.dto.AddMyBookRequest;
import com.project.capstone.member.controller.dto.MemberResponse;
import com.project.capstone.member.controller.dto.MyBookResponse;
import com.project.capstone.member.domain.Member;
Expand All @@ -13,17 +11,14 @@
import com.project.capstone.mybook.domain.MyBook;
import com.project.capstone.mybook.domain.MyBookRepository;
import com.project.capstone.mybook.exception.MyBookException;
import com.project.capstone.mybook.exception.MyBookExceptionType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static com.project.capstone.book.exception.BookExceptionType.BOOK_NOT_FOUND;
import static com.project.capstone.member.exception.MemberExceptionType.MEMBER_NOT_FOUND;
import static com.project.capstone.mybook.exception.MyBookExceptionType.ALREADY_EXIST_MYBOOK;

Expand Down Expand Up @@ -54,7 +49,7 @@ public List<MyBookResponse> getMyBooks(String userId) {
return books;
}

public void addMyBook(String userId, AddMyBookRequest request) {
public void addMyBook(String userId, AddBookRequest request) {
Member member = memberRepository.findMemberById(UUID.fromString(userId)).orElseThrow(
() -> new MemberException(MEMBER_NOT_FOUND)
);
Expand Down

0 comments on commit 945521b

Please sign in to comment.