Files
mtusi2026lab3/src/main/java/ru/mtusi/messenger/controller/ChatController.java
T

50 lines
1.7 KiB
Java

package ru.mtusi.messenger.controller;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import ru.mtusi.messenger.dto.Chat;
import ru.mtusi.messenger.dto.CreateChat;
import ru.mtusi.messenger.exception.UnauthorizedException;
import ru.mtusi.messenger.service.ChatService;
import java.util.UUID;
@RestController
@RequestMapping("/api/v1/chats")
public class ChatController {
private final ChatService chatService;
public ChatController(ChatService chatService) {
this.chatService = chatService;
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Chat createChat(Authentication auth, @Valid @RequestBody CreateChat body) {
return chatService.createChat(getUserId(auth), body);
}
@GetMapping
public Object listChats(Authentication auth) {
// Упрощенная версия без курсора для lab2, если тесты не требуют обратного
return new ChatPage(chatService.getUserChats(getUserId(auth)), null);
}
@GetMapping("/{chat_id}")
public Chat getChat(Authentication auth, @PathVariable("chat_id") UUID chatId) {
return chatService.getChat(getUserId(auth), chatId);
}
private UUID getUserId(Authentication auth) {
if (auth == null || !(auth.getPrincipal() instanceof UUID)) {
throw new UnauthorizedException("Not authenticated");
}
return (UUID) auth.getPrincipal();
}
// Заглушка для DTO, чтобы не ломать компиляцию
public record ChatPage(java.util.List<Chat> items, String nextCursor) {}
}