diff --git a/src/main/java/org/harang/server/controller/MatchingController.java b/src/main/java/org/harang/server/controller/MatchingController.java index 9f2edc3..687fd33 100644 --- a/src/main/java/org/harang/server/controller/MatchingController.java +++ b/src/main/java/org/harang/server/controller/MatchingController.java @@ -8,6 +8,7 @@ import org.harang.server.dto.request.MatchingRequest; import org.harang.server.dto.type.SuccessMessage; import org.harang.server.service.MatchingService; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -38,4 +39,9 @@ public ApiResponse finishMatching(final @NotNull @PathVariable("postId") Long matchingService.finishMatching(postId); return ApiResponse.success(SuccessMessage.OK); } + + @GetMapping("/{postId}/waitings") + public ApiResponse getWaitinglist(final @NotNull @PathVariable("postId") Long postId) { + return ApiResponse.success(matchingService.getWaitingList(postId)); + } } diff --git a/src/main/java/org/harang/server/domain/Member.java b/src/main/java/org/harang/server/domain/Member.java index 7935225..b80a087 100644 --- a/src/main/java/org/harang/server/domain/Member.java +++ b/src/main/java/org/harang/server/domain/Member.java @@ -28,6 +28,7 @@ public class Member { @Column(name = "age", nullable = false) private Long age; + /* Relation Parent Mapping */ @OneToMany(mappedBy = "member") private List matchingList = new ArrayList<>(); @@ -35,6 +36,9 @@ public class Member { @OneToMany(mappedBy = "member") private List postList = new ArrayList<>(); + @OneToMany(mappedBy = "member") + private List waitingList = new ArrayList<>(); + @Builder public Member(Type type, Gender gender, Long age) { this.type = type; diff --git a/src/main/java/org/harang/server/domain/Post.java b/src/main/java/org/harang/server/domain/Post.java index 6d749b9..381f609 100644 --- a/src/main/java/org/harang/server/domain/Post.java +++ b/src/main/java/org/harang/server/domain/Post.java @@ -61,6 +61,9 @@ public class Post { @OneToMany(mappedBy = "post") private List postCategoryList = new ArrayList<>(); + @OneToMany(mappedBy = "post") + private List waitingList = new ArrayList<>(); + @Builder public Post(Member member, LocalDateTime createdAt, String title, String content, String chatLink, Gender preferredGender, int preferredAge, LocalDate preferredStartAt, LocalDate preferredEndAt, Status status) { this.member = member; diff --git a/src/main/java/org/harang/server/domain/Waiting.java b/src/main/java/org/harang/server/domain/Waiting.java new file mode 100644 index 0000000..f718143 --- /dev/null +++ b/src/main/java/org/harang/server/domain/Waiting.java @@ -0,0 +1,37 @@ +package org.harang.server.domain; + +import jakarta.persistence.Entity; +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 lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Waiting { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "post_id", nullable = false) + private Post post; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "member_id", nullable = false) + private Member member; + + @Builder + public Waiting(Post post, Member member) { + this.post = post; + this.member = member; + } +} diff --git a/src/main/java/org/harang/server/dto/response/WaitingResponse.java b/src/main/java/org/harang/server/dto/response/WaitingResponse.java new file mode 100644 index 0000000..8503539 --- /dev/null +++ b/src/main/java/org/harang/server/dto/response/WaitingResponse.java @@ -0,0 +1,16 @@ +package org.harang.server.dto.response; + +import lombok.Builder; +import lombok.Getter; + +@Getter +public class WaitingResponse { + private Long memberId; + private String nickname; + + @Builder + public WaitingResponse(Long memberId, String nickname) { + this.memberId = memberId; + this.nickname = nickname; + } +} diff --git a/src/main/java/org/harang/server/dto/type/ErrorMessage.java b/src/main/java/org/harang/server/dto/type/ErrorMessage.java index a3b2f7a..61ad981 100644 --- a/src/main/java/org/harang/server/dto/type/ErrorMessage.java +++ b/src/main/java/org/harang/server/dto/type/ErrorMessage.java @@ -36,6 +36,7 @@ public enum ErrorMessage { MEMBER_NOT_FOUND("40402", HttpStatus.NOT_FOUND, "사용자가 존재하지 않습니다."), POST_NOT_FOUND("40403", HttpStatus.NOT_FOUND, "게시글이 존재하지 않습니다."), MATCHING_NOT_FOUND("40404", HttpStatus.NOT_FOUND, "매칭이 존재하지 않습니다."), + MEMBER_INFO_NOT_FOUND("40405", HttpStatus.NOT_FOUND, "사용자의 정보가 존재하지 않습니다."), // method not allowed - 409 diff --git a/src/main/java/org/harang/server/repository/MemberInfoRepository.java b/src/main/java/org/harang/server/repository/MemberInfoRepository.java new file mode 100644 index 0000000..81dd863 --- /dev/null +++ b/src/main/java/org/harang/server/repository/MemberInfoRepository.java @@ -0,0 +1,17 @@ +package org.harang.server.repository; + +import java.util.Optional; +import org.harang.server.domain.MemberInfo; +import org.harang.server.dto.type.ErrorMessage; +import org.harang.server.exception.CustomException; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface MemberInfoRepository extends JpaRepository { + default MemberInfo findByMemberIdOrThrow(Long memberId) { + return findByMemberId(memberId).orElseThrow(() -> new CustomException(ErrorMessage.MEMBER_INFO_NOT_FOUND)); + } + + Optional findByMemberId(Long memberId); +} diff --git a/src/main/java/org/harang/server/repository/WaitingRepository.java b/src/main/java/org/harang/server/repository/WaitingRepository.java new file mode 100644 index 0000000..36f55b2 --- /dev/null +++ b/src/main/java/org/harang/server/repository/WaitingRepository.java @@ -0,0 +1,12 @@ +package org.harang.server.repository; + +import java.util.List; +import org.harang.server.domain.Member; +import org.harang.server.domain.Waiting; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface WaitingRepository extends JpaRepository { + List findAllByPostId(Long postId); +} diff --git a/src/main/java/org/harang/server/service/MatchingService.java b/src/main/java/org/harang/server/service/MatchingService.java index 98b6779..5090e17 100644 --- a/src/main/java/org/harang/server/service/MatchingService.java +++ b/src/main/java/org/harang/server/service/MatchingService.java @@ -1,17 +1,22 @@ package org.harang.server.service; +import java.util.List; import lombok.RequiredArgsConstructor; import org.harang.server.domain.Matching; import org.harang.server.domain.Member; import org.harang.server.domain.Post; +import org.harang.server.domain.Waiting; import org.harang.server.domain.enums.Status; import org.harang.server.domain.enums.Type; import org.harang.server.dto.request.MatchingRequest; +import org.harang.server.dto.response.WaitingResponse; import org.harang.server.dto.type.ErrorMessage; import org.harang.server.exception.CustomException; import org.harang.server.repository.MatchingRepository; +import org.harang.server.repository.MemberInfoRepository; import org.harang.server.repository.MemberRepository; import org.harang.server.repository.PostRepository; +import org.harang.server.repository.WaitingRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -21,6 +26,8 @@ public class MatchingService { private final MatchingRepository matchingRepository; private final MemberRepository memberRepository; private final PostRepository postRepository; + private final WaitingRepository waitingRepository; + private final MemberInfoRepository memberInfoRepository; @Transactional public void createMatching(Long memberId, MatchingRequest matchingRequest) { @@ -76,4 +83,15 @@ public void finishMatching(Long postId) { postRepository.save(post); matchingRepository.save(matching); } + + public List getWaitingList(Long postId) { + List waitingList = waitingRepository.findAllByPostId(postId); + + List waitingListResponse = waitingList.stream() + .map(w -> WaitingResponse.builder() + .memberId(w.getMember().getId()) + .nickname(memberInfoRepository.findByMemberIdOrThrow(w.getMember().getId()).getNickname()) + .build()).toList(); + return waitingListResponse; + } }