Skip to content

Commit

Permalink
Merge pull request #22 from GDSC-DGU/feat/19
Browse files Browse the repository at this point in the history
[feat] #19 매칭 대기자 목록 조회 API
  • Loading branch information
nykoh2001 authored Feb 17, 2024
2 parents 14750c4 + 3853de8 commit 57ea450
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
4 changes: 4 additions & 0 deletions src/main/java/org/harang/server/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ public class Member {
@Column(name = "age", nullable = false)
private Long age;


/* Relation Parent Mapping */
@OneToMany(mappedBy = "member")
private List<Matching> matchingList = new ArrayList<>();

@OneToMany(mappedBy = "member")
private List<Post> postList = new ArrayList<>();

@OneToMany(mappedBy = "member")
private List<Waiting> waitingList = new ArrayList<>();

@Builder
public Member(Type type, Gender gender, Long age) {
this.type = type;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/harang/server/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public class Post {
@OneToMany(mappedBy = "post")
private List<PostCategory> postCategoryList = new ArrayList<>();

@OneToMany(mappedBy = "post")
private List<Waiting> 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;
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/org/harang/server/domain/Waiting.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/harang/server/dto/response/WaitingResponse.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions src/main/java/org/harang/server/dto/type/ErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MemberInfo, Long> {
default MemberInfo findByMemberIdOrThrow(Long memberId) {
return findByMemberId(memberId).orElseThrow(() -> new CustomException(ErrorMessage.MEMBER_INFO_NOT_FOUND));
}

Optional<MemberInfo> findByMemberId(Long memberId);
}
12 changes: 12 additions & 0 deletions src/main/java/org/harang/server/repository/WaitingRepository.java
Original file line number Diff line number Diff line change
@@ -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<Waiting, Long> {
List<Waiting> findAllByPostId(Long postId);
}
18 changes: 18 additions & 0 deletions src/main/java/org/harang/server/service/MatchingService.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -76,4 +83,15 @@ public void finishMatching(Long postId) {
postRepository.save(post);
matchingRepository.save(matching);
}

public List<WaitingResponse> getWaitingList(Long postId) {
List<Waiting> waitingList = waitingRepository.findAllByPostId(postId);

List<WaitingResponse> waitingListResponse = waitingList.stream()
.map(w -> WaitingResponse.builder()
.memberId(w.getMember().getId())
.nickname(memberInfoRepository.findByMemberIdOrThrow(w.getMember().getId()).getNickname())
.build()).toList();
return waitingListResponse;
}
}

0 comments on commit 57ea450

Please sign in to comment.