diff --git a/src/main/java/org/harang/server/controller/ChatController.java b/src/main/java/org/harang/server/controller/ChatController.java new file mode 100644 index 0000000..ceaeae1 --- /dev/null +++ b/src/main/java/org/harang/server/controller/ChatController.java @@ -0,0 +1,24 @@ +package org.harang.server.controller; + +import lombok.RequiredArgsConstructor; +import org.harang.server.annotation.MemberId; +import org.harang.server.dto.common.ApiResponse; +import org.harang.server.service.PostService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/v1/chats") +@RequiredArgsConstructor +public class ChatController { + // 엔드포인트 고려하여 새로운 컨트롤러 생성 + + private final PostService postService; + + @GetMapping + public ApiResponse getChattings(@MemberId Long memberId) { + // 사용자가 참여한 채팅방 목록을 조회 + return ApiResponse.success(postService.getChattings(memberId)); + } +} 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 03032a7..8b4ac5e 100644 --- a/src/main/java/org/harang/server/dto/type/ErrorMessage.java +++ b/src/main/java/org/harang/server/dto/type/ErrorMessage.java @@ -50,6 +50,7 @@ public enum ErrorMessage { ONLY_SPROUT_CAN_CREATE_MATCH("50005", HttpStatus.INTERNAL_SERVER_ERROR, "새싹인 유저만 매칭을 생성할 수 있습니다."), ONLY_WATERING_CAN_HELP_SPROUT("50006", HttpStatus.INTERNAL_SERVER_ERROR, "물뿌리개인 유저만 새싹을 도와줄 수 있습니다."), CHAT_ALREADY_STARTED("50007", HttpStatus.INTERNAL_SERVER_ERROR, "이미 채팅을 시작한 게시글입니다."), + INVALID_MEMBER_TYPE("50008", HttpStatus.INTERNAL_SERVER_ERROR, "유효하지 않은 사용자 타입입니다."), ; private String code; diff --git a/src/main/java/org/harang/server/repository/PostRepository.java b/src/main/java/org/harang/server/repository/PostRepository.java index fcb5104..026a447 100644 --- a/src/main/java/org/harang/server/repository/PostRepository.java +++ b/src/main/java/org/harang/server/repository/PostRepository.java @@ -13,4 +13,6 @@ default Post findByIdOrThrow(Long id) { } List findByTitleIsContaining(String title); + + List findAllByMemberId(Long memberId); } diff --git a/src/main/java/org/harang/server/repository/WaitingRepository.java b/src/main/java/org/harang/server/repository/WaitingRepository.java index 36f55b2..fa8a6ba 100644 --- a/src/main/java/org/harang/server/repository/WaitingRepository.java +++ b/src/main/java/org/harang/server/repository/WaitingRepository.java @@ -9,4 +9,6 @@ @Repository public interface WaitingRepository extends JpaRepository { List findAllByPostId(Long postId); + + List findAllByMemberId(Long memberId); } diff --git a/src/main/java/org/harang/server/service/PostService.java b/src/main/java/org/harang/server/service/PostService.java index e266d2c..02c4ce6 100644 --- a/src/main/java/org/harang/server/service/PostService.java +++ b/src/main/java/org/harang/server/service/PostService.java @@ -5,8 +5,11 @@ import lombok.RequiredArgsConstructor; import org.harang.server.domain.*; import org.harang.server.domain.enums.Status; +import org.harang.server.domain.enums.Type; import org.harang.server.dto.request.PostRequest; import org.harang.server.dto.response.PostResponse; +import org.harang.server.dto.type.ErrorMessage; +import org.harang.server.exception.CustomException; import org.harang.server.repository.*; import org.hibernate.event.spi.PostDeleteEventListener; import org.springframework.stereotype.Service; @@ -26,6 +29,7 @@ public class PostService { private final CategoryRepository categoryRepository; private final LocationRepository locationRepository; private final PostCategoryRepository postCategoryRepository; + private final WaitingRepository waitingRepository; @Transactional public Post createPost(Long memberId, PostRequest request) { @@ -111,4 +115,26 @@ public List getRecommendedPosts() { .map(p -> PostResponse.of(p)) .toList(); } + + public List getChattings(Long memberId) { + // 사용자가 물뿌리개인 경우 - 매칭 대기 리스트의 게시글 조회 + // 사용자가 새싹인 경우 - 자신이 작성한 게시글 조회 + + Member member = memberRepository.findByIdOrThrow(memberId); + Type type = member.getType(); + List posts; + + if (type.equals(Type.SPROUT)) { + // 자신이 작성한 게시글 조회 + posts = postRepository.findAllByMemberId(memberId); + } else if ((type.equals(Type.WATERING))) { + // 자신이 매칭 대기 중인 게시글 조회 + List myWaitings = waitingRepository.findAllByMemberId(memberId); + posts = myWaitings.stream().map(w -> w.getPost()).toList(); + } else { + throw new CustomException(ErrorMessage.INVALID_MEMBER_TYPE); + } + + return posts.stream().map(p -> PostResponse.of(p)).toList(); + } }