forked from mtusi2026/mtusi2026lab2
Исправление по Ревью
This commit is contained in:
@@ -44,8 +44,8 @@ public class MessageService {
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public MessagePage getMessage(UUID chatId, UUID userId, String cursor, int limit) {
|
||||
if (limit < 1) {
|
||||
throw new IllegalArgumentException("Лимит должен быть больше 0");
|
||||
if (limit < 1 || limit > 100) {
|
||||
throw new IllegalArgumentException("limit must be between 1 and 100");
|
||||
}
|
||||
|
||||
chatService.requireMember(chatId);
|
||||
@@ -57,7 +57,7 @@ public class MessageService {
|
||||
List<Message> items;
|
||||
|
||||
if (cursor == null || cursor.isBlank()) {
|
||||
items = messageRepository.findByChatIdOrderByCreatedAtDescIdDesc(chatId, pageable);
|
||||
items = messageRepository.findByChatIdOrderByCreatedAtAscIdAsc(chatId, pageable);
|
||||
} else {
|
||||
UUID cursorId;
|
||||
try {
|
||||
@@ -67,11 +67,7 @@ public class MessageService {
|
||||
}
|
||||
|
||||
Message cursorMsg = messageRepository.findByIdAndChatId(cursorId, chatId)
|
||||
.orElse(null);
|
||||
|
||||
if (cursorMsg == null) {
|
||||
throw new IllegalArgumentException("Invalid cursor");
|
||||
}
|
||||
.orElseThrow(() -> new IllegalArgumentException("Invalid cursor"));
|
||||
|
||||
items = messageRepository.findNextPage(
|
||||
chatId,
|
||||
@@ -83,7 +79,18 @@ public class MessageService {
|
||||
|
||||
String nextCursor = null;
|
||||
if (items.size() == limit && !items.isEmpty()) {
|
||||
nextCursor = items.get(items.size() - 1).getId().toString();
|
||||
Message last = items.get(items.size() - 1);
|
||||
boolean hasMore = !messageRepository
|
||||
.findNextPage(
|
||||
chatId,
|
||||
last.getCreatedAt(),
|
||||
last.getId(),
|
||||
PageRequest.of(0, 1)
|
||||
)
|
||||
.isEmpty();
|
||||
if (hasMore) {
|
||||
nextCursor = last.getId().toString();
|
||||
}
|
||||
}
|
||||
|
||||
return new MessagePage(items, nextCursor);
|
||||
|
||||
Reference in New Issue
Block a user