46 lines
1.7 KiB
Java
46 lines
1.7 KiB
Java
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<ChatDto> getMyChats(Authentication auth) {
|
||
return chatService.getUserChats(extractUserId(auth));
|
||
}
|
||
|
||
// История сообщений конкретного чата
|
||
@GetMapping("/{chatId}/messages")
|
||
public List<MessageDto> 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);
|
||
}
|
||
|
||
}
|