for 3
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package ru.mtusi.messenger.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.mtusi.messenger.dto.ChatDTO;
|
||||
import ru.mtusi.messenger.dto.ChatPageDTO;
|
||||
import ru.mtusi.messenger.exception.InvalidRequestException;
|
||||
import ru.mtusi.messenger.exception.NotFoundException;
|
||||
import ru.mtusi.messenger.mapper.ChatMapper;
|
||||
import ru.mtusi.messenger.model.Chat;
|
||||
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
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class ChatService {
|
||||
private final ChatMapper chatMapper;
|
||||
private final UserService userService;
|
||||
private final ChatRepository chatRepository;
|
||||
|
||||
@Transactional
|
||||
public ChatDTO createChat(UUID caller , ChatDTO chatDTO) {
|
||||
Set<User> members = validateCreate(caller, chatDTO);
|
||||
Chat chat = buildChat(chatDTO, members);
|
||||
try {
|
||||
chat = chatRepository.saveAndFlush(chat);
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
throw new NotFoundException("Cannot create chat: referenced entity is missing");
|
||||
}
|
||||
return chatMapper.toChatDto(chat);
|
||||
}
|
||||
|
||||
private Set<User> validateCreate(UUID caller, ChatDTO chatDTO) {
|
||||
Set<UUID> memberIds = chatDTO.getMemberIds();
|
||||
if (!memberIds.contains(caller)) {
|
||||
throw new InvalidRequestException("Вызывающий должен быть участником");
|
||||
}
|
||||
return userService.requireUsers(memberIds);
|
||||
}
|
||||
|
||||
private Chat buildChat(ChatDTO chatDTO, Set<User> members) {
|
||||
Chat chat = chatMapper.toChat(chatDTO);
|
||||
chat.setMembers(members);
|
||||
return chat;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public ChatDTO 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 chatMapper.toChatDto(chat);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
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));
|
||||
return buildPage(rows, size);
|
||||
}
|
||||
|
||||
private CursorCodec.Cursor parseChatsCursor(String cursor) {
|
||||
if (cursor == null || cursor.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
CursorCodec.Cursor decoded = CursorCodec.decode(cursor);
|
||||
if (decoded.scopeId() != null) {
|
||||
throw new InvalidRequestException("Invalid cursor");
|
||||
}
|
||||
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)
|
||||
.map(chatMapper::toChatDto)
|
||||
.toList();
|
||||
|
||||
ChatPageDTO page = new ChatPageDTO();
|
||||
page.setItems(items);
|
||||
page.setNextCursor(buildNextCursor(rows, size));
|
||||
return page;
|
||||
}
|
||||
|
||||
private String buildNextCursor(List<Chat> rows, int size) {
|
||||
if (rows.size() <= size) {
|
||||
return null;
|
||||
}
|
||||
Chat last = rows.get(size - 1);
|
||||
return CursorCodec.encode(null, boundaryOf(last));
|
||||
}
|
||||
|
||||
private CursorCodec.PageBoundary boundaryOf(Chat chat) {
|
||||
return new CursorCodec.PageBoundary(chat.getCreatedAt(), chat.getId());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user