Skip to content

Commit

Permalink
MATE-107: [FEAT] 메이트 채팅방 목록 조회 시 에러 처리 로직 추가 (#96)
Browse files Browse the repository at this point in the history
* MATE-107 : [FEAT] 채팅 목록 조회 시 memberId 에러 처리 로직 추가

* MATE-107 : [FEAT] 채팅방 입장 url chat 중복 수정
  • Loading branch information
MisaSohee authored Dec 7, 2024
1 parent ee713e1 commit 88c1c15
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/example/mate/common/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum ErrorCode {
AUTH_BAD_REQUEST(HttpStatus.BAD_REQUEST, "A001", "잘못된 인증 요청입니다. 요청 파라미터를 확인해주세요."),
AUTH_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "A002", "인증에 실패했습니다. 유효한 토큰을 제공해주세요."),
AUTH_FORBIDDEN(HttpStatus.FORBIDDEN, "A003", "접근 권한이 없습니다. 권한을 확인해주세요."),
UNAUTHORIZED_USER(HttpStatus.UNAUTHORIZED, "A004", "인증되지 않은 사용자입니다"),

// Team
TEAM_NOT_FOUND(HttpStatus.NOT_FOUND, "T001", "팀을 찾을 수 없습니다"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.mate.domain.mateChat.controller;

import com.example.mate.common.error.CustomException;
import com.example.mate.common.error.ErrorCode;
import com.example.mate.common.response.ApiResponse;
import com.example.mate.common.response.PageResponse;
import com.example.mate.common.security.auth.AuthMember;
Expand Down Expand Up @@ -35,21 +37,25 @@ public ResponseEntity<ApiResponse<MateChatRoomResponse>> createOrJoinChatRoomFro
}

@GetMapping("/me")
@Operation(summary = "채팅방 목록 조회", description = "사용자의 채팅방 목록을 조회합니다.")
@Operation(summary = "채팅방 목록 조회", description = "사용자의 채팅방 목록을 조회합니다.")
public ResponseEntity<ApiResponse<PageResponse<MateChatRoomListResponse>>> getMyChatRooms(
@Parameter(description = "페이지 정보") @PageableDefault Pageable pageable,
@AuthenticationPrincipal AuthMember member

) {

if (member == null) {
throw new CustomException(ErrorCode.UNAUTHORIZED_USER);
}
PageResponse<MateChatRoomListResponse> response = chatRoomService.getMyChatRooms(member.getMemberId(), pageable);
return ResponseEntity.ok(ApiResponse.success(response));
}

@PostMapping("chat/{chatroomId}/join")
@Operation(summary = "채팅방 목록 -> 채팅방 입장", description = "채팅 목록 페이지에서 채팅방으로 입장")
@PostMapping("/{chatroomId}/join")
@Operation(summary = "채팅방 목록 -> 채팅방 입장", description = "채팅 목록 페이지에서 채팅방으로 입장")
public ResponseEntity<ApiResponse<MateChatRoomResponse>> joinExistingChatRoom(
@Parameter(description = "채팅방 ID") @PathVariable Long chatroomId,
@AuthenticationPrincipal AuthMember member
@AuthenticationPrincipal AuthMember member
) {
MateChatRoomResponse response = chatRoomService.joinExistingChatRoom(chatroomId, member.getMemberId());
return ResponseEntity.ok(ApiResponse.success(response));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ public PageResponse<MateChatMessageResponse> getChatMessages(Long roomId, Long m
// 내 채팅방 목록 조회
@Transactional(readOnly = true)
public PageResponse<MateChatRoomListResponse> getMyChatRooms(Long memberId, Pageable pageable) {
memberRepository.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND_BY_ID));

Page<MateChatRoom> chatRooms = chatRoomRepository.findActiveChatRoomsByMemberId(memberId, pageable);

List<MateChatRoomListResponse> responses = chatRooms.getContent().stream()
Expand Down Expand Up @@ -299,6 +302,9 @@ private void sendLeaveMessage(Long roomId, Member member) {
}

private void validateChatRoomAccess(Long roomId, Long memberId) {
memberRepository.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND_BY_ID));

MateChatRoomMember member = chatRoomMemberRepository.findByChatRoomIdAndMemberId(roomId, memberId)
.orElseThrow(() -> new CustomException(ErrorCode.CHAT_ROOM_MEMBER_NOT_FOUND));

Expand Down

0 comments on commit 88c1c15

Please sign in to comment.