From 69940872b4b41158f5ea1864497c29b10a11109f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=AA=85=EB=8D=95?= <68066437+Profile-exe@users.noreply.github.com> Date: Thu, 25 Jul 2024 19:07:41 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20=EB=A7=A4=EC=B9=AD=20=EC=83=81?= =?UTF-8?q?=ED=83=9C=20=EB=B3=80=ED=99=94=EC=97=90=20=EB=94=B0=EB=A5=B8=20?= =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EC=83=81=ED=83=9C=20=EB=B3=80?= =?UTF-8?q?=ED=99=94=20(#101)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: 엔티티, DTO 변환 로직을 DTO 내부로 이동 * refactor: 게시글 상태 변경을 위한 메서드 접근지정자 변경 * refactor: Schedule 클래스 Builder 적용 * feat: 매칭 상태 변경 시 게시글 상태도 변경하도록 구현 --- .../buddybridge/matching/entity/Matching.java | 11 +++ .../buddybridge/member/dto/MemberResDto.java | 14 ++++ .../buddybridge/post/dto/PostReqDto.java | 30 +++++++- .../buddybridge/post/dto/PostResDto.java | 14 ++-- .../econo/buddybridge/post/entity/Post.java | 41 +++++----- .../buddybridge/post/entity/Schedule.java | 1 + .../buddybridge/post/service/PostService.java | 76 +------------------ 7 files changed, 84 insertions(+), 103 deletions(-) diff --git a/src/main/java/econo/buddybridge/matching/entity/Matching.java b/src/main/java/econo/buddybridge/matching/entity/Matching.java index 6a84d65..ed8eb26 100644 --- a/src/main/java/econo/buddybridge/matching/entity/Matching.java +++ b/src/main/java/econo/buddybridge/matching/entity/Matching.java @@ -3,6 +3,7 @@ import econo.buddybridge.common.persistence.BaseEntity; import econo.buddybridge.member.entity.Member; import econo.buddybridge.post.entity.Post; +import econo.buddybridge.post.entity.PostStatus; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Builder; @@ -47,5 +48,15 @@ public Matching(Post post, Member taker, Member giver, MatchingStatus matchingSt // 매칭 상태 변경 public void updateMatching(MatchingStatus matchingStatus){ this.matchingStatus = matchingStatus; + switch (matchingStatus){ + case PENDING: + post.changeStatus(PostStatus.RECRUITING); + case DONE: + post.changeStatus(PostStatus.FINISHED); + break; + case FAILED: + post.changeStatus(PostStatus.RECRUITING); + break; + } } } diff --git a/src/main/java/econo/buddybridge/member/dto/MemberResDto.java b/src/main/java/econo/buddybridge/member/dto/MemberResDto.java index 070dc42..b2d62ef 100644 --- a/src/main/java/econo/buddybridge/member/dto/MemberResDto.java +++ b/src/main/java/econo/buddybridge/member/dto/MemberResDto.java @@ -2,6 +2,7 @@ import econo.buddybridge.member.entity.DisabilityType; import econo.buddybridge.member.entity.Gender; +import econo.buddybridge.member.entity.Member; import lombok.Builder; @Builder @@ -15,4 +16,17 @@ public record MemberResDto( Gender gender, DisabilityType disabilityType ) { + + public MemberResDto(Member member) { + this( + member.getId(), + member.getName(), + member.getNickname(), + member.getProfileImageUrl(), + member.getEmail(), + member.getAge(), + member.getGender(), + member.getDisabilityType() + ); + } } \ No newline at end of file diff --git a/src/main/java/econo/buddybridge/post/dto/PostReqDto.java b/src/main/java/econo/buddybridge/post/dto/PostReqDto.java index aec2c13..f8c8701 100644 --- a/src/main/java/econo/buddybridge/post/dto/PostReqDto.java +++ b/src/main/java/econo/buddybridge/post/dto/PostReqDto.java @@ -1,9 +1,15 @@ package econo.buddybridge.post.dto; -import econo.buddybridge.post.entity.*; -import lombok.Builder; - +import econo.buddybridge.member.entity.Member; +import econo.buddybridge.post.entity.AssistanceType; +import econo.buddybridge.post.entity.District; +import econo.buddybridge.post.entity.Post; +import econo.buddybridge.post.entity.PostStatus; +import econo.buddybridge.post.entity.PostType; +import econo.buddybridge.post.entity.Schedule; +import econo.buddybridge.post.entity.ScheduleType; import java.time.LocalDateTime; +import lombok.Builder; @Builder public record PostReqDto( @@ -19,4 +25,22 @@ public record PostReqDto( PostType postType, PostStatus postStatus ) { + + public Post toEntity(Member author) { + return Post.builder() + .author(author) + .title(title) + .assistanceType(assistanceType) + .schedule(Schedule.builder() + .startTime(startTime) + .endTime(endTime) + .scheduleType(scheduleType) + .scheduleDetails(scheduleDetails) + .build()) + .district(district) + .content(content) + .postType(postType) + .postStatus(postStatus) + .build(); + } } diff --git a/src/main/java/econo/buddybridge/post/dto/PostResDto.java b/src/main/java/econo/buddybridge/post/dto/PostResDto.java index a7c964a..3ba8a39 100644 --- a/src/main/java/econo/buddybridge/post/dto/PostResDto.java +++ b/src/main/java/econo/buddybridge/post/dto/PostResDto.java @@ -2,11 +2,14 @@ import econo.buddybridge.member.dto.MemberResDto; import econo.buddybridge.member.entity.DisabilityType; -import econo.buddybridge.post.entity.*; -import lombok.Builder; -import econo.buddybridge.post.service.PostService; - +import econo.buddybridge.post.entity.AssistanceType; +import econo.buddybridge.post.entity.District; +import econo.buddybridge.post.entity.Post; +import econo.buddybridge.post.entity.PostStatus; +import econo.buddybridge.post.entity.PostType; +import econo.buddybridge.post.entity.ScheduleType; import java.time.LocalDateTime; +import lombok.Builder; @Builder public record PostResDto( @@ -30,7 +33,7 @@ public record PostResDto( public PostResDto(Post post) { this( post.getId(), - PostService.toMemberResDto(post.getAuthor()), + new MemberResDto(post.getAuthor()), post.getTitle(), post.getAssistanceType(), post.getSchedule().getStartTime(), @@ -46,5 +49,4 @@ public PostResDto(Post post) { post.getDisabilityType() ); } - } diff --git a/src/main/java/econo/buddybridge/post/entity/Post.java b/src/main/java/econo/buddybridge/post/entity/Post.java index c7fcf80..4dad67c 100644 --- a/src/main/java/econo/buddybridge/post/entity/Post.java +++ b/src/main/java/econo/buddybridge/post/entity/Post.java @@ -5,9 +5,23 @@ import econo.buddybridge.member.entity.DisabilityType; import econo.buddybridge.member.entity.Member; import econo.buddybridge.post.dto.PostReqDto; -import jakarta.persistence.*; +import jakarta.persistence.Column; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.EntityListeners; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; import lombok.AccessLevel; +import lombok.AllArgsConstructor; import lombok.Builder; +import lombok.Builder.Default; import lombok.Getter; import lombok.NoArgsConstructor; import org.springframework.data.jpa.domain.support.AuditingEntityListener; @@ -16,6 +30,8 @@ @Getter @Table(name = "POST") @NoArgsConstructor(access = AccessLevel.PROTECTED) +@Builder +@AllArgsConstructor @EntityListeners(value = AuditingEntityListener.class) public class Post extends BaseEntity { @Id @@ -23,7 +39,6 @@ public class Post extends BaseEntity { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; -// @ManyToOne(cascade = CascadeType.REMOVE) @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "author_id") private Member author; @@ -45,12 +60,14 @@ public class Post extends BaseEntity { @Enumerated(EnumType.STRING) private PostType postType; - private PostStatus postStatus; // 모집 중, 모집 완료 + @Default + @Enumerated(EnumType.STRING) + private PostStatus postStatus = PostStatus.RECRUITING; // 모집 중, 모집 완료 @Enumerated(EnumType.STRING) private DisabilityType disabilityType; - protected void setStatus(PostStatus status){ // 상태 변경 + public void changeStatus(PostStatus status){ // 상태 변경 this.postStatus = status; } @@ -65,20 +82,4 @@ public void updatePost(PostReqDto postReqDto){ this.content = postReqDto.content(); this.postType = postReqDto.postType(); } - - @Builder - public Post(Member author, String title, AssistanceType assistanceType, - Schedule schedule, District district, - String content, PostType postType - ) { - this.author = author; - this.title = title; - this.assistanceType = assistanceType; - this.schedule = schedule; - this.district = district; - this.content = content; - this.postType = postType; - this.postStatus = PostStatus.RECRUITING; - this.disabilityType = author.getDisabilityType(); - } } diff --git a/src/main/java/econo/buddybridge/post/entity/Schedule.java b/src/main/java/econo/buddybridge/post/entity/Schedule.java index a8a54ac..40aae94 100644 --- a/src/main/java/econo/buddybridge/post/entity/Schedule.java +++ b/src/main/java/econo/buddybridge/post/entity/Schedule.java @@ -10,6 +10,7 @@ @Embeddable @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) +@Builder @AllArgsConstructor @EqualsAndHashCode public class Schedule { diff --git a/src/main/java/econo/buddybridge/post/service/PostService.java b/src/main/java/econo/buddybridge/post/service/PostService.java index d5300ef..44deebc 100644 --- a/src/main/java/econo/buddybridge/post/service/PostService.java +++ b/src/main/java/econo/buddybridge/post/service/PostService.java @@ -1,9 +1,6 @@ package econo.buddybridge.post.service; -import econo.buddybridge.matching.entity.Matching; -import econo.buddybridge.matching.entity.MatchingStatus; import econo.buddybridge.matching.repository.MatchingRepository; -import econo.buddybridge.member.dto.MemberResDto; import econo.buddybridge.member.entity.Member; import econo.buddybridge.member.repository.MemberRepository; import econo.buddybridge.post.dto.PostCustomPage; @@ -12,12 +9,8 @@ import econo.buddybridge.post.entity.Post; import econo.buddybridge.post.entity.PostStatus; import econo.buddybridge.post.entity.PostType; -import econo.buddybridge.post.entity.Schedule; -import econo.buddybridge.post.entity.ScheduleType; import econo.buddybridge.post.repository.PostRepository; import econo.buddybridge.post.repository.PostRepositoryCustom; -import java.time.LocalDateTime; -import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -36,7 +29,7 @@ public class PostService { public PostResDto findPost(Long postId) { Post post = postRepository.findById(postId) .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 게시글입니다.")); - return postToPostRes(post); + return new PostResDto(post); } @Transactional(readOnly = true) @@ -50,8 +43,7 @@ public Long createPost(PostReqDto postReqDto, Long memberId) { Member member = memberRepository.findById(memberId) .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 회원입니다.")); - Post post = postReqToPost(postReqDto, member); - + Post post = postReqDto.toEntity(member); return postRepository.save(post).getId(); } @@ -78,68 +70,4 @@ public void deletePost(Long postId, Long memberId) { } postRepository.deleteById(postId); } - - // Post를 사용하여 PostResDto 생성 - public PostResDto postToPostRes(Post post) { - List matchingList = matchingRepository.findByPostId(post.getId()); - PostStatus postStatus = determinePostStatus(matchingList); - - return PostResDto.builder() - .id(post.getId()) - .author(toMemberResDto(post.getAuthor())) - .title(post.getTitle()) - .assistanceType(post.getAssistanceType()) - .startTime(post.getSchedule().getStartTime()) - .endTime(post.getSchedule().getEndTime()) - .scheduleType(post.getSchedule().getScheduleType()) - .scheduleDetails(post.getSchedule().getScheduleDetails()) - .district(post.getDistrict()) - .content(post.getContent()) - .postType(post.getPostType()) - .createdAt(post.getCreatedAt()) - .modifiedAt(post.getModifiedAt()) - .postStatus(postStatus) - .disabilityType(post.getDisabilityType()) - .build(); - } - - // 매칭 상태에 따라 게시글 상태 결정 - public PostStatus determinePostStatus(List matchingList) { - if (matchingList.isEmpty()) { - return PostStatus.RECRUITING; // 모집중 - } - - boolean isCompleted = matchingList.stream() - .anyMatch(matching -> matching.getMatchingStatus() == MatchingStatus.DONE); - - return isCompleted ? PostStatus.FINISHED : PostStatus.RECRUITING; - } - - // PostReqDto를 바탕으로 Post생성 - public Post postReqToPost(PostReqDto postReqDto, Member member) { - LocalDateTime startTime = postReqDto.startTime(); - LocalDateTime endTime = postReqDto.endTime(); - ScheduleType scheduleType = postReqDto.scheduleType(); - String scheduleDetails = postReqDto.scheduleDetails(); - - Schedule schedule = new Schedule(startTime, endTime, scheduleType, scheduleDetails); - - return new Post(member, postReqDto.title(), postReqDto.assistanceType(), schedule, postReqDto.district(), - postReqDto.content(), postReqDto.postType()); - } - - - // 지연 로딩 시 생성되는 프록시 맴버 객체를 멤버 DTO로 변환 - public static MemberResDto toMemberResDto(Member member) { - return MemberResDto.builder() - .memberId(member.getId()) - .name(member.getName()) - .nickname(member.getNickname()) - .profileImageUrl(member.getProfileImageUrl()) - .email(member.getEmail()) - .age(member.getAge()) - .gender(member.getGender()) - .disabilityType(member.getDisabilityType()) - .build(); - } }