fixed controllers

This commit is contained in:
SlimusMinus
2026-07-31 01:08:53 +03:00
parent 74f7c88843
commit 2468c26b84
32 changed files with 536 additions and 432 deletions

View File

@@ -4,11 +4,17 @@ import com.krylov.refound.dto.*;
import com.krylov.refound.service.ChatService;
import com.krylov.refound.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/chats")
@@ -20,40 +26,46 @@ public class ChatController {
// Создать чат с пользователем или получить существующий
@PostMapping("/with/{otherUserId}")
public ChatDto createOrGetChat(@PathVariable Long otherUserId, @RequestParam(required = false) Long postId, Authentication auth) {
Long currentUserId = extractUserId(auth); // см. ниже как достать id
Long currentUserId = extractUserId(auth);
log.info("createOrGetChat currentUserId {}, otherUserId {}, postId {}", currentUserId, otherUserId, postId);
return chatService.getOrCreateChat(currentUserId, otherUserId, postId);
}
// Список всех чатов текущего пользователя
@GetMapping
public List<ChatDto> getMyChats(Authentication auth) {
return chatService.getUserChats(extractUserId(auth));
Long currentUserId = extractUserId(auth);
log.info("getMyChats for user {}", currentUserId);
return chatService.getUserChats(currentUserId);
}
// История сообщений конкретного чата
// История сообщений конкретного чата, с пагинацией
@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);
public Page<MessageDto> getMessages(@PathVariable Long chatId, @PageableDefault(size = 30, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable, Authentication auth) {
Long currentUserId = extractUserId(auth);
log.info("getMessages for chatId {} page {} by user {}", chatId, pageable.getPageNumber(), currentUserId);
return chatService.getMessages(chatId, currentUserId, pageable);
}
@DeleteMapping("/{chatId}")
public void deleteChat(@PathVariable Long chatId, Authentication auth) {
Long currentUserId = extractUserId(auth);
chatService.deleteChat(chatId);
log.info("user {} deleteChat {}", currentUserId, chatId);
chatService.deleteChat(chatId, currentUserId);
}
@PostMapping("/api/v1/chats/{chatId}/read")
public void markAsRead(@PathVariable Long chatId) {
Long userId = (Long) SecurityContextHolder.getContext()
.getAuthentication().getPrincipal(); // или твой способ
chatService.markAsRead(chatId, userId);
@PostMapping("/{chatId}/read")
public void markAsRead(@PathVariable Long chatId, Authentication auth) {
Long currentUserId = extractUserId(auth);
log.info("markAsRead chatId {} by user {}", chatId, currentUserId);
chatService.markAsRead(chatId, currentUserId);
}
private Long extractUserId(Authentication auth) {
// Подставь свою логику получения userId из JWT/Authentication
String login = auth.getName();
log.debug("extractUserId by login {}", login);
return userService.getUserIdByLogin(login);
}
}