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> 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; }