Files
nahodka-back/src/main/java/com/krylov/refound/controller/ChatController.java
2026-06-22 01:08:47 +03:00

46 lines
1.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}