Skip to content

Commit c318803

Browse files
committed
refactor(be): 컨트롤러 분리 및 SSE 사설정 삭제
1 parent f57fc5c commit c318803

File tree

4 files changed

+55
-77
lines changed

4 files changed

+55
-77
lines changed

src/main/kotlin/com/back/koreaTravelGuide/domain/userChat/UserChatSseEvents.kt

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.back.koreaTravelGuide.domain.userChat.chatmessage.controller
2+
3+
import com.back.koreaTravelGuide.common.ApiResponse
4+
import com.back.koreaTravelGuide.domain.userChat.chatmessage.service.ChatMessageService
5+
import org.springframework.http.ResponseEntity
6+
import org.springframework.web.bind.annotation.GetMapping
7+
import org.springframework.web.bind.annotation.PathVariable
8+
import org.springframework.web.bind.annotation.PostMapping
9+
import org.springframework.web.bind.annotation.RequestBody
10+
import org.springframework.web.bind.annotation.RequestMapping
11+
import org.springframework.web.bind.annotation.RequestParam
12+
import org.springframework.web.bind.annotation.RestController
13+
14+
@RestController
15+
@RequestMapping("/api/userchat/rooms")
16+
class ChatMessageController(
17+
private val msgSvc: ChatMessageService,
18+
// private val messagingTemplate: SimpMessagingTemplate,
19+
) {
20+
@GetMapping("/{roomId}/messages")
21+
fun listMessages(
22+
@PathVariable roomId: Long,
23+
@RequestParam(required = false) after: Long?,
24+
@RequestParam(defaultValue = "50") limit: Int,
25+
): ResponseEntity<ApiResponse<Any>> {
26+
val messages =
27+
if (after == null) {
28+
msgSvc.getlistbefore(roomId, limit)
29+
} else {
30+
msgSvc.getlistafter(roomId, after)
31+
}
32+
return ResponseEntity.ok(ApiResponse(msg = "메시지 조회", data = messages))
33+
}
34+
35+
@PostMapping("/{roomId}/messages")
36+
fun sendMessage(
37+
@PathVariable roomId: Long,
38+
@RequestBody req: ChatMessageService.SendMessageReq,
39+
): ResponseEntity<ApiResponse<Any>> {
40+
val saved = msgSvc.send(roomId, req)
41+
// messagingTemplate.convertAndSend(
42+
// "/topic/userchat/$roomId",
43+
// ApiResponse(msg = "메시지 전송", data = saved),
44+
// )
45+
return ResponseEntity.status(201).body(ApiResponse(msg = "메시지 전송", data = saved))
46+
}
47+
}
Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
package com.back.koreaTravelGuide.domain.userChat.chatroom.controller
22

33
import com.back.koreaTravelGuide.common.ApiResponse
4-
import com.back.koreaTravelGuide.domain.userChat.UserChatSseEvents
5-
import com.back.koreaTravelGuide.domain.userChat.chatmessage.service.ChatMessageService
64
import com.back.koreaTravelGuide.domain.userChat.chatroom.service.ChatRoomService
7-
import org.springframework.http.MediaType
85
import org.springframework.http.ResponseEntity
96
import org.springframework.web.bind.annotation.DeleteMapping
107
import org.springframework.web.bind.annotation.GetMapping
118
import org.springframework.web.bind.annotation.PathVariable
129
import org.springframework.web.bind.annotation.PostMapping
1310
import org.springframework.web.bind.annotation.RequestBody
1411
import org.springframework.web.bind.annotation.RequestMapping
15-
import org.springframework.web.bind.annotation.RequestParam
1612
import org.springframework.web.bind.annotation.RestController
17-
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter
1813

19-
// 컨트롤러는 임시로 강사님 스타일 따라서 통합해놓았음. 추후 리팩토링 예정
2014
@RestController
2115
@RequestMapping("/api/userchat/rooms")
2216
class ChatRoomController(
2317
private val roomSvc: ChatRoomService,
24-
private val msgSvc: ChatMessageService,
25-
private val events: UserChatSseEvents,
2618
) {
2719
data class StartChatReq(val guideId: Long, val userId: Long)
2820

2921
data class DeleteChatReq(val userId: Long)
3022

31-
// MVP: 같은 페어는 방 재사용
23+
// 같은 페어는 방 재사용
3224
@PostMapping("/start")
3325
fun startChat(
3426
@RequestBody req: StartChatReq,
@@ -50,36 +42,4 @@ class ChatRoomController(
5042
fun get(
5143
@PathVariable roomId: Long,
5244
) = ResponseEntity.ok(ApiResponse(msg = "채팅방 조회", data = roomSvc.get(roomId)))
53-
54-
@GetMapping("/{roomId}/messages")
55-
fun listMessages(
56-
@PathVariable roomId: Long,
57-
@RequestParam(required = false) after: Long?,
58-
@RequestParam(defaultValue = "50") limit: Int,
59-
): ResponseEntity<ApiResponse<Any>> {
60-
val messages =
61-
if (after == null) {
62-
msgSvc.getlistbefore(roomId, limit)
63-
} else {
64-
msgSvc.getlistafter(roomId, after)
65-
}
66-
return ResponseEntity.ok(ApiResponse(msg = "메시지 조회", data = messages))
67-
}
68-
69-
@PostMapping("/{roomId}/messages")
70-
fun sendMessage(
71-
@PathVariable roomId: Long,
72-
@RequestBody req: ChatMessageService.SendMessageReq,
73-
): ResponseEntity<ApiResponse<Any>> {
74-
val saved = msgSvc.send(roomId, req)
75-
events.publishNew(roomId, saved.id!!)
76-
return ResponseEntity.status(201).body(ApiResponse(msg = "메시지 전송", data = saved))
77-
}
78-
79-
// SSE는 스트림이여서 ApiResponse로 감싸지 않았음
80-
// WebSocket,Stomp 적용되면 바로 삭제 예정
81-
@GetMapping("/{roomId}/events", produces = [MediaType.TEXT_EVENT_STREAM_VALUE])
82-
fun subscribe(
83-
@PathVariable roomId: Long,
84-
): SseEmitter = events.subscribe(roomId)
8545
}

src/main/kotlin/com/back/koreaTravelGuide/domain/userChat/chatroom/service/ChatRoomService.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.back.koreaTravelGuide.domain.userChat.chatroom.service
22

3+
import com.back.koreaTravelGuide.domain.userChat.chatmessage.repository.ChatMessageRepository
34
import com.back.koreaTravelGuide.domain.userChat.chatroom.entity.ChatRoom
45
import com.back.koreaTravelGuide.domain.userChat.chatroom.repository.ChatRoomRepository
56
import org.springframework.stereotype.Service
@@ -9,8 +10,9 @@ import java.time.Instant
910
@Service
1011
class ChatRoomService(
1112
private val roomRepository: ChatRoomRepository,
13+
private val messageRepository: ChatMessageRepository,
1214
) {
13-
data class CreateRoomReq(val title: String, val guideId: Long, val userId: Long)
15+
data class CreateRoomRequest(val title: String, val guideId: Long, val userId: Long)
1416

1517
@Transactional
1618
fun exceptOneToOneRoom(
@@ -27,7 +29,9 @@ class ChatRoomService(
2729
)
2830
}
2931

30-
fun get(roomId: Long): ChatRoom = roomRepository.findById(roomId).orElseThrow { NoSuchElementException("room not found: $roomId") }
32+
fun get(roomId: Long): ChatRoom =
33+
roomRepository.findById(roomId)
34+
.orElseThrow { NoSuchElementException("room not found: $roomId") }
3135

3236
@Transactional
3337
fun deleteByOwner(
@@ -39,6 +43,7 @@ class ChatRoomService(
3943
// 예외처리 임시
4044
throw IllegalArgumentException("채팅방 생성자만 삭제할 수 있습니다.")
4145
}
46+
messageRepository.deleteByRoomId(roomId)
4247
roomRepository.deleteById(roomId)
4348
}
4449
}

0 commit comments

Comments
 (0)