Skip to content

Commit

Permalink
Merge pull request #198 from PSR-Co/feat/#196-leaveChatRoom
Browse files Browse the repository at this point in the history
[feat] 채팅방 나가기 API
  • Loading branch information
sojungpp authored Nov 26, 2023
2 parents be1d964 + 7dfa40b commit b42f1fa
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
24 changes: 24 additions & 0 deletions src/main/kotlin/com/psr/psr/chat/controller/ChatRoomController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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 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 @@ -11,6 +12,7 @@ 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 jakarta.validation.Valid
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*

Expand Down Expand Up @@ -41,4 +43,26 @@ class ChatRoomController(
): BaseResponse<Unit> {
return BaseResponse(chatRoomService.createChatRoom(userAccount.getUser(), orderId))
}

/**
* 채팅방 나가기
*/
@Operation(summary = "채팅방 나가기(박소정)", description = "채팅방을 나간다.")
@ApiResponses(
value = [
ApiResponse(responseCode = "200", description = "요청에 성공했습니다."),
ApiResponse(
responseCode = "400",
description = "해당 채팅방을 찾을 수 없습니다.",
content = arrayOf(Content(schema = Schema(implementation = BaseResponse::class)))
)]
)
@PatchMapping("/{chatRoomId}")
fun leaveChatRoom(@AuthenticationPrincipal userAccount: UserAccount,
@Parameter(description = "(Long) 채팅방 id", example = "1") @PathVariable chatRoomId: Long
): BaseResponse<Unit> {
return BaseResponse(chatRoomService.leaveChatRoom(userAccount.getUser(), chatRoomId));
}


}
17 changes: 11 additions & 6 deletions src/main/kotlin/com/psr/psr/chat/entity/ChatRoom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ data class ChatRoom(
var id: Long? = null,

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

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

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

) : BaseEntity() {
fun leave(user: User) {
if (senderUser == user) senderUser = null
if (receiverUser == user) receiverUser = null
}

companion object {
fun toEntity(user: User, order: Order): ChatRoom {
return ChatRoom(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.psr.psr.chat.repository

import com.psr.psr.chat.entity.ChatRoom
import com.psr.psr.order.entity.Order
import com.psr.psr.user.entity.User
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
Expand All @@ -9,4 +10,6 @@ import org.springframework.stereotype.Repository
interface ChatRoomRepository: JpaRepository<ChatRoom, Long> {
fun deleteBySenderUser(user: User)
fun deleteByReceiverUser(user: User)
fun findByIdAndStatus(chatRoomId: Long, activeStatus: String): ChatRoom?

}
15 changes: 14 additions & 1 deletion src/main/kotlin/com/psr/psr/chat/service/ChatRoomService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ class ChatRoomService(
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));
chatRoomRepository.save(ChatRoom.toEntity(user, order))
}

@Transactional
fun leaveChatRoom(user: User, chatRoomId: Long) {
val chatRoom: ChatRoom = chatRoomRepository.findByIdAndStatus(chatRoomId, Constant.UserStatus.ACTIVE_STATUS)
?: throw BaseException(BaseResponseCode.NOT_FOUND_CHATROOM)
chatRoom.leave(user)
checkChatRoom(chatRoom)
}

private fun checkChatRoom(chatRoom: ChatRoom) {
if(chatRoom.senderUser==null && chatRoom.receiverUser==null)
chatRoomRepository.delete(chatRoom)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ enum class BaseResponseCode(status: HttpStatus, message: String) {
// product
NOT_FOUND_PRODUCT(HttpStatus.NOT_FOUND, "해당 상품을 찾을 수 없습니다."),
NULL_PRODUCT_ID(HttpStatus.BAD_REQUEST, "상품ID를 입력해주세요."),
INVALID_PRODUCT_USER(HttpStatus.BAD_REQUEST, "해당 글 작성자가 아닙니다.");
INVALID_PRODUCT_USER(HttpStatus.BAD_REQUEST, "해당 글 작성자가 아닙니다."),

// chatRoom
NOT_FOUND_CHATROOM(HttpStatus.NOT_FOUND, "해당 채팅방을 찾을 수 없습니다.");

val status: HttpStatus = status
val message: String = message
Expand Down

0 comments on commit b42f1fa

Please sign in to comment.