Skip to content

Commit

Permalink
[FEAT] 매칭 상태 변화에 따른 게시글 상태 변화 (#101)
Browse files Browse the repository at this point in the history
* refactor: 엔티티, DTO 변환 로직을 DTO 내부로 이동

* refactor: 게시글 상태 변경을 위한 메서드 접근지정자 변경

* refactor: Schedule 클래스 Builder 적용

* feat: 매칭 상태 변경 시 게시글 상태도 변경하도록 구현
  • Loading branch information
Profile-exe authored Jul 25, 2024
1 parent 563b5d3 commit 6994087
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 103 deletions.
11 changes: 11 additions & 0 deletions src/main/java/econo/buddybridge/matching/entity/Matching.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/econo/buddybridge/member/dto/MemberResDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
);
}
}
30 changes: 27 additions & 3 deletions src/main/java/econo/buddybridge/post/dto/PostReqDto.java
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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();
}
}
14 changes: 8 additions & 6 deletions src/main/java/econo/buddybridge/post/dto/PostResDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(),
Expand All @@ -46,5 +49,4 @@ public PostResDto(Post post) {
post.getDisabilityType()
);
}

}
41 changes: 21 additions & 20 deletions src/main/java/econo/buddybridge/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,14 +30,15 @@
@Getter
@Table(name = "POST")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@EntityListeners(value = AuditingEntityListener.class)
public class Post extends BaseEntity {
@Id
@Column(name = "post_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

// @ManyToOne(cascade = CascadeType.REMOVE)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
private Member author;
Expand All @@ -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;
}

Expand All @@ -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();
}
}
1 change: 1 addition & 0 deletions src/main/java/econo/buddybridge/post/entity/Schedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@Embeddable
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@EqualsAndHashCode
public class Schedule {
Expand Down
76 changes: 2 additions & 74 deletions src/main/java/econo/buddybridge/post/service/PostService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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();
}

Expand All @@ -78,68 +70,4 @@ public void deletePost(Long postId, Long memberId) {
}
postRepository.deleteById(postId);
}

// Post를 사용하여 PostResDto 생성
public PostResDto postToPostRes(Post post) {
List<Matching> 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<Matching> 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();
}
}

0 comments on commit 6994087

Please sign in to comment.