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 6b3cfe8 commit c6e3ea1
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.project.capstone.member.controller;

import com.project.capstone.auth.domain.PrincipalDetails;
import com.project.capstone.member.controller.dto.MemberResponse;
import com.project.capstone.member.controller.dto.MyBookResponse;
import com.project.capstone.member.service.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

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

@RestController
Expand All @@ -23,4 +24,17 @@ public ResponseEntity<MemberResponse> getMember(@PathVariable UUID id) {
MemberResponse memberResponse = memberService.getMember(id);
return ResponseEntity.ok().body(memberResponse);
}

// 나만의 서재 조회
@GetMapping("/my-book")
public ResponseEntity<List<MyBookResponse>> getMyBook(@AuthenticationPrincipal PrincipalDetails details) {
List<MyBookResponse> myBooks = memberService.getMyBooks(details.getUserId());
return ResponseEntity.ok().body(myBooks);
}

// 나만의 서재 추가
// @PostMapping("/my-book/add")
// public ResponseEntity<?> addMyBook() {
//
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.project.capstone.member.controller.dto;

public record AddMyBookRequest(

) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.project.capstone.member.controller.dto;

import com.project.capstone.mybook.domain.MyBook;

public record MyBookResponse(
Long id,
String title,
String author,
String publisher
) {
public MyBookResponse(MyBook myBook) {
this(myBook.getId(), myBook.getBook().getTitle(), myBook.getBook().getAuthor(), myBook.getBook().getPublisher());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.project.capstone.comment.domain.Comment;
import com.project.capstone.memberclub.domain.MemberClub;
import com.project.capstone.content.domain.Content;
import com.project.capstone.mybook.domain.MyBook;
import com.project.capstone.post.domain.Post;
import com.project.capstone.quiz.domain.Quiz;
import jakarta.persistence.*;
Expand Down Expand Up @@ -62,9 +63,13 @@ public class Member {
@OneToMany(mappedBy = "member")
private List<Quiz> quizzes = new ArrayList<>();

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

public Member(SignupRequest request) {
this(null, request.email(), request.name(), request.age(), request.gender(), null,
new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.project.capstone.member.service;

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 lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

import static com.project.capstone.member.exception.MemberExceptionType.MEMBER_NOT_FOUND;
Expand All @@ -17,11 +22,25 @@
@Slf4j
public class MemberService {
private final MemberRepository memberRepository;
private final MyBookRepository myBookRepository;


public MemberResponse getMember(UUID id) {
Member member = memberRepository.findMemberById(id).orElseThrow(
() -> new MemberException(MEMBER_NOT_FOUND)
);
return new MemberResponse(member);
}

public List<MyBookResponse> getMyBooks(String userId) {
Member member = memberRepository.findMemberById(UUID.fromString(userId)).orElseThrow(
() -> new MemberException(MEMBER_NOT_FOUND)
);
List<MyBook> myBooksByMember = myBookRepository.findMyBooksByMember(member);
List<MyBookResponse> books = new ArrayList<>();
for (MyBook book : myBooksByMember) {
books.add(new MyBookResponse(book));
}
return books;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.project.capstone.mybook.domain;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.project.capstone.book.domain.Book;
import com.project.capstone.member.domain.Member;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class MyBook {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@JsonBackReference
@ManyToOne
private Member member;

@JsonBackReference
@ManyToOne
private Book book;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.project.capstone.mybook.domain;

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

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

public interface MyBookRepository extends JpaRepository<MyBook, Long> {
List<MyBook> findMyBooksByMember(Member member);
}

0 comments on commit c6e3ea1

Please sign in to comment.