Skip to content

Commit

Permalink
Merge pull request #200 from PSR-Co/feat/#199-createChatMessage
Browse files Browse the repository at this point in the history
[feat] 메시지 전송 API
  • Loading branch information
sojungpp authored Nov 27, 2023
2 parents b42f1fa + 12cba85 commit eafdade
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.psr.psr.chat.controller

import com.psr.psr.chat.service.ChatRoomService
import com.psr.psr.chat.dto.request.ChatMessageReq
import com.psr.psr.chat.service.ChatService
import com.psr.psr.global.dto.BaseResponse
import com.psr.psr.global.jwt.UserAccount
import com.psr.psr.product.dto.request.CreateproductReq
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.media.Content
Expand All @@ -17,11 +17,11 @@ import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/chatRooms")
@Tag(name = "ChatRoom", description = "채팅방 API")
@RequestMapping("/chat")
@Tag(name = "Chat", description = "채팅 API")
@SecurityRequirement(name = "Bearer")
class ChatRoomController(
private val chatRoomService: ChatRoomService
class ChatController(
private val chatService: ChatService
) {

/**
Expand All @@ -37,11 +37,11 @@ class ChatRoomController(
content = arrayOf(Content(schema = Schema(implementation = BaseResponse::class)))
)]
)
@PostMapping("/{orderId}")
@PostMapping("/rooms/{orderId}")
fun createChatRoom(@AuthenticationPrincipal userAccount: UserAccount,
@Parameter(description = "(Long) 요청 id", example = "1") @PathVariable orderId: Long
): BaseResponse<Unit> {
return BaseResponse(chatRoomService.createChatRoom(userAccount.getUser(), orderId))
return BaseResponse(chatService.createChatRoom(userAccount.getUser(), orderId))
}

/**
Expand All @@ -57,11 +57,32 @@ class ChatRoomController(
content = arrayOf(Content(schema = Schema(implementation = BaseResponse::class)))
)]
)
@PatchMapping("/{chatRoomId}")
@PatchMapping("/rooms/{chatRoomId}")
fun leaveChatRoom(@AuthenticationPrincipal userAccount: UserAccount,
@Parameter(description = "(Long) 채팅방 id", example = "1") @PathVariable chatRoomId: Long
): BaseResponse<Unit> {
return BaseResponse(chatRoomService.leaveChatRoom(userAccount.getUser(), chatRoomId));
return BaseResponse(chatService.leaveChatRoom(userAccount.getUser(), chatRoomId));
}

/**
* 메시지 전송
*/
@Operation(summary = "메시지 전송(박소정)", description = "채팅방에 메시지를 전송한다.")
@ApiResponses(
value = [
ApiResponse(responseCode = "200", description = "요청에 성공했습니다."),
ApiResponse(
responseCode = "404",
description = "해당 요청을 찾을 수 없습니다.",
content = arrayOf(Content(schema = Schema(implementation = BaseResponse::class)))
)]
)
@PostMapping("/{chatRoomId}")
fun createChatMessage(@AuthenticationPrincipal userAccount: UserAccount,
@Parameter(description = "(Long) 채팅방 id", example = "1") @PathVariable chatRoomId: Long,
@RequestBody @Valid request: ChatMessageReq
): BaseResponse<Unit> {
return BaseResponse(chatService.createChatMessage(userAccount.getUser(), chatRoomId, request))
}


Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/com/psr/psr/chat/dto/request/ChatMessageReq.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.psr.psr.chat.dto.request


import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.NotBlank

data class ChatMessageReq(

@Schema(description = "메세지", example = "안녕하세요~")
@field:NotBlank(message = "메세지를 입력해주세요.")
val message: String

)

14 changes: 12 additions & 2 deletions src/main/kotlin/com/psr/psr/chat/entity/ChatMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.jetbrains.annotations.NotNull
@Entity
data class ChatMessage(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long,
var id: Long? = null,

@ManyToOne
@JoinColumn(nullable = false, name = "sender_user_id")
Expand All @@ -21,4 +21,14 @@ data class ChatMessage(
@NotNull
var message: String

) : BaseEntity()
) : BaseEntity() {
companion object {
fun toEntity(user: User, chatRoom: ChatRoom, message: String): ChatMessage {
return ChatMessage(
senderUser = user,
chatRoom = chatRoom,
message = message
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.psr.psr.chat.repository

import com.psr.psr.chat.entity.ChatMessage
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository

@Repository
interface ChatMessageRepository : JpaRepository<ChatMessage, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.psr.psr.chat.service

import com.psr.psr.chat.dto.request.ChatMessageReq
import com.psr.psr.chat.entity.ChatMessage
import com.psr.psr.chat.entity.ChatRoom
import com.psr.psr.chat.repository.ChatMessageRepository
import com.psr.psr.chat.repository.ChatRoomRepository
import com.psr.psr.global.Constant
import com.psr.psr.global.exception.BaseException
Expand All @@ -12,9 +15,10 @@ import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class ChatRoomService(
class ChatService(
private val orderRepository: OrderRepository,
private val chatRoomRepository: ChatRoomRepository
private val chatRoomRepository: ChatRoomRepository,
private val chatMessageRepository: ChatMessageRepository
) {
@Transactional
fun createChatRoom(user: User, orderId: Long) {
Expand All @@ -35,4 +39,11 @@ class ChatRoomService(
if(chatRoom.senderUser==null && chatRoom.receiverUser==null)
chatRoomRepository.delete(chatRoom)
}

@Transactional
fun createChatMessage(user: User, chatRoomId: Long, request: ChatMessageReq) {
val chatRoom: ChatRoom = chatRoomRepository.findByIdAndStatus(chatRoomId, Constant.UserStatus.ACTIVE_STATUS)
?: throw BaseException(BaseResponseCode.NOT_FOUND_CHATROOM)
chatMessageRepository.save(ChatMessage.toEntity(user, chatRoom, request.message))
}
}

0 comments on commit eafdade

Please sign in to comment.