Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public ResponseEntity<ApiResponse<ChatRoomResDto>> getChatRoom(@PathVariable Lon
return ApiResponse.success(SuccessStatus.SEND_LOAD_CHATROOM, chatService.getChatRoom(chatRoomId));
}

@PatchMapping("/{chatRoomId}")
public ResponseEntity<ApiResponse<Void>> readChatRoom(@PathVariable Long chatRoomId,
@AuthenticationPrincipal SecurityMember securityMember) {
Long memberId = securityMember.getId();
chatService.readChatRoom(chatRoomId, memberId);
return ApiResponse.success_only(SuccessStatus.READ_CHAT_SUCCESS);
}

@GetMapping("/my")
public ResponseEntity<ApiResponse<MyChatResDto>> getMyChatRoom(@AuthenticationPrincipal SecurityMember securityMember) {
Long memberId = securityMember.getId();
Expand All @@ -55,6 +63,5 @@ public ResponseEntity<ApiResponse<Void>> completeChat(@PathVariable Long chatRoo
Long memberId = securityMember.getId();
chatService.completeChat(chatRoomId, memberId);
return ApiResponse.success_only(SuccessStatus.COMPLETE_CHAT);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.campick.server.api.chat.entity.ChatMessage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

Expand All @@ -14,6 +15,16 @@ public interface ChatMessageRepository extends JpaRepository<ChatMessage, Long>
"ORDER BY cm.createdAt ASC")
List<ChatMessage> findMessagesByChatRoomId(@Param("chatRoomId") Long chatRoomId);

@Modifying
@Query("UPDATE ChatMessage m " +
"SET m.isRead = true " +
"WHERE m.chatRoom.id = :chatRoomId " +
"AND m.member.id <> :memberId " +
"AND m.isRead = false")
Integer markMessagesAsRead(@Param("chatRoomId") Long chatRoomId,
@Param("memberId") Long memberId);


@Query("SELECT m FROM ChatMessage m " +
"WHERE m.chatRoom.id = :chatRoomId " +
"ORDER BY m.createdAt DESC LIMIT 1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long> {
"JOIN FETCH cr.product p " +
"JOIN FETCH cr.seller s " +
"JOIN FETCH cr.buyer b " +
"WHERE s.id = :memberId OR b.id = :memberId")
"WHERE (s.id = :memberId OR b.id = :memberId) " +
"AND ((s.id = :memberId AND cr.isSellerOut = false) " +
" OR (b.id = :memberId AND cr.isBuyerOut = false))")
List<ChatRoom> findAllByMemberId(@Param("memberId") Long memberId);

Optional<ChatRoom> findByProductAndSellerAndBuyer(Product product, Member seller, Member buyer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public ChatRoomResDto getChatRoom(Long chatRoomId) {
return convertToChatRoomResDto(chatRoom, chatMessages);
}

public void readChatRoom(Long chatRoomId, Long memberId) {
Integer readMessageCount = chatMessageRepository.markMessagesAsRead(chatRoomId, memberId);
}

public MyChatResDto getMyChatRooms(Long memberId) {
List<ChatRoom> myChatRooms = chatRoomRepository.findAllByMemberId(memberId);
List<ChatListDto> chatListDtos = myChatRooms.stream().map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public enum SuccessStatus {
SEND_TOTAL_UNREAD_MSG(HttpStatus.OK, "총 안 읽은 메시지 수 조회 성공"),
COMPLETE_CHAT(HttpStatus.OK, "채팅방 종료 완료"),
UPLOAD_CHAT_IMAGE_SUCCESS(HttpStatus.OK, "채팅방 내 이미지 업로드 성공"),
READ_CHAT_SUCCESS(HttpStatus.OK, "채팅방 읽음 성공"),


/**
Expand Down