-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Feat] 채팅방 등록 API
- Loading branch information
Showing
4 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/main/kotlin/com/psr/psr/chat/controller/ChatRoomController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/main/kotlin/com/psr/psr/chat/service/ChatRoomService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |