Skip to content

Commit

Permalink
#32 feat: 나만의 서재 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
wjdwlghks committed Apr 26, 2024
1 parent c6e3ea1 commit 669feeb
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 12 deletions.
19 changes: 18 additions & 1 deletion backend/src/main/java/com/project/capstone/book/domain/Book.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.project.capstone.book.domain;

import com.fasterxml.jackson.annotation.JsonManagedReference;
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.*;
import lombok.AllArgsConstructor;
Expand All @@ -21,6 +24,7 @@ public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String isbn;
private String title;
@Column(name = "category_1d")
private String category1d;
Expand All @@ -30,14 +34,27 @@ public class Book {
private String category3d;
private String author;
private String publisher;
private String publish_date;
@Column(name = "publish_date")
private String publishDate;

@JsonManagedReference
@OneToMany(mappedBy = "book")
private List<Club> clubs = new ArrayList<>();

@JsonManagedReference
@OneToMany(mappedBy = "book")
private List<Content> contents = new ArrayList<>();

@JsonManagedReference
@OneToMany(mappedBy = "book")
private List<Quiz> quizzes = new ArrayList<>();

@JsonManagedReference
@OneToMany(mappedBy = "book")
private List<MyBook> membersAddThisBook = new ArrayList<>();

public Book(AddMyBookRequest 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 @@ -6,4 +6,5 @@

public interface BookRepository extends JpaRepository<Book, Long> {
Optional<Book> findBookById(Long id);
Optional<Book> findBookByIsbn(String isbn);
}
Original file line number Diff line number Diff line change
@@ -1,6 +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.member.controller.dto.MemberResponse;
import com.project.capstone.member.controller.dto.MyBookResponse;
import com.project.capstone.member.service.MemberService;
Expand Down Expand Up @@ -33,8 +34,9 @@ public ResponseEntity<List<MyBookResponse>> getMyBook(@AuthenticationPrincipal P
}

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

public record AddMyBookRequest(
import java.time.LocalDateTime;

public record AddMyBookRequest (
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
Expand Up @@ -8,6 +8,7 @@
import com.project.capstone.member.domain.Gender;
import com.project.capstone.member.domain.Member;
import com.project.capstone.memberclub.domain.MemberClub;
import com.project.capstone.mybook.domain.MyBook;
import com.project.capstone.post.controller.dto.PostResponse;
import com.project.capstone.post.controller.dto.SimplePostResponse;
import com.project.capstone.post.domain.Post;
Expand All @@ -30,12 +31,13 @@ public record MemberResponse (
List<SimplePostResponse> postList,
List<CommentResponse> commentList,
List<ContentResponse> contentList,
List<QuizResponse> quizList
List<QuizResponse> quizList,
List<MyBookResponse> myBookList
) {
public MemberResponse(Member member) {
this(member.getId(), member.getEmail(), member.getName(), member.getAge(), member.getGender(), member.getCreatedAt(),
member.getClubs(), createSimplePostResponseList(member.getPosts()), createCommentResponseList(member.getComments()),
createContentResponseList(member.getContents()), createQuizResponseList(member.getQuizzes()));
createContentResponseList(member.getContents()), createQuizResponseList(member.getQuizzes()), createMyBookResponseList(member.getMyBooks()));
}

private static List<SimplePostResponse> createSimplePostResponseList(List<Post> postList) {
Expand Down Expand Up @@ -69,6 +71,11 @@ private static List<QuizResponse> createQuizResponseList(List<Quiz> quizList) {
return quizResponseList;
}



private static List<MyBookResponse> createMyBookResponseList(List<MyBook> myBookList) {
List<MyBookResponse> myBookResponseList = new ArrayList<>();
for (MyBook myBook : myBookList) {
myBookResponseList.add(new MyBookResponse(myBook));
}
return myBookResponseList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

public record MyBookResponse(
Long id,
String isbn,
String title,
String author,
String publisher
) {
public MyBookResponse(MyBook myBook) {
this(myBook.getId(), myBook.getBook().getTitle(), myBook.getBook().getAuthor(), myBook.getBook().getPublisher());
this(myBook.getId(), myBook.getBook().getIsbn(), myBook.getBook().getTitle(), myBook.getBook().getAuthor(), myBook.getBook().getPublisher());
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
package com.project.capstone.member.service;

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;
import com.project.capstone.member.domain.MemberRepository;
import com.project.capstone.member.exception.MemberException;
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;

@Service
@RequiredArgsConstructor
@Slf4j
public class MemberService {
private final MemberRepository memberRepository;
private final MyBookRepository myBookRepository;

private final BookRepository bookRepository;

public MemberResponse getMember(UUID id) {
Member member = memberRepository.findMemberById(id).orElseThrow(
Expand All @@ -43,4 +53,20 @@ public List<MyBookResponse> getMyBooks(String userId) {
}
return books;
}

public void addMyBook(String userId, AddMyBookRequest request) {
Member member = memberRepository.findMemberById(UUID.fromString(userId)).orElseThrow(
() -> new MemberException(MEMBER_NOT_FOUND)
);
Book book = bookRepository.findBookByIsbn(request.isbn()).orElseGet(
() -> bookRepository.save(new Book(request))
);
if (myBookRepository.findMyBookByMemberAndBook(member, book).isPresent()) {
throw new MyBookException(ALREADY_EXIST_MYBOOK);
}

MyBook saved = myBookRepository.save(new MyBook(null, member, book));
member.getMyBooks().add(saved);
book.getMembersAddThisBook().add(saved);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.project.capstone.mybook.domain;

import com.project.capstone.book.domain.Book;
import com.project.capstone.member.domain.Member;
import org.springframework.data.jpa.repository.JpaRepository;

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

public interface MyBookRepository extends JpaRepository<MyBook, Long> {
List<MyBook> findMyBooksByMember(Member member);
Optional<MyBook> findMyBookByMemberAndBook(Member member, Book book);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.project.capstone.mybook.exception;

import com.project.capstone.common.exception.BaseException;
import com.project.capstone.common.exception.ExceptionType;

public class MyBookException extends BaseException {
public MyBookException(ExceptionType exceptionType) {
super(exceptionType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.project.capstone.mybook.exception;

import com.project.capstone.common.exception.ExceptionType;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;

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

@AllArgsConstructor
public enum MyBookExceptionType implements ExceptionType {
ALREADY_EXIST_MYBOOK(BAD_REQUEST, 2001, "이미 나만의 서재에 등록된 책 입니다.")
;

private final HttpStatus status;
private final int exceptionCode;
private final String message;

@Override
public HttpStatus httpStatus() {
return status;
}

@Override
public int exceptionCode() {
return exceptionCode;
}

@Override
public String message() {
return message;
}
}

0 comments on commit 669feeb

Please sign in to comment.