From d9e76c5c8e0eb887d40919a46aab2b54aabdea25 Mon Sep 17 00:00:00 2001 From: Dom1046 Date: Thu, 26 Dec 2024 17:58:35 +0900 Subject: [PATCH] =?UTF-8?q?=EC=B1=84=ED=8C=85=EB=B0=A9=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/controller/ChatRoomController.java | 34 ++++++++++--------- .../dto/response/ChatRoomCreateResponse.java | 18 ++++++++++ .../dto/response/ChatRoomDeleteResponse.java | 18 ++++++++++ .../ParticipatedRoomRepository.java | 9 +++++ .../chat/service/ChatMessageService.java | 17 ++++++++-- .../domain/chat/service/ChatRoomService.java | 9 +++-- 6 files changed, 83 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/mallangs/domain/chat/dto/response/ChatRoomCreateResponse.java create mode 100644 src/main/java/com/mallangs/domain/chat/dto/response/ChatRoomDeleteResponse.java diff --git a/src/main/java/com/mallangs/domain/chat/controller/ChatRoomController.java b/src/main/java/com/mallangs/domain/chat/controller/ChatRoomController.java index 368eeee1..1648c1b0 100644 --- a/src/main/java/com/mallangs/domain/chat/controller/ChatRoomController.java +++ b/src/main/java/com/mallangs/domain/chat/controller/ChatRoomController.java @@ -1,5 +1,7 @@ package com.mallangs.domain.chat.controller; +import com.mallangs.domain.chat.dto.response.ChatRoomCreateResponse; +import com.mallangs.domain.chat.dto.response.ChatRoomDeleteResponse; import com.mallangs.domain.chat.dto.response.ChatRoomResponse; import com.mallangs.domain.chat.dto.response.ParticipatedRoomListResponse; import com.mallangs.domain.chat.service.ChatMessageService; @@ -11,6 +13,7 @@ import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.security.core.annotation.AuthenticationPrincipal; @@ -34,14 +37,13 @@ public class ChatRoomController { @Operation(summary = "채팅방 생성", description = "채팅방을 생성합니다.") @ApiResponses({ @ApiResponse(responseCode = "201", description = "채팅방 생성 성공"), - @ApiResponse(responseCode = "400", description = "잘못된 요청") + @ApiResponse(responseCode = "404", description = "회원정보를 찾을 수 없습니다.") }) - public ResponseEntity create(@PathVariable("memberId") Long memberId, - @AuthenticationPrincipal CustomMemberDetails customMemberDetails) { + public ResponseEntity create(@PathVariable("memberId") Long memberId, + @AuthenticationPrincipal CustomMemberDetails customMemberDetails) { Long myId = customMemberDetails.getMemberId(); - Long roomId = chatRoomService.create(memberId, myId); - return ResponseEntity.created(URI.create("/api/chat-room/" + roomId)).body(roomId); + return ResponseEntity.status(HttpStatus.CREATED).body(chatRoomService.create(memberId, myId)); } //채팅방리스트 조회 @@ -54,7 +56,7 @@ public ResponseEntity create(@PathVariable("memberId") Long memberId, public ResponseEntity> getList( @AuthenticationPrincipal CustomMemberDetails customMemberDetails) { log.info("채팅방 조회 memberId 입력값: {}", customMemberDetails.getMemberId()); - return ResponseEntity.ok(chatRoomService.get(customMemberDetails.getMemberId())); + return ResponseEntity.status(HttpStatus.CREATED).body(chatRoomService.get(customMemberDetails.getMemberId())); } // //채팅방 수정 @@ -74,28 +76,28 @@ public ResponseEntity> getList( @Operation(summary = "참여 채팅방 삭제", description = "참여 채팅방을 나갑니다.") @ApiResponses({ @ApiResponse(responseCode = "201", description = "삭제 성공"), - @ApiResponse(responseCode = "400", description = "참여 채팅방이 존재하지 않습니다.") + @ApiResponse(responseCode = "401", description = "참여 채팅방 삭제 권한이 없는 사용자 입니다."), + @ApiResponse(responseCode = "404", description = "참여 채팅방이 존재하지 않습니다.") }) - public ResponseEntity delete(@PathVariable("participatedRoomId") Long participatedRoomId, - Authentication authentication) { - chatRoomService.delete(authentication.getName(), participatedRoomId); - return ResponseEntity.ok().build(); + public ResponseEntity delete(@PathVariable("participatedRoomId") Long participatedRoomId, + Authentication authentication) { + return ResponseEntity.status(HttpStatus.CREATED).body(chatRoomService.delete(authentication.getName(), participatedRoomId)); } //참여 채팅방 조회 @ResponseBody - @GetMapping("/{participatedRoomId}") - @Operation(summary = "참여 채팅방 조회", description = "참여 채팅방을 조회합니다.") + @GetMapping("/{profileMemberId}") + @Operation(summary = "타인이 내 채팅방 조회", description = "타인이 내 채팅방을 조회합니다.") @ApiResponses({ @ApiResponse(responseCode = "201", description = "조회 성공"), @ApiResponse(responseCode = "400", description = "채팅방이 존재하지 않습니다.") }) - public ResponseEntity changeStatus(@PathVariable Long participatedRoomId, + public ResponseEntity changeStatus(@PathVariable Long profileMemberId, @AuthenticationPrincipal CustomMemberDetails customMemberDetails) { - String nickname = customMemberDetails.getNickname(); - return ResponseEntity.ok(chatMessageService.changeUnReadToRead(participatedRoomId, nickname)); + Long memberId = customMemberDetails.getMemberId(); + return ResponseEntity.status(HttpStatus.CREATED).body(chatMessageService.changeUnReadToRead(profileMemberId, memberId)); } } diff --git a/src/main/java/com/mallangs/domain/chat/dto/response/ChatRoomCreateResponse.java b/src/main/java/com/mallangs/domain/chat/dto/response/ChatRoomCreateResponse.java new file mode 100644 index 00000000..4841f79b --- /dev/null +++ b/src/main/java/com/mallangs/domain/chat/dto/response/ChatRoomCreateResponse.java @@ -0,0 +1,18 @@ +package com.mallangs.domain.chat.dto.response; + +import lombok.Getter; +import lombok.ToString; + +@Getter +@ToString +public class ChatRoomCreateResponse { + private String success; + private String message; + private Long chatRoomId; + + public ChatRoomCreateResponse(Long chatRoomId) { + this.success = "성공"; + this.message = "채팅 생성에 성공하였습니다."; + this.chatRoomId = chatRoomId; + } +} diff --git a/src/main/java/com/mallangs/domain/chat/dto/response/ChatRoomDeleteResponse.java b/src/main/java/com/mallangs/domain/chat/dto/response/ChatRoomDeleteResponse.java new file mode 100644 index 00000000..7a1e6dab --- /dev/null +++ b/src/main/java/com/mallangs/domain/chat/dto/response/ChatRoomDeleteResponse.java @@ -0,0 +1,18 @@ +package com.mallangs.domain.chat.dto.response; + +import lombok.Getter; +import lombok.ToString; + +@Getter +@ToString +public class ChatRoomDeleteResponse { + private String success; + private String message; + private Long participatedRoomId; + + public ChatRoomDeleteResponse(Long participatedRoomId) { + this.success = "성공"; + this.message = "참여채팅방 삭제에 성공하였습니다."; + this.participatedRoomId = participatedRoomId; + } +} diff --git a/src/main/java/com/mallangs/domain/chat/repository/ParticipatedRoomRepository.java b/src/main/java/com/mallangs/domain/chat/repository/ParticipatedRoomRepository.java index 9ccda1f0..433dd796 100644 --- a/src/main/java/com/mallangs/domain/chat/repository/ParticipatedRoomRepository.java +++ b/src/main/java/com/mallangs/domain/chat/repository/ParticipatedRoomRepository.java @@ -1,6 +1,7 @@ package com.mallangs.domain.chat.repository; import com.mallangs.domain.chat.entity.ParticipatedRoom; +import com.mallangs.domain.member.entity.embadded.Nickname; import com.mallangs.domain.member.entity.embadded.UserId; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -34,4 +35,12 @@ public interface ParticipatedRoomRepository extends JpaRepository findByPRoomId(@Param("participatedRoomId") Long participatedRoomId); + // 참여채팅방 Id로 참여채팅방 불러오기- 회원, 채팅방 조인 + @Query("SELECT DISTINCT p FROM ParticipatedRoom p" + + " LEFT JOIN FETCH p.participant m" + + " LEFT JOIN FETCH p.chatRoom c" + + " WHERE p.roomName = :myNickname" + + " AND p.participant.memberId = :otherId ") + Optional findByMyNicknameAndOtherId(@Param("myNickname") String myNickname, @Param("otherId") Long otherId); + } diff --git a/src/main/java/com/mallangs/domain/chat/service/ChatMessageService.java b/src/main/java/com/mallangs/domain/chat/service/ChatMessageService.java index abf6b2fa..3237b9a5 100644 --- a/src/main/java/com/mallangs/domain/chat/service/ChatMessageService.java +++ b/src/main/java/com/mallangs/domain/chat/service/ChatMessageService.java @@ -10,6 +10,8 @@ import com.mallangs.domain.chat.repository.ChatMessageRepository; import com.mallangs.domain.chat.repository.ParticipatedRoomRepository; import com.mallangs.domain.member.dto.PageRequestDTO; +import com.mallangs.domain.member.entity.Member; +import com.mallangs.domain.member.repository.MemberRepository; import com.mallangs.global.exception.ErrorCode; import com.mallangs.global.exception.MallangsCustomException; import lombok.RequiredArgsConstructor; @@ -20,6 +22,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.Optional; + @Log4j2 @Service @Transactional @@ -29,6 +33,7 @@ public class ChatMessageService { private final ChatMessageRepository chatMessageRepository; private final ParticipatedRoomRepository participatedRoomRepository; private final RedisSubscriber redisSubscriber; + private final MemberRepository memberRepository; //채팅 메세지 생성/송신 public ChatMessageSuccessResponse sendMessage(ChatMessageRequest chatMessageRequest) { @@ -132,10 +137,16 @@ public Page getPage(PageRequestDTO pageRequestDTO, Long } //가장 최근 읽음 처리된 메세지 부터, 찾아 읽음 처리하는 메서드 - public ChatRoomResponse changeUnReadToRead(Long participatedRoomId, String nickname) { + public ChatRoomResponse changeUnReadToRead(Long profileMemberId, Long myId) { + + Member proFileMember = memberRepository.findById(profileMemberId) + .orElseThrow(() -> new MallangsCustomException(ErrorCode.MEMBER_NOT_FOUND)); + + Member myMember = memberRepository.findById(myId) + .orElseThrow(() -> new MallangsCustomException(ErrorCode.MEMBER_NOT_FOUND)); //참여 채팅 정보 추출 - ParticipatedRoom foundPartRoom = participatedRoomRepository.findByParticipatedRoomId(participatedRoomId) + ParticipatedRoom foundPartRoom = participatedRoomRepository.findByMyNicknameAndOtherId(myMember.getNickname().getValue(), profileMemberId) .orElseThrow(() -> new MallangsCustomException(ErrorCode.PARTICIPATED_ROOM_NOT_FOUND)); ChatRoom chatRoom = foundPartRoom.getChatRoom(); @@ -143,7 +154,7 @@ public ChatRoomResponse changeUnReadToRead(Long participatedRoomId, String nickn throw new MallangsCustomException(ErrorCode.CHATROOM_NOT_FOUND); } - int numChanged = chatMessageRepository.updateRead(chatRoom.getChatRoomId(), nickname); + int numChanged = chatMessageRepository.updateRead(chatRoom.getChatRoomId(), proFileMember.getNickname().getValue()); //dto 로 변경 return ChatRoomResponse.builder() diff --git a/src/main/java/com/mallangs/domain/chat/service/ChatRoomService.java b/src/main/java/com/mallangs/domain/chat/service/ChatRoomService.java index ded4797c..d7b63803 100644 --- a/src/main/java/com/mallangs/domain/chat/service/ChatRoomService.java +++ b/src/main/java/com/mallangs/domain/chat/service/ChatRoomService.java @@ -1,6 +1,8 @@ package com.mallangs.domain.chat.service; import com.mallangs.domain.chat.dto.request.ChatRoomChangeNameRequest; +import com.mallangs.domain.chat.dto.response.ChatRoomCreateResponse; +import com.mallangs.domain.chat.dto.response.ChatRoomDeleteResponse; import com.mallangs.domain.chat.dto.response.ChatRoomResponse; import com.mallangs.domain.chat.dto.response.ParticipatedRoomListResponse; import com.mallangs.domain.chat.entity.ChatMessage; @@ -41,7 +43,7 @@ public class ChatRoomService { private final SseEmitters sseEmitters; // 채팅방 생성 - public Long create(Long myId, Long partnerId) { + public ChatRoomCreateResponse create(Long myId, Long partnerId) { //채팅방 생성 ChatRoom chatRoom = ChatRoom.builder().build(); ChatRoom savedChatRoom = chatRoomRepository.save(chatRoom); @@ -95,7 +97,7 @@ public Long create(Long myId, Long partnerId) { } } - return savedChatRoom.getChatRoomId(); + return new ChatRoomCreateResponse(savedChatRoom.getChatRoomId()); } //채팅방 리스트 조회 @@ -152,12 +154,13 @@ public boolean update(ChatRoomChangeNameRequest chatRoomChangeNameRequest) { } //채팅방 삭제 - public void delete(String userId, Long participatedRoomId) { + public ChatRoomDeleteResponse delete(String userId, Long participatedRoomId) { ParticipatedRoom partRoom = participatedRoomRepository.findByPRoomId(participatedRoomId) .orElseThrow(() -> new MallangsCustomException(ErrorCode.PARTICIPATED_ROOM_NOT_FOUND)); if (partRoom.getParticipant().getUserId().getValue().equals(userId)) { participatedRoomRepository.deleteById(participatedRoomId); + return new ChatRoomDeleteResponse(partRoom.getParticipatedRoomId()); } else { throw new MallangsCustomException(ErrorCode.FAILED_DELETE_CHATROOM); }