Skip to content

Commit

Permalink
Merge pull request #195 from PSR-Co/feat/#194-createChatRoom
Browse files Browse the repository at this point in the history
[Feat] 채팅방 등록 API
  • Loading branch information
sojungpp authored Nov 26, 2023
2 parents fec0311 + d6ced57 commit be1d964
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
44 changes: 44 additions & 0 deletions src/main/kotlin/com/psr/psr/chat/controller/ChatRoomController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.psr.psr.chat.controller

import com.psr.psr.chat.service.ChatRoomService
import com.psr.psr.global.dto.BaseResponse
import com.psr.psr.global.jwt.UserAccount
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.responses.ApiResponses
import io.swagger.v3.oas.annotations.security.SecurityRequirement
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*

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

/**
* 채팅방 등록
*/
@Operation(summary = "채팅방 등록(박소정)", description = "채팅방을 등록한다.")
@ApiResponses(
value = [
ApiResponse(responseCode = "200", description = "요청에 성공했습니다."),
ApiResponse(
responseCode = "404",
description = "해당 요청을 찾을 수 없습니다.",
content = arrayOf(Content(schema = Schema(implementation = BaseResponse::class)))
)]
)
@PostMapping("/{orderId}")
fun createChatRoom(@AuthenticationPrincipal userAccount: UserAccount,
@Parameter(description = "(Long) 요청 id", example = "1") @PathVariable orderId: Long
): BaseResponse<Unit> {
return BaseResponse(chatRoomService.createChatRoom(userAccount.getUser(), orderId))
}
}
24 changes: 24 additions & 0 deletions src/main/kotlin/com/psr/psr/chat/entity/ChatMessage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.psr.psr.chat.entity

import com.psr.psr.global.entity.BaseEntity
import com.psr.psr.user.entity.User
import jakarta.persistence.*
import org.jetbrains.annotations.NotNull

@Entity
data class ChatMessage(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long,

@ManyToOne
@JoinColumn(nullable = false, name = "sender_user_id")
var senderUser: User,

@ManyToOne
@JoinColumn(nullable = false, name = "chat_room_id")
var chatRoom: ChatRoom,

@NotNull
var message: String

) : BaseEntity()
22 changes: 19 additions & 3 deletions src/main/kotlin/com/psr/psr/chat/entity/ChatRoom.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
package com.psr.psr.chat.entity

import com.psr.psr.global.entity.BaseEntity
import com.psr.psr.order.entity.Order
import com.psr.psr.user.entity.User
import jakarta.persistence.*

@Entity
data class ChatRoom(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long,
var id: Long? = null,

@ManyToOne
@JoinColumn(nullable = false, name = "sender_user_id")
var senderUser: User,

@ManyToOne
@JoinColumn(nullable = false, name = "receiver_user_id")
var receiverUser: User
var receiverUser: User,

@OneToOne
@JoinColumn(nullable = true, name = "order_id")
var order: Order

) : BaseEntity() {
companion object {
fun toEntity(user: User, order: Order): ChatRoom {
return ChatRoom(
senderUser = user,
receiverUser = order.user,
order = order
)
}
}
}

): BaseEntity()
25 changes: 25 additions & 0 deletions src/main/kotlin/com/psr/psr/chat/service/ChatRoomService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.psr.psr.chat.service

import com.psr.psr.chat.entity.ChatRoom
import com.psr.psr.chat.repository.ChatRoomRepository
import com.psr.psr.global.Constant
import com.psr.psr.global.exception.BaseException
import com.psr.psr.global.exception.BaseResponseCode
import com.psr.psr.order.entity.Order
import com.psr.psr.order.repository.OrderRepository
import com.psr.psr.user.entity.User
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class ChatRoomService(
private val orderRepository: OrderRepository,
private val chatRoomRepository: ChatRoomRepository
) {
@Transactional
fun createChatRoom(user: User, orderId: Long) {
val order: Order = orderRepository.findByIdAndStatus(orderId, Constant.UserStatus.ACTIVE_STATUS)
?: throw BaseException(BaseResponseCode.NOT_FOUND_ORDER)
chatRoomRepository.save(ChatRoom.toEntity(user, order));
}
}

0 comments on commit be1d964

Please sign in to comment.