fix list chat

This commit is contained in:
2026-02-28 02:41:37 +01:00
parent 19c91c09dc
commit 6da76dccf0
6 changed files with 34 additions and 27 deletions

View File

@@ -22,24 +22,24 @@ public class ChatController {
private final ChatService chatService;
private final EventPublisher eventPublisher;
private final ApiUtils apiUtils;
@GetMapping("")
public ResponseEntity<List<Chat>> mainPage() {
log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName());
List<Chat> response = chatService.getAllChats();
Long ownerId = apiUtils.getUserIdFromAuthentication().longValue();
List<Chat> response = chatService.getAllChatsByOwner(ownerId);
return ResponseEntity.ok(response);
}
@GetMapping("/{chatId}")
public ResponseEntity<Chat> showChat(@PathVariable Long chatId) {
log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName());
Chat response = chatService.getChat(chatId);
Long ownerId = apiUtils.getUserIdFromAuthentication().longValue();
Chat response = chatService.getChat(chatId, ownerId);
return ResponseEntity.ok(response);
}
@PostMapping("/new")
public ResponseEntity<Chat> newChat(@RequestParam String title) {
log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName());
Chat chat = chatService.createNewChat(title);
eventPublisher.publishChatCreated(
@@ -51,9 +51,9 @@ public class ChatController {
@DeleteMapping("/{chatId}")
public ResponseEntity<Void> deleteChat(@PathVariable Long chatId) {
log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName());
Chat chat = chatService.getChat(chatId);
chatService.deleteChat(chatId);
Long ownerId = apiUtils.getUserIdFromAuthentication().longValue();
Chat chat = chatService.getChat(chatId, ownerId);
chatService.deleteChat(chatId, ownerId);
eventPublisher.publishChatDeleted(
chat.getIdOwner().toString(),

View File

@@ -26,6 +26,7 @@ public class ChatEntryController {
private final ChatService chatService;
private final RagDefaultsProperties ragDefaults;
private final EventPublisher eventPublisher;
private final ApiUtils apiUtils;
@PostMapping("/{chatId}")
public ResponseEntity<ChatEntry> addUserEntry(
@@ -33,10 +34,12 @@ public class ChatEntryController {
@RequestBody UserEntryRequest request) {
log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName());
Long ownerId = apiUtils.getUserIdFromAuthentication().longValue();
boolean onlyContext = request.onlyContext() != null ? request.onlyContext() : ragDefaults.onlyContext();
double topP = request.topP() != null ? request.topP() : ragDefaults.topP();
Chat chat = chatService.getChat(chatId);
Chat chat = chatService.getChat(chatId, ownerId);
ChatEntry entry = chatEntryService.addUserEntry(chatId, request.content(), onlyContext, topP, chat.getIdOwner());
eventPublisher.publishQuerySent(

View File

@@ -3,7 +3,9 @@ package com.balex.rag.repo;
import com.balex.rag.model.entity.Chat;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ChatRepository extends JpaRepository<Chat, Long> {
import java.util.List;
public interface ChatRepository extends JpaRepository<Chat, Long> {
List<Chat> findByIdOwnerOrderByCreatedAtDesc(Long idOwner);
}

View File

@@ -9,12 +9,11 @@ public interface ChatService {
Chat createNewChat(String title);
List<Chat> getAllChats();
List<Chat> getAllChatsByOwner(Long ownerId);
Chat getChat(Long chatId);
Chat getChat(Long chatId, Long ownerId);
void deleteChat(Long chatId);
void deleteChat(Long chatId, Long ownerId);
SseEmitter proceedInteractionWithStreaming(Long chatId, String userPrompt);
}

View File

@@ -10,7 +10,9 @@ import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.util.List;
@@ -20,13 +22,11 @@ import java.util.List;
public class ChatServiceImpl implements ChatService {
private final ChatRepository chatRepo;
private final ChatClient chatClient;
private final ApiUtils apiUtils;
public List<Chat> getAllChats() {
return chatRepo.findAll(Sort.by(Sort.Direction.DESC, "createdAt"));
public List<Chat> getAllChatsByOwner(Long ownerId) {
return chatRepo.findByIdOwnerOrderByCreatedAtDesc(ownerId);
}
public Chat createNewChat(String title) {
@@ -39,17 +39,21 @@ public class ChatServiceImpl implements ChatService {
return chat;
}
public Chat getChat(Long chatId) {
return chatRepo.findById(chatId).orElseThrow();
public Chat getChat(Long chatId, Long ownerId) {
Chat chat = chatRepo.findById(chatId).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found"));
if (!chat.getIdOwner().equals(ownerId)) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Access denied");
}
return chat;
}
public void deleteChat(Long chatId) {
chatRepo.deleteById(chatId);
public void deleteChat(Long chatId, Long ownerId) {
Chat chat = getChat(chatId, ownerId);
chatRepo.deleteById(chat.getId());
}
public SseEmitter proceedInteractionWithStreaming(Long chatId, String userPrompt) {
SseEmitter sseEmitter = new SseEmitter(0L);
final StringBuilder answer = new StringBuilder();
@@ -65,12 +69,10 @@ public class ChatServiceImpl implements ChatService {
return sseEmitter;
}
@SneakyThrows
private static void processToken(ChatResponse response, SseEmitter emitter, StringBuilder answer) {
var token = response.getResult().getOutput();
emitter.send(token);
answer.append(token.getText());
}
}
}