Skip to content

Commit

Permalink
#32 feat: member 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
wjdwlghks committed Apr 25, 2024
1 parent 538fef0 commit 6b3cfe8
Show file tree
Hide file tree
Showing 19 changed files with 228 additions and 36 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
) {
}
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,26 @@
package com.project.capstone.member.controller;

import com.project.capstone.member.controller.dto.MemberResponse;
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 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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.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
) {
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()));
}

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;
}



}
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("잘못된 성별 입니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.project.capstone.auth.controller.dto.SignupRequest;
import com.project.capstone.club.domain.Club;
import com.project.capstone.comment.domain.Comment;
import com.project.capstone.memberclub.domain.MemberClub;
import com.project.capstone.content.domain.Content;
Expand Down Expand Up @@ -35,7 +36,8 @@ public class Member {
private String email;
private String name;
private int age;
private String gender;
@Enumerated(EnumType.STRING)
private Gender gender;
@CreatedDate
@Column(name = "created_at")
private LocalDateTime createdAt;
Expand Down Expand Up @@ -64,4 +66,5 @@ 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<>());
}

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

import com.project.capstone.member.controller.dto.MemberResponse;
import com.project.capstone.member.domain.Member;
import com.project.capstone.member.domain.MemberRepository;
import com.project.capstone.member.exception.MemberException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.UUID;

import static com.project.capstone.member.exception.MemberExceptionType.MEMBER_NOT_FOUND;

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

public MemberResponse getMember(UUID id) {
Member member = memberRepository.findMemberById(id).orElseThrow(
() -> new MemberException(MEMBER_NOT_FOUND)
);
return new MemberResponse(member);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package com.project.capstone.memberclub.domain;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.project.capstone.club.domain.Club;
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 MemberClub {

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

@JsonBackReference
@ManyToOne
@JoinColumn(name = "member_id")
private Member member;

@JsonBackReference
@ManyToOne
@JoinColumn(name = "club_id")
private Club club;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import org.springframework.data.jpa.repository.JpaRepository;

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


public interface MemberClubRepository extends JpaRepository<MemberClub, Long> {
void deleteMemberClubByClub_IdAndMember_Id(Long clubId, UUID memberId);
Optional<MemberClub> findMemberClubByMember_IdAndClub_Id(UUID memberId, Long clubId);
List<MemberClub> findMemberClubsByMember_Id(UUID memberId);
}
Loading

0 comments on commit 6b3cfe8

Please sign in to comment.