|
| 1 | +#include <iostream> |
| 2 | +#include "json.hpp" |
| 3 | +#include "Appwrite.hpp" |
| 4 | + |
| 5 | +using json = nlohmann::json; |
| 6 | + |
| 7 | +Account account; |
| 8 | +Databases databases; |
| 9 | + |
| 10 | +void displayMenu() { |
| 11 | + std::cout << "\n=== Appwrite C++ SDK Test Menu ===\n"; |
| 12 | + std::cout << "1. Create Account\n"; |
| 13 | + std::cout << "2. Create Session\n"; |
| 14 | + std::cout << "3. List Databases\n"; |
| 15 | + std::cout << "4. List Collections\n"; |
| 16 | + std::cout << "5. Create Collection\n"; |
| 17 | + std::cout << "6. Create Email Attribute\n"; |
| 18 | + std::cout << "7. List Documents\n"; |
| 19 | + std::cout << "8. List Attributes\n"; |
| 20 | + std::cout << "9. Create Document\n"; |
| 21 | + std::cout << "10. Create Index\n"; |
| 22 | + std::cout << "11. Exit\n"; |
| 23 | + std::cout << "Enter your choice: "; |
| 24 | +} |
| 25 | + |
| 26 | +void handleCreateAccount() { |
| 27 | + std::string email = "test@example.com"; |
| 28 | + std::string password = "securePassword"; |
| 29 | + std::string userId = "uniqueUserId"; |
| 30 | + std::string name = "Test User"; |
| 31 | + |
| 32 | + bool success = account.createAccount(email, password, userId, name); |
| 33 | + std::cout << (success ? "Account created successfully.\n" : "Failed to create account.\n"); |
| 34 | +} |
| 35 | + |
| 36 | +void handleCreateSession() { |
| 37 | + std::string email = "test@example.com"; |
| 38 | + std::string password = "securePassword"; |
| 39 | + |
| 40 | + std::string sessionId = account.createSession(email, password); |
| 41 | + if (!sessionId.empty()) { |
| 42 | + std::cout << "Session created successfully. Session ID: " << sessionId << "\n"; |
| 43 | + } else { |
| 44 | + std::cout << "Failed to create session.\n"; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +void handleListDatabases() { |
| 49 | + std::string response = databases.list(); |
| 50 | + std::cout << "Databases:\n" << response << "\n"; |
| 51 | +} |
| 52 | + |
| 53 | +void handleListCollections() { |
| 54 | + std::string databaseId = "database123"; |
| 55 | + std::string response = databases.listCollection(databaseId); |
| 56 | + std::cout << "Collections:\n" << response << "\n"; |
| 57 | +} |
| 58 | + |
| 59 | +void handleCreateCollection() { |
| 60 | + std::string databaseId = "database123"; |
| 61 | + std::string collectionId = "test1234collectionnew"; |
| 62 | + std::string name = "Sample Collection"; |
| 63 | + bool enabled = true; |
| 64 | + |
| 65 | + std::string response = databases.createCollection(databaseId, collectionId, name, enabled); |
| 66 | + std::cout << "Collection Creation Response:\n" << response << "\n"; |
| 67 | +} |
| 68 | + |
| 69 | +void handleCreateEmailAttribute() { |
| 70 | + std::string databaseId = "database123"; |
| 71 | + std::string collectionId = "test1234collectionnew"; |
| 72 | + std::string attributeId = "email"; |
| 73 | + bool required = true; |
| 74 | + std::string defaultValue = ""; |
| 75 | + |
| 76 | + std::string response = databases.createEmailAttribute(databaseId, collectionId, attributeId, required, defaultValue); |
| 77 | + std::cout << "Email Attribute Creation Response:\n" << response << "\n"; |
| 78 | +} |
| 79 | + |
| 80 | +void handleListDocuments() { |
| 81 | + std::string databaseId = "database123"; |
| 82 | + std::string collectionId = "test1234collectionnew"; |
| 83 | + |
| 84 | + std::string response = databases.listDocument(databaseId, collectionId); |
| 85 | + std::cout << "Documents:\n" << response << "\n"; |
| 86 | +} |
| 87 | + |
| 88 | +void handleListAttributes() { |
| 89 | + std::string databaseId = "database123"; |
| 90 | + std::string collectionId = "test1234collectionnew"; |
| 91 | + |
| 92 | + std::string response = databases.listAttributes(databaseId, collectionId); |
| 93 | + std::cout << "Attributes:\n" << response << "\n"; |
| 94 | +} |
| 95 | + |
| 96 | +void handleCreateDocument() { |
| 97 | + std::string databaseId = "database123"; |
| 98 | + std::string collectionId = "test1234collectionnew"; |
| 99 | + std::string documentId = "sampleDocumentId"; |
| 100 | + |
| 101 | + json data = { |
| 102 | + {"email", "test@example.com"} |
| 103 | + }; |
| 104 | + |
| 105 | + std::string response = databases.createDocument(databaseId, collectionId, documentId, data); |
| 106 | + std::cout << "Document Creation Response:\n" << response << "\n"; |
| 107 | +} |
| 108 | + |
| 109 | +void handleCreateIndex() { |
| 110 | + std::string databaseId = "database123"; |
| 111 | + std::string collectionId = "test1234collectionnew"; |
| 112 | + std::string key = "indexKey"; |
| 113 | + std::string type = "unique"; |
| 114 | + std::vector<std::string> attributes = {"email"}; |
| 115 | + |
| 116 | + std::string response = databases.createIndexes(databaseId, collectionId, key, type, attributes); |
| 117 | + std::cout << "Index Creation Response:\n" << response << "\n"; |
| 118 | +} |
| 119 | + |
| 120 | +int main() { |
| 121 | + std::string projectId = "66fbb5a100070a3a1d19"; |
| 122 | + std::string apiKey = ""; |
| 123 | + |
| 124 | + account.setup(projectId); |
| 125 | + databases.setup(apiKey, projectId); |
| 126 | + |
| 127 | + int choice; |
| 128 | + do { |
| 129 | + displayMenu(); |
| 130 | + std::cin >> choice; |
| 131 | + switch (choice) { |
| 132 | + case 1: |
| 133 | + handleCreateAccount(); |
| 134 | + break; |
| 135 | + case 2: |
| 136 | + handleCreateSession(); |
| 137 | + break; |
| 138 | + case 3: |
| 139 | + handleListDatabases(); |
| 140 | + break; |
| 141 | + case 4: |
| 142 | + handleListCollections(); |
| 143 | + break; |
| 144 | + case 5: |
| 145 | + handleCreateCollection(); |
| 146 | + break; |
| 147 | + case 6: |
| 148 | + handleCreateEmailAttribute(); |
| 149 | + break; |
| 150 | + case 7: |
| 151 | + handleListDocuments(); |
| 152 | + break; |
| 153 | + case 8: |
| 154 | + handleListAttributes(); |
| 155 | + break; |
| 156 | + case 9: |
| 157 | + handleCreateDocument(); |
| 158 | + break; |
| 159 | + case 10: |
| 160 | + handleCreateIndex(); |
| 161 | + break; |
| 162 | + case 11: |
| 163 | + std::cout << "Exiting program.\n"; |
| 164 | + break; |
| 165 | + default: |
| 166 | + std::cout << "Invalid choice. Please try again.\n"; |
| 167 | + } |
| 168 | + } while (choice != 11); |
| 169 | + |
| 170 | + return 0; |
| 171 | +} |
0 commit comments