Skip to content

Commit

Permalink
Merge pull request #38 from kookmin-sw/BE_Feature/#32-member
Browse files Browse the repository at this point in the history
Be feature/#32 member
  • Loading branch information
wjdwlghks authored Apr 26, 2024
2 parents 538fef0 + 669feeb commit 82fba5f
Show file tree
Hide file tree
Showing 27 changed files with 439 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.project.capstone.auth.controller.dto;

import com.project.capstone.member.domain.Gender;

public record SignupRequest(
String email,
String name,
int age,
String gender
Gender gender
) {
}
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
Expand Up @@ -3,9 +3,11 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.project.capstone.club.domain.Club;
import com.project.capstone.club.domain.PublicStatus;
import com.project.capstone.post.controller.dto.PostResponse;
import com.project.capstone.post.domain.Post;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public record ClubResponse (
Expand All @@ -16,10 +18,19 @@ public record ClubResponse (
LocalDateTime createdAt,
int maximum,
PublicStatus publicstatus,
List<Post> posts
List<PostResponse> posts

) {
public ClubResponse(Club club) {
this(club.getId(), club.getBook() == null ? null : club.getBook().getId(), club.getTopic(), club.getName(), club.getCreatedAt(), club.getMaximum(), club.getPublicStatus(), club.getPosts());
this(club.getId(), club.getBook() == null ? null : club.getBook().getId(), club.getTopic(), club.getName(), club.getCreatedAt(), club.getMaximum(),
club.getPublicStatus(), createPostResponseList(club.getPosts()));
}

private static List<PostResponse> createPostResponseList(List<Post> postList) {
List<PostResponse> postResponseList = new ArrayList<>();
for (Post post : postList) {
postResponseList.add(new PostResponse(post));
}
return postResponseList;
}
}
12 changes: 8 additions & 4 deletions backend/src/main/java/com/project/capstone/club/domain/Club.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.project.capstone.book.domain.Book;
import com.project.capstone.club.controller.dto.ClubCreateRequest;
import com.project.capstone.memberclub.domain.MemberClub;
import com.project.capstone.content.domain.Content;
import com.project.capstone.post.domain.Post;
import com.project.capstone.quiz.domain.Quiz;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

Expand All @@ -26,6 +24,7 @@
@AllArgsConstructor
@Getter
@Builder
@ToString
public class Club {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down Expand Up @@ -62,4 +61,9 @@ public class Club {
@ManyToOne
private Book book;

public Club(ClubCreateRequest request, UUID memberId) {
this(null, memberId, request.topic(), request.name(), null, request.maximum(), request.publicStatus(), request.password(),
new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,7 @@ public List<ClubResponse> searchByName(String name) {

@Transactional
public void createClub(ClubCreateRequest request, String memberId) {
Club savedClub = clubRepository.save(Club.builder()
.managerId(UUID.fromString(memberId))
.topic(request.topic())
.name(request.name())
.maximum(request.maximum())
.publicStatus(request.publicStatus())
.password(request.password())
.build());

Club savedClub = clubRepository.save(new Club(request, UUID.fromString(memberId)));
join(memberId, savedClub.getId());
}

Expand All @@ -75,7 +67,9 @@ public void join(String memberId, Long clubId) {
if (memberClubRepository.findMemberClubByMember_IdAndClub_Id(UUID.fromString(memberId), clubId).isPresent()) {
throw new MemberClubException(ALREADY_JOIN);
}
memberClubRepository.save(new MemberClub(null, member, club));
MemberClub saved = memberClubRepository.save(new MemberClub(null, member, club));
member.getClubs().add(saved);
club.getMembers().add(saved);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
public record CommentResponse(
Long id,
Long postId,
UUID memberId,
String writer,
String body,
LocalDateTime createdAt
) {
public CommentResponse(Comment comment) {
this(comment.getId(), comment.getPost().getId(), comment.getMember().getId(), comment.getBody(), comment.getCreatedAt());
this(comment.getId(), comment.getPost().getId(), comment.getMember().getName(), comment.getBody(), comment.getCreatedAt());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public record ContentResponse(
Long id,
UUID memberId,
String writer,
Long bookId,
Long clubId,
ContentType type,
Expand All @@ -16,7 +16,7 @@ public record ContentResponse(
int likes
) {
public ContentResponse(Content content) {
this(content.getId(), content.getMember().getId(), content.getBook().getId(),
this(content.getId(), content.getMember().getName(), content.getBook().getId(),
content.getClub() == null ? null : content.getClub().getId(), content.getType(), content.getTitle(), content.getBody(), content.getLikes());
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
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;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
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
@RequiredArgsConstructor
@RequestMapping("/member")
public class MemberController {
@GetMapping("/test")
public ResponseEntity<?> test() {
return ResponseEntity.ok("ok");

private final MemberService memberService;

@GetMapping("/{id}")
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(@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
@@ -0,0 +1,15 @@
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
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.project.capstone.member.controller.dto;

import com.project.capstone.club.domain.Club;
import com.project.capstone.comment.controller.dto.CommentResponse;
import com.project.capstone.comment.domain.Comment;
import com.project.capstone.content.controller.dto.ContentResponse;
import com.project.capstone.content.domain.Content;
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;
import com.project.capstone.quiz.controller.dto.QuizResponse;
import com.project.capstone.quiz.domain.Quiz;

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

public record MemberResponse (
UUID id,
String email,
String name,
int age,
Gender gender,
LocalDateTime createdAt,
List<MemberClub> clubsList,
List<SimplePostResponse> postList,
List<CommentResponse> commentList,
List<ContentResponse> contentList,
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()), createMyBookResponseList(member.getMyBooks()));
}

private static List<SimplePostResponse> createSimplePostResponseList(List<Post> postList) {
List<SimplePostResponse> simplePostResponses = new ArrayList<>();
for (Post post: postList) {
simplePostResponses.add(new SimplePostResponse(post));
}
return simplePostResponses;
}

private static List<CommentResponse> createCommentResponseList(List<Comment> commentList) {
List<CommentResponse> commentResponseList = new ArrayList<>();
for (Comment comment: commentList) {
commentResponseList.add(new CommentResponse(comment));
}
return commentResponseList;
}

private static List<ContentResponse> createContentResponseList(List<Content> contentList) {
List<ContentResponse> contentResponseList = new ArrayList<>();
for (Content content : contentList) {
contentResponseList.add(new ContentResponse(content));
}
return contentResponseList;
}
private static List<QuizResponse> createQuizResponseList(List<Quiz> quizList) {
List<QuizResponse> quizResponseList = new ArrayList<>();
for (Quiz quiz : quizList) {
quizResponseList.add(new QuizResponse(quiz));
}
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
@@ -0,0 +1,15 @@
package com.project.capstone.member.controller.dto;

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

public record MyBookResponse(
Long id,
String isbn,
String title,
String author,
String publisher
) {
public MyBookResponse(MyBook myBook) {
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
@@ -0,0 +1,25 @@
package com.project.capstone.member.domain;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.project.capstone.quiz.domain.QuizType;
import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public enum Gender {
MALE("남자"),
FEMALE("여자")
;
private final String type;

@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
public static Gender from(String type) {
for (Gender gender: Gender.values()) {
if (gender.getType().equals(type)) {
return gender;
}
}
throw new RuntimeException("잘못된 성별 입니다.");
}
}
Loading

0 comments on commit 82fba5f

Please sign in to comment.