-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Chat] 서버 전체 채팅 조회 API 구현 GET api/v1/chat #145
Changes from 18 commits
83c87d3
a824d5f
7b0d1ba
9ebca75
4c245e4
e1e949e
2eb2d98
848052a
99ec231
fa7e126
a0def5e
88be642
cb614a1
8e255cf
81ecf3c
b5fdf77
cebb203
68f158a
3967416
9127519
dba4c47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,10 @@ package kpring.chat.chat.api.v1 | |
import kpring.chat.chat.service.ChatService | ||
import kpring.core.auth.client.AuthClient | ||
import kpring.core.chat.chat.dto.request.CreateChatRequest | ||
import kpring.core.chat.chat.dto.response.ChatResponse | ||
import kpring.core.global.dto.response.ApiResponse | ||
import kpring.core.server.client.ServerClient | ||
import kpring.core.server.dto.request.GetServerCondition | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.validation.annotation.Validated | ||
import org.springframework.web.bind.annotation.* | ||
|
@@ -12,6 +16,7 @@ import org.springframework.web.bind.annotation.* | |
class ChatController( | ||
private val chatService: ChatService, | ||
val authClient: AuthClient, | ||
val serverClient: ServerClient, | ||
) { | ||
@PostMapping("/chat") | ||
fun createChat( | ||
|
@@ -23,14 +28,23 @@ class ChatController( | |
return ResponseEntity.ok().body(result) | ||
} | ||
|
||
@GetMapping("/chat/{chatRoomId}/{page}") | ||
fun getChatsByChatRoom( | ||
@PathVariable("chatRoomId") chatRoomId: String, | ||
@PathVariable("page") page: Int, | ||
@GetMapping("/chat") | ||
fun getChats( | ||
@RequestParam("type") type: String, | ||
@RequestParam("id") id: String, | ||
@RequestParam("page") page: Int, | ||
@RequestHeader("Authorization") token: String, | ||
): ResponseEntity<*> { | ||
val userId = authClient.getTokenInfo(token).data!!.userId | ||
val result = chatService.getChatsByChatRoom(chatRoomId, userId, page) | ||
return ResponseEntity.ok().body(result) | ||
var result: List<ChatResponse>? = null | ||
|
||
if (type.equals("Room")) { | ||
result = chatService.getRoomChats(id, userId, page) | ||
} else if (type.equals("Server")) { | ||
val serverList = serverClient.getServerList(token, GetServerCondition()).body!!.data!! | ||
result = chatService.getServerChats(id, userId, page, serverList) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. val result = when(type) {
"Server" -> return chatService.getRoomChats(id, userId, page)
"Room" -> return chatService.getServerList(token, ..)
}
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋네요~! |
||
|
||
return ResponseEntity.ok().body(ApiResponse(data = result, status = 200)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package kpring.chat.chat.model | ||
|
||
import kpring.chat.NoArg | ||
import kpring.chat.global.model.BaseTime | ||
import org.springframework.data.annotation.Id | ||
import org.springframework.data.mongodb.core.mapping.Document | ||
|
||
@NoArg | ||
@Document(collection = "server_chats") | ||
class ServerChat( | ||
val userId: String, | ||
val serverId: String, | ||
val content: String, | ||
) : BaseTime() { | ||
@Id | ||
var id: String? = null | ||
|
||
fun isEdited(): Boolean { | ||
return !createdAt.equals(updatedAt) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
package kpring.chat.chat.repository | ||
|
||
import kpring.chat.chat.model.Chat | ||
import kpring.chat.chat.model.RoomChat | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.data.mongodb.repository.MongoRepository | ||
import org.springframework.data.querydsl.QuerydslPredicateExecutor | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
interface ChatRepository : MongoRepository<Chat, String>, QuerydslPredicateExecutor<Chat> { | ||
interface RoomChatRepository : MongoRepository<RoomChat, String>, QuerydslPredicateExecutor<RoomChat> { | ||
fun findAllByRoomId( | ||
roomId: String, | ||
pageable: Pageable, | ||
): List<Chat> | ||
): List<RoomChat> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package kpring.chat.chat.repository | ||
|
||
import kpring.chat.chat.model.ServerChat | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.data.mongodb.repository.MongoRepository | ||
import org.springframework.data.querydsl.QuerydslPredicateExecutor | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
interface ServerChatRepository : MongoRepository<ServerChat, String>, QuerydslPredicateExecutor<ServerChat> { | ||
fun findAllByServerId( | ||
serverId: String, | ||
pageable: Pageable, | ||
): List<ServerChat> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,102 @@ | ||
package kpring.chat.chat.service | ||
|
||
import kpring.chat.chat.model.Chat | ||
import kpring.chat.chat.repository.ChatRepository | ||
import kpring.chat.chat.model.RoomChat | ||
import kpring.chat.chat.model.ServerChat | ||
import kpring.chat.chat.repository.RoomChatRepository | ||
import kpring.chat.chat.repository.ServerChatRepository | ||
import kpring.chat.chatroom.repository.ChatRoomRepository | ||
import kpring.chat.global.exception.ErrorCode | ||
import kpring.chat.global.exception.GlobalException | ||
import kpring.core.chat.chat.dto.request.CreateChatRequest | ||
import kpring.core.chat.chat.dto.response.ChatResponse | ||
import kpring.core.server.dto.ServerSimpleInfo | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.data.domain.PageRequest | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class ChatService( | ||
private val chatRepository: ChatRepository, | ||
private val roomChatRepository: RoomChatRepository, | ||
private val serverChatRepository: ServerChatRepository, | ||
private val chatRoomRepository: ChatRoomRepository, | ||
@Value("\${page.size}") val pageSize: Int = 100, | ||
) { | ||
/* | ||
business logic | ||
*/ | ||
fun createChat( | ||
request: CreateChatRequest, | ||
userId: String, | ||
) { | ||
val chat = | ||
chatRepository.save( | ||
Chat( | ||
val roomChat = | ||
roomChatRepository.save( | ||
RoomChat( | ||
userId = userId, | ||
roomId = request.room, | ||
content = request.content, | ||
), | ||
) | ||
} | ||
|
||
fun getChatsByChatRoom( | ||
fun getRoomChats( | ||
chatRoomId: String, | ||
userId: String, | ||
page: Int, | ||
): List<ChatResponse> { | ||
checkIfAuthorized(chatRoomId, userId) | ||
verifyChatRoomAccess(chatRoomId, userId) | ||
|
||
// find chats by chatRoomId and convert them into DTOs | ||
val pageable: Pageable = PageRequest.of(page, pageSize) | ||
val chats: List<Chat> = chatRepository.findAllByRoomId(chatRoomId, pageable) | ||
val roomChats: List<RoomChat> = roomChatRepository.findAllByRoomId(chatRoomId, pageable) | ||
|
||
return convertChatsToResponses(chats) | ||
return convertRoomChatsToResponses(roomChats) | ||
} | ||
|
||
fun checkIfAuthorized( | ||
fun getServerChats( | ||
serverId: String, | ||
userId: String, | ||
page: Int, | ||
servers: List<ServerSimpleInfo>, | ||
): List<ChatResponse> { | ||
verifyServerAccess(servers, serverId) | ||
|
||
val pageable: Pageable = PageRequest.of(page, pageSize) | ||
val chats: List<ServerChat> = serverChatRepository.findAllByServerId(serverId, pageable) | ||
|
||
return convertServerChatsToResponses(chats) | ||
} | ||
|
||
fun verifyServerAccess( | ||
servers: List<ServerSimpleInfo>, | ||
serverId: String, | ||
) { | ||
servers.forEach { info -> | ||
if (info.id.equals(serverId)) { | ||
return | ||
} | ||
} | ||
throw GlobalException(ErrorCode.FORBIDDEN_SERVER) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 에러 핸들러를 추가할 필요가 없다고 생각해서 공용으로 쓰고 있었습니다~ |
||
} | ||
|
||
fun verifyChatRoomAccess( | ||
chatRoomId: String, | ||
userId: String, | ||
) { | ||
// check if there is a chatroom with the chatRoomId and the user is one of the members | ||
if (!chatRoomRepository.existsByIdAndMembersContaining(chatRoomId, userId)) { | ||
throw GlobalException(ErrorCode.UNAUTHORIZED_CHATROOM) | ||
throw GlobalException(ErrorCode.FORBIDDEN_CHATROOM) | ||
} | ||
} | ||
|
||
fun convertChatsToResponses(chats: List<Chat>): List<ChatResponse> { | ||
val chatResponses = | ||
fun convertRoomChatsToResponses(roomChats: List<RoomChat>): List<ChatResponse> { | ||
val chatResponse = | ||
roomChats.map { chat -> | ||
ChatResponse(chat.id!!, chat.isEdited(), chat.createdAt.toString(), chat.content) | ||
} | ||
return chatResponse | ||
} | ||
|
||
fun convertServerChatsToResponses(chats: List<ServerChat>): List<ChatResponse> { | ||
val chatResponse = | ||
chats.map { chat -> | ||
ChatResponse(chat.roomId, chat.isEdited(), chat.createdAt, chat.content) | ||
ChatResponse(chat.id!!, chat.isEdited(), chat.createdAt.toString(), chat.content) | ||
} | ||
return chatResponses | ||
return chatResponse | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 확장된 기능이 아니라면 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋은 지적이네요~! |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package kpring.chat.global.config | ||
|
||
import kpring.core.auth.config.AuthClientConfig | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Import | ||
|
||
@Import(AuthClientConfig::class) | ||
@Configuration | ||
class AuthConfig |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package kpring.chat.global.config | ||
|
||
import kpring.core.server.config.ServerClientConfig | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Import | ||
|
||
@Import(ServerClientConfig::class) | ||
@Configuration | ||
class ServerConfig |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기는 chattype이 없네요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기도 ChatType을 받을까 하다가 requestParam 방식으로 받게 되어서 저렇게 했는데 ChatType.ROOM.toString()이라도 쓰는게 나아보이긴 하네요~! 다시 보니 createServerChat할때 만든 ChatType이 merge되지 않아서 쓸 수 없었어요..!