|
| 1 | +package cmf.commitField.domain.chat.chatMessage.controller; |
| 2 | + |
| 3 | +import cmf.commitField.domain.chat.chatMessage.controller.request.ChatMsgRequest; |
| 4 | +import cmf.commitField.domain.chat.chatMessage.controller.response.ChatMsgResponse; |
| 5 | +import cmf.commitField.domain.chat.chatMessage.dto.ChatMsgDto; |
| 6 | +import cmf.commitField.domain.chat.chatMessage.service.ChatMessageService; |
| 7 | +import cmf.commitField.domain.user.entity.CustomOAuth2User; |
| 8 | +import cmf.commitField.global.error.ErrorCode; |
| 9 | +import cmf.commitField.global.globalDto.GlobalResponse; |
| 10 | +import cmf.commitField.global.security.LoginCheck; |
| 11 | +import jakarta.validation.Valid; |
| 12 | +import lombok.RequiredArgsConstructor; |
| 13 | +import org.springframework.security.core.Authentication; |
| 14 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 15 | +import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; |
| 16 | +import org.springframework.web.bind.annotation.*; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +@RestController |
| 21 | +@RequiredArgsConstructor |
| 22 | +@RequestMapping("/chat") |
| 23 | +public class ChatMessageController { |
| 24 | + |
| 25 | + private final ChatMessageService chatMessageService; |
| 26 | + |
| 27 | + @PostMapping("/msg/{roomId}") |
| 28 | + @LoginCheck |
| 29 | + public GlobalResponse<Object> sendChat( |
| 30 | + @PathVariable Long roomId, |
| 31 | + @RequestBody @Valid ChatMsgRequest message) { |
| 32 | + |
| 33 | + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 34 | + if (!(authentication instanceof OAuth2AuthenticationToken)) { |
| 35 | + return GlobalResponse.error(ErrorCode.NOT_AUTHENTICATED); |
| 36 | + } |
| 37 | + |
| 38 | + CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal(); |
| 39 | + Long userId = principal.getId(); // OAuth2 ๋ก๊ทธ์ธํ ์ ์ ์ ID ๊ฐ์ ธ์ค๊ธฐ |
| 40 | + |
| 41 | + if (message == null || message.getMessage().trim().isEmpty()) { |
| 42 | + return GlobalResponse.error(ErrorCode.EMPTY_MESSAGE); |
| 43 | + } |
| 44 | + |
| 45 | + ChatMsgResponse response = chatMessageService.sendMessage(message, userId, roomId); |
| 46 | + return GlobalResponse.success("์ฑํ
๋ฉ์์ง ๋ณด๋ด๊ธฐ ์ฑ๊ณต", response); |
| 47 | + } |
| 48 | + |
| 49 | + @GetMapping("/msg/{roomId}") |
| 50 | + @LoginCheck |
| 51 | + public GlobalResponse<Object> getChatList( |
| 52 | + @PathVariable Long roomId, |
| 53 | + @RequestParam(required = false) Long lastId) { |
| 54 | + |
| 55 | + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 56 | + if (!(authentication instanceof OAuth2AuthenticationToken)) { |
| 57 | + return GlobalResponse.error(ErrorCode.NOT_AUTHENTICATED); |
| 58 | + } |
| 59 | + |
| 60 | + CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal(); |
| 61 | + Long userId = principal.getId(); // OAuth2 ๋ก๊ทธ์ธํ ์ ์ ์ ID ๊ฐ์ ธ์ค๊ธฐ |
| 62 | + |
| 63 | + List<ChatMsgDto> roomChatMsgList = chatMessageService.getRoomChatMsgList(roomId, userId, lastId); |
| 64 | + if (roomChatMsgList == null || roomChatMsgList.isEmpty()) { |
| 65 | + return GlobalResponse.error(ErrorCode.CHAT_NOT_FOUND); |
| 66 | + } |
| 67 | + |
| 68 | + return GlobalResponse.success("ํด๋น ์ฑํ
๋ฐฉ์ ๋ฉ์์ง๋ค์ ์กฐํํ์์ต๋๋ค.", roomChatMsgList); |
| 69 | + } |
| 70 | +} |
| 71 | + |
0 commit comments