package com.krylov.refound.controller; import com.krylov.refound.dto.*; import com.krylov.refound.service.ChatService; import com.krylov.refound.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequiredArgsConstructor @RequestMapping("/api/v1/chats") public class ChatController { private final UserService userService; private final ChatService chatService; // Создать чат с пользователем или получить существующий @PostMapping("/with/{otherUserId}") public ChatDto createOrGetChat(@PathVariable Long otherUserId, @RequestParam(required = false) Long postId, Authentication auth) { Long currentUserId = extractUserId(auth); // см. ниже как достать id return chatService.getOrCreateChat(currentUserId, otherUserId, postId); } // Список всех чатов текущего пользователя @GetMapping public List getMyChats(Authentication auth) { return chatService.getUserChats(extractUserId(auth)); } // История сообщений конкретного чата @GetMapping("/{chatId}/messages") public List getMessages(@PathVariable Long chatId) { return chatService.getMessages(chatId); } private Long extractUserId(Authentication auth) { // Подставь свою логику получения userId из JWT/Authentication // Например если в токене login, а не id - подтяни через UserService String login = auth.getName(); return userService.getUserIdByLogin(login); } }