Skip to content

Commit

Permalink
Merge pull request #36 from GDSC-DGU/feat/35
Browse files Browse the repository at this point in the history
[feat] 채팅 목록 조회 API
  • Loading branch information
nykoh2001 authored Mar 3, 2024
2 parents 924d305 + 2c96f70 commit 31cfea1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/main/java/org/harang/server/controller/ChatController.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
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 @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ default Post findByIdOrThrow(Long id) {
}

List<Post> findByTitleIsContaining(String title);

List<Post> findAllByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
@Repository
public interface WaitingRepository extends JpaRepository<Waiting, Long> {
List<Waiting> findAllByPostId(Long postId);

List<Waiting> findAllByMemberId(Long memberId);
}
26 changes: 26 additions & 0 deletions src/main/java/org/harang/server/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -111,4 +115,26 @@ public List<PostResponse> getRecommendedPosts() {
.map(p -> PostResponse.of(p))
.toList();
}

public List<PostResponse> getChattings(Long memberId) {
// 사용자가 물뿌리개인 경우 - 매칭 대기 리스트의 게시글 조회
// 사용자가 새싹인 경우 - 자신이 작성한 게시글 조회

Member member = memberRepository.findByIdOrThrow(memberId);
Type type = member.getType();
List<Post> posts;

if (type.equals(Type.SPROUT)) {
// 자신이 작성한 게시글 조회
posts = postRepository.findAllByMemberId(memberId);
} else if ((type.equals(Type.WATERING))) {
// 자신이 매칭 대기 중인 게시글 조회
List<Waiting> 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();
}
}

0 comments on commit 31cfea1

Please sign in to comment.