concurrency

This commit is contained in:
2026-10-08 13:08:31 +03:00
parent b95197a6fc
commit 65bd58c409
42 changed files with 743 additions and 224 deletions
@@ -16,7 +16,6 @@ import ru.mtusi.messenger.model.User;
import ru.mtusi.messenger.repository.ChatRepository;
import ru.mtusi.messenger.util.CursorCodec;
import java.time.Instant;
import java.util.*;
@Service
@@ -54,7 +53,7 @@ public class ChatService {
}
@Transactional(readOnly = true)
public ChatDTO getChat(UUID caller, UUID chatId) {
public ChatDTO getChatDTO(UUID caller, UUID chatId) {
Chat chat = chatRepository.findById(chatId)
.orElseThrow(() -> new NotFoundException("Chat not found"));
@@ -68,7 +67,8 @@ public class ChatService {
public ChatPageDTO listChats(UUID userId, Integer limit, String cursor) {
int size = (limit == null) ? 50 : limit;
CursorCodec.Cursor decoded = parseChatsCursor(cursor);
List<Chat> rows = chatRepository.findPageByMember(userId, boundaryCreatedAt(decoded), boundaryId(decoded), Pageable.ofSize(size + 1));
List<Chat> rows = (decoded == null) ? chatRepository.findPageByMember(userId, Pageable.ofSize(size + 1))
: chatRepository.findPageByMemberAfterCursor(userId, decoded.boundary().createdAt(), decoded.boundary().id(), Pageable.ofSize(size + 1));
return buildPage(rows, size);
}
@@ -83,14 +83,6 @@ public class ChatService {
return decoded;
}
private Instant boundaryCreatedAt(CursorCodec.Cursor cursor) {
return cursor == null ? null : cursor.boundary().createdAt();
}
private UUID boundaryId(CursorCodec.Cursor cursor) {
return cursor == null ? null : cursor.boundary().id();
}
private ChatPageDTO buildPage(List<Chat> rows, int size) {
List<ChatDTO> items = rows.stream()
.limit(size)
@@ -114,4 +106,15 @@ public class ChatService {
private CursorCodec.PageBoundary boundaryOf(Chat chat) {
return new CursorCodec.PageBoundary(chat.getCreatedAt(), chat.getId());
}
@Transactional(readOnly = true)
public Chat getChat(UUID caller, UUID chatId) {
Chat chat = chatRepository.findById(chatId)
.orElseThrow(() -> new NotFoundException("Chat not found"));
if (chat.getMembers().stream().noneMatch(m -> m.getId().equals(caller))) {
throw new NotFoundException("Chat not found");
}
return chat;
}
}