85 lines
3.1 KiB
Java
85 lines
3.1 KiB
Java
package ru.mtusi.messenger.service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import ru.mtusi.messenger.dto.Chat;
|
|
import ru.mtusi.messenger.dto.CreateChat;
|
|
import ru.mtusi.messenger.entity.ChatEntity;
|
|
import ru.mtusi.messenger.entity.UserEntity;
|
|
import ru.mtusi.messenger.exception.InvalidRequestException;
|
|
import ru.mtusi.messenger.exception.NotFoundException;
|
|
import ru.mtusi.messenger.repository.ChatRepository;
|
|
import ru.mtusi.messenger.repository.UserRepository;
|
|
|
|
import java.time.OffsetDateTime;
|
|
import java.time.ZoneOffset;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
|
|
@Service
|
|
public class ChatService {
|
|
|
|
private final ChatRepository chatRepository;
|
|
private final UserRepository userRepository;
|
|
|
|
public ChatService(ChatRepository chatRepository, UserRepository userRepository) {
|
|
this.chatRepository = chatRepository;
|
|
this.userRepository = userRepository;
|
|
}
|
|
|
|
@Transactional
|
|
public Chat createChat(UUID callerId, CreateChat request) {
|
|
Set<UUID> uniqueMembers = new HashSet<>(request.memberIds());
|
|
if (uniqueMembers.size() != request.memberIds().size()) {
|
|
throw new InvalidRequestException("member_ids must be unique");
|
|
}
|
|
if (!uniqueMembers.contains(callerId)) {
|
|
throw new InvalidRequestException("caller must be a member of the chat");
|
|
}
|
|
|
|
List<UserEntity> members = userRepository.findAllById(request.memberIds());
|
|
if (members.size() != request.memberIds().size()) {
|
|
throw new InvalidRequestException("Some members do not exist");
|
|
}
|
|
|
|
ChatEntity chat = new ChatEntity();
|
|
chat.setId(UUID.randomUUID());
|
|
chat.setTitle(request.title());
|
|
chat.setCreatedAt(OffsetDateTime.now(ZoneOffset.UTC));
|
|
chat.setMembers(new HashSet<>(members));
|
|
|
|
chatRepository.save(chat);
|
|
return toDto(chat);
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
public Chat getChat(UUID callerId, UUID chatId) {
|
|
ChatEntity chat = chatRepository.findById(chatId)
|
|
.orElseThrow(() -> new NotFoundException("Chat not found"));
|
|
|
|
boolean isMember = chat.getMembers().stream().anyMatch(u -> u.getId().equals(callerId));
|
|
if (!isMember) throw new NotFoundException("Chat not found");
|
|
|
|
return toDto(chat);
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
public List<Chat> getUserChats(UUID callerId) {
|
|
// В реальном проекте лучше сделать кастомный запрос, но для lab2 сойдет
|
|
return chatRepository.findAll().stream()
|
|
.filter(c -> c.getMembers().stream().anyMatch(u -> u.getId().equals(callerId)))
|
|
.map(this::toDto)
|
|
.toList();
|
|
}
|
|
|
|
public Chat toDto(ChatEntity entity) {
|
|
return new Chat(
|
|
entity.getId(),
|
|
entity.getTitle(),
|
|
entity.getMembers().stream().map(UserEntity::getId).toList(),
|
|
entity.getCreatedAt()
|
|
);
|
|
}
|
|
} |