Skip to content

Commit

Permalink
Merge pull request #202 from PSR-Co/feat/#197-getChatMessages
Browse files Browse the repository at this point in the history
[feat] 채팅 상세 조회 API
  • Loading branch information
sojungpp authored Nov 28, 2023
2 parents 2316d31 + 8a2cb89 commit 23630d3
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/kotlin/com/psr/psr/chat/controller/ChatController.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.psr.psr.chat.controller

import com.psr.psr.chat.dto.request.ChatMessageReq
import com.psr.psr.chat.dto.response.ChatMessagesRes
import com.psr.psr.chat.dto.response.GetChatMessagesRes
import com.psr.psr.chat.dto.response.GetChatRoomsRes
import com.psr.psr.chat.service.ChatService
import com.psr.psr.global.dto.BaseResponse
Expand Down Expand Up @@ -103,5 +105,25 @@ class ChatController(
return BaseResponse(chatService.getChatRooms(userAccount.getUser()));
}

/**
* 채팅 상세 조회
*/
@Operation(summary = "채팅 상세 조회(박소정)", description = "채팅방 채팅을 조회한다.")
@ApiResponses(
value = [
ApiResponse(responseCode = "200", description = "요청에 성공했습니다."),
ApiResponse(
responseCode = "404",
description = "해당 채팅방을 찾을 수 없습니다.",
content = arrayOf(Content(schema = Schema(implementation = BaseResponse::class)))
)])
@GetMapping("/{chatRoomId}")
fun getChatMessages(
@AuthenticationPrincipal userAccount: UserAccount,
@Parameter(description = "(Long) 채팅방 id", example = "1") @PathVariable chatRoomId: Long,
): BaseResponse<GetChatMessagesRes> {
return BaseResponse(chatService.getChatMessages(userAccount.getUser(), chatRoomId));
}


}
34 changes: 34 additions & 0 deletions src/main/kotlin/com/psr/psr/chat/dto/response/ChatMessageRes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.psr.psr.chat.dto.response

import com.fasterxml.jackson.annotation.JsonFormat
import com.psr.psr.chat.entity.ChatMessage
import com.psr.psr.user.entity.User
import com.querydsl.core.annotations.QueryProjection
import io.swagger.v3.oas.annotations.media.Schema
import java.time.LocalDateTime

data class ChatMessageRes @QueryProjection constructor(

@Schema(description = "본인이 보낸 메시지 여부", example = "false")
val isSend: Boolean,

@Schema(description = "메시지", example = "안녕하세요!")
val message: String,

@Schema(description = "메시지 보낸 시간", example = "11:30")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:MM")
val date: LocalDateTime,

) {
companion object {
fun toDto(user: User, chatMessage: ChatMessage): ChatMessageRes {
val isSend: Boolean = chatMessage.senderUser == user
return ChatMessageRes(
isSend,
chatMessage.message,
chatMessage.createdAt
)

}
}
}
23 changes: 23 additions & 0 deletions src/main/kotlin/com/psr/psr/chat/dto/response/ChatMessagesRes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.psr.psr.chat.dto.response

import com.fasterxml.jackson.annotation.JsonFormat
import io.swagger.v3.oas.annotations.media.Schema
import java.time.LocalDate

data class ChatMessagesRes(
@Schema(description = "최근 메시지 날짜", example = "11.30")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "YYYY년 MM월 dd일")
val date: LocalDate? = null,

@Schema(description = "메시지 리스트")
val chatMessages: List<ChatMessageRes>?
) {
companion object {
fun toDto(standardDate: LocalDate, messagesOfDate: List<ChatMessageRes>): ChatMessagesRes {
return ChatMessagesRes(
standardDate,
messagesOfDate
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.psr.psr.chat.dto.response

import io.swagger.v3.oas.annotations.media.Schema

data class GetChatMessagesRes(
@Schema(description = "채팅 시작된 게시글 이름", example = "목도리")
val productName: String? = null,

@Schema(description = "날짜별 리스트")
val chatMessagesOfDate: List<ChatMessagesRes>?
) {
companion object {
fun toDto(name: String, chatMessages: List<ChatMessagesRes>?): GetChatMessagesRes {
return GetChatMessagesRes(
name,
chatMessages
)
}

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.psr.psr.chat.repository

import com.psr.psr.chat.dto.response.GetChatRoomsRes
import com.psr.psr.chat.dto.response.ChatMessagesRes
import com.psr.psr.chat.entity.ChatRoom
import com.psr.psr.user.entity.User


interface ChatMessageCustom {
fun getRecentChat(user: User, chatRooms: List<ChatRoom>): GetChatRoomsRes?
fun getChatMessages(user: User, chatRoom: ChatRoom): List<ChatMessagesRes>?

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.psr.psr.chat.repository

import com.psr.psr.chat.dto.response.ChatMessageRes
import com.psr.psr.chat.dto.response.ChatMessagesRes
import com.psr.psr.chat.dto.response.ChatRoomRes
import com.psr.psr.chat.dto.response.GetChatRoomsRes
import com.psr.psr.chat.entity.ChatMessage
Expand All @@ -9,6 +11,7 @@ import com.psr.psr.global.Constant.UserStatus.UserStatus.ACTIVE_STATUS
import com.psr.psr.user.entity.User
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.stereotype.Component
import java.time.LocalDate

@Component
class ChatMessageRepositoryImpl(
Expand Down Expand Up @@ -51,4 +54,34 @@ class ChatMessageRepositoryImpl(
return GetChatRoomsRes.toDto(response)
}

override fun getChatMessages(user: User, chatRoom: ChatRoom): List<ChatMessagesRes>? {
val chatMessages: MutableList<ChatMessage>? = queryFactory
.selectFrom(chatMessage)
.where(
chatMessage.chatRoom.id.eq(chatRoom.id),
chatMessage.status.eq(ACTIVE_STATUS),
)
.orderBy(chatMessage.createdAt.asc())
.fetch()

val response: MutableList<ChatMessagesRes> = mutableListOf()
val messagesOfDate: MutableList<ChatMessageRes> = mutableListOf()
var standardDate: LocalDate =
if (chatMessages?.isEmpty() == true) return null
else chatMessages!![0].createdAt.toLocalDate()

for (i: Int in 0 until chatMessages.size) {
if (standardDate == chatMessages[i].createdAt.toLocalDate()) {
messagesOfDate.add(ChatMessageRes.toDto(user, chatMessages[i]))
} else {
response.add(ChatMessagesRes.toDto(standardDate, messagesOfDate.toList()))
standardDate = chatMessages[i].createdAt.toLocalDate()
messagesOfDate.clear()
messagesOfDate.add(ChatMessageRes.toDto(user, chatMessages[i]))
}
if (i == chatMessages.size-1) response.add(ChatMessagesRes.toDto(standardDate, messagesOfDate.toList()))
}
return response
}

}
9 changes: 9 additions & 0 deletions src/main/kotlin/com/psr/psr/chat/service/ChatService.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.psr.psr.chat.service

import com.psr.psr.chat.dto.request.ChatMessageReq
import com.psr.psr.chat.dto.response.ChatMessagesRes
import com.psr.psr.chat.dto.response.GetChatMessagesRes
import com.psr.psr.chat.dto.response.GetChatRoomsRes
import com.psr.psr.chat.entity.ChatMessage
import com.psr.psr.chat.entity.ChatRoom
Expand Down Expand Up @@ -53,4 +55,11 @@ class ChatService(
if (chatRooms?.isEmpty() == true) return null;
return chatMessageRepository.getRecentChat(user, chatRooms!!)
}

fun getChatMessages(user: User, chatRoomId: Long): GetChatMessagesRes {
val chatRoom: ChatRoom = chatRoomRepository.findByIdAndStatus(chatRoomId, Constant.UserStatus.ACTIVE_STATUS)
?: throw BaseException(BaseResponseCode.NOT_FOUND_CHATROOM)
val chatMessages :List<ChatMessagesRes>? = chatMessageRepository.getChatMessages(user, chatRoom)
return GetChatMessagesRes.toDto(chatRoom.order.product.name, chatMessages)
}
}

0 comments on commit 23630d3

Please sign in to comment.