public ChatRoomDTO create(Token token, UserId userId, String chatRoomName) throws ChatRoomAlreadyExistsException { LOG.info(format("Creating chat-room \"%s.\"", chatRoomName)); if (chatRoomRepository.count() > 0 && chatRoomRepository.findByName(chatRoomName) != null) { throw new ChatRoomAlreadyExistsException(format("ChatRoom %s already exists.", chatRoomName)); } ChatRoom chatRoom = new ChatRoom(chatRoomName); chatRoomRepository.save(chatRoom); LOG.info(format("Chat-room \"%s\" created successfully.", chatRoom.getName())); return new ChatRoomDTO(chatRoom.getId(), chatRoom.getName(), 0, 0); }
@Override public ArrayList<ChatRoomDTO> findByUserId(Token token, UserId userId) { return chatRoomRepository .findChatRoomsByUserId(userId.id) .stream() .map(chatRoom -> new ChatRoomDTO(chatRoom.getId(), chatRoom.getName(), 0, 0)) .collect(Collectors.toCollection(ArrayList<ChatRoomDTO>::new)); }
private ChatRoom getChatRoom(ChatRoomId chatRoomId) throws ChatRoomNotFoundException { ChatRoom chatRoom = chatRoomRepository.findOne(chatRoomId.id); if (chatRoom == null) { throw new ChatRoomNotFoundException( format("ChatRoom with this id [%d] not exists.", chatRoomId.id)); } return chatRoom; }
@Override public ArrayList<ChatRoomDTO> findAll(Token token, UserId userId) { Iterable<ChatRoom> chatRooms = chatRoomRepository.findAll(); ArrayList<ChatRoomDTO> chatRoomDTOs = new ArrayList<>(); for (ChatRoom chat : chatRooms) { chatRoomDTOs.add( new ChatRoomDTO( chat.getId(), chat.getName(), chat.getUsers().size(), chat.getMessages().size())); } return chatRoomDTOs; }