|
1 | 1 | package cmf.commitField.domain.chat.chatRoom.controller; |
2 | 2 |
|
| 3 | +import cmf.commitField.domain.File.service.FileService; |
3 | 4 | import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomRequest; |
4 | 5 | import cmf.commitField.domain.chat.chatRoom.controller.request.ChatRoomUpdateRequest; |
5 | 6 | import cmf.commitField.domain.chat.chatRoom.dto.ChatRoomDto; |
|
12 | 13 | import jakarta.validation.Valid; |
13 | 14 | import lombok.RequiredArgsConstructor; |
14 | 15 | import org.springframework.data.domain.Pageable; |
| 16 | +import org.springframework.http.MediaType; |
15 | 17 | import org.springframework.security.core.Authentication; |
16 | 18 | import org.springframework.security.core.annotation.AuthenticationPrincipal; |
17 | 19 | import org.springframework.security.core.context.SecurityContextHolder; |
18 | 20 | import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; |
19 | 21 | import org.springframework.web.bind.annotation.*; |
20 | 22 |
|
| 23 | +import java.io.IOException; |
21 | 24 | import java.util.List; |
22 | 25 |
|
23 | 26 | @RestController |
24 | 27 | @RequiredArgsConstructor |
25 | 28 | @RequestMapping("/chat") |
26 | 29 | public class ChatRoomController { |
27 | 30 | private final ChatRoomService chatRoomService; |
| 31 | + private final FileService fileService; |
28 | 32 |
|
29 | | - //채팅방 생성 |
30 | | - @PostMapping("/room") |
| 33 | + // 채팅방 생성 (파일 업로드 포함) |
| 34 | + @PostMapping(value = "/room", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) |
31 | 35 | public GlobalResponse<Object> createRoom( |
32 | | - @RequestBody @Valid ChatRoomRequest chatRoomRequest) { |
| 36 | + @ModelAttribute @Valid ChatRoomRequest chatRoomRequest) throws IOException { |
| 37 | + |
| 38 | + |
| 39 | + // 인증 확인 |
33 | 40 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
34 | 41 |
|
35 | 42 | if (authentication instanceof OAuth2AuthenticationToken) { |
36 | 43 | CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal(); |
37 | 44 | Long userId = principal.getId(); // getId()를 통해 userId를 추출 |
38 | | - chatRoomService.createRoom(chatRoomRequest, userId); // userId를 전달 |
| 45 | + |
| 46 | + // 파일 업로드 처리 |
| 47 | + String imageUrl = null; |
| 48 | + if (chatRoomRequest.getFile() != null && !chatRoomRequest.getFile().isEmpty()) { |
| 49 | + imageUrl = fileService.saveFile(chatRoomRequest.getFile()); // 파일 저장 |
| 50 | + } |
| 51 | + |
| 52 | + // 채팅방 생성 서비스 호출 (이미지 URL 포함) |
| 53 | + chatRoomService.createRoom(chatRoomRequest, userId, imageUrl); |
| 54 | + |
39 | 55 | return GlobalResponse.success("채팅방을 생성하였습니다."); |
40 | 56 | } else { |
41 | 57 | throw new IllegalArgumentException("로그인 후에 이용해 주세요."); |
|
0 commit comments