Skip to content

Commit fd54471

Browse files
committed
Bug Fixed: listCollection
1 parent 0b7ab5a commit fd54471

File tree

4 files changed

+42
-43
lines changed

4 files changed

+42
-43
lines changed

examples/database/collection/listCollection.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33

44
int main() {
55
std::string projectId = "66fbb5a100070a3a1d19";
6-
std::string apiKey = "";
6+
std::string apiKey = "";
7+
std::string databaseId = "database123";
8+
79
Appwrite appwrite(projectId);
810
Databases& databases = appwrite.getDatabases();
911

1012
databases.setup(apiKey, projectId);
1113

1214
try {
13-
std::string response = databases.listCollection();
14-
std::cout << "Collection listed successfully! \nResponse: " << response << std::endl;
15+
std::string response = databases.listCollection(databaseId);
16+
std::cout << "Collections listed successfully! \nResponse: " << response << std::endl;
1517
} catch (const AppwriteException& ex) {
1618
std::cerr << "Exception: " << ex.what() << std::endl;
1719
}

include/classes/Databases.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Databases {
2323

2424

2525
// collections
26-
std::string listCollection();
26+
std::string listCollection(const std::string& databaseId);
2727
std::string createCollection(const std::string& databaseId, const std::string& collectionId, const std::string& name, bool enabled);
2828
std::string getCollection(const std::string& databaseId, const std::string& collectionId);
2929
std::string updateCollection(const std::string& databaseId,const std::string& collectionId, const std::string& name, bool enabled);

src/services/Databases.cpp

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,36 @@ std::string Databases::update(const std::string &databaseId, const std::string &
102102
}
103103
}
104104

105-
//collection
106-
std::string Databases::listCollection(){
107-
std::string url = Config::API_BASE_URL + "/databases";
105+
std::string Databases::getDatabaseUsage(const std::string& databaseId, const std::string& range) {
106+
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/usage?range=" + range;
107+
std::cout << "Request URL: " << url << std::endl;
108108

109109
std::vector<std::string> headers = Config::getHeaders(projectId);
110110
headers.push_back("X-Appwrite-Key: " + apiKey);
111111

112112
std::string response;
113-
114113
int statusCode = Utils::getRequest(url, headers, response);
115114

116115
if (statusCode == HttpStatus::OK) {
117116
return response;
117+
} else {
118+
throw AppwriteException("Error fetching database usage. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
118119
}
119-
else {
120+
}
121+
122+
//collection
123+
std::string Databases::listCollection(const std::string& databaseId) {
124+
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections";
125+
126+
std::vector<std::string> headers = Config::getHeaders(projectId);
127+
headers.push_back("X-Appwrite-Key: " + apiKey);
128+
129+
std::string response;
130+
int statusCode = Utils::getRequest(url, headers, response);
131+
132+
if (statusCode == HttpStatus::OK) {
133+
return response;
134+
} else {
120135
throw AppwriteException("Error listing collections. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
121136
}
122137
}
@@ -205,6 +220,22 @@ std::string Databases::deleteCollection(const std::string& databaseId, const std
205220
}
206221
}
207222

223+
std::string Databases::getCollectionUsage(const std::string& databaseId, const std::string& collectionId, const std::string& range) {
224+
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections/" + collectionId + "/usage?range=" + range;
225+
std::cout << "Request URL: " << url << std::endl;
226+
227+
std::vector<std::string> headers = Config::getHeaders(projectId);
228+
headers.push_back("X-Appwrite-Key: " + apiKey);
229+
230+
std::string response;
231+
int statusCode = Utils::getRequest(url, headers, response);
232+
233+
if (statusCode == HttpStatus::OK) {
234+
return response;
235+
} else {
236+
throw AppwriteException("Error fetching collection usage. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
237+
}
238+
}
208239
//attribute
209240
std::string Databases::createBooleanAttribute(const std::string& databaseId, const std::string& collectionId, const std::string& attributeId, bool defaultValue, bool required) {
210241

@@ -794,37 +825,3 @@ std::string Databases::getIndexes(const std::string& databaseId, const std::stri
794825
}
795826

796827
}
797-
798-
std::string Databases::getDatabaseUsage(const std::string& databaseId, const std::string& range) {
799-
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/usage?range=" + range;
800-
std::cout << "Request URL: " << url << std::endl;
801-
802-
std::vector<std::string> headers = Config::getHeaders(projectId);
803-
headers.push_back("X-Appwrite-Key: " + apiKey);
804-
805-
std::string response;
806-
int statusCode = Utils::getRequest(url, headers, response);
807-
808-
if (statusCode == HttpStatus::OK) {
809-
return response;
810-
} else {
811-
throw AppwriteException("Error fetching database usage. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
812-
}
813-
}
814-
815-
std::string Databases::getCollectionUsage(const std::string& databaseId, const std::string& collectionId, const std::string& range) {
816-
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections/" + collectionId + "/usage?range=" + range;
817-
std::cout << "Request URL: " << url << std::endl;
818-
819-
std::vector<std::string> headers = Config::getHeaders(projectId);
820-
headers.push_back("X-Appwrite-Key: " + apiKey);
821-
822-
std::string response;
823-
int statusCode = Utils::getRequest(url, headers, response);
824-
825-
if (statusCode == HttpStatus::OK) {
826-
return response;
827-
} else {
828-
throw AppwriteException("Error fetching collection usage. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
829-
}
830-
}

tests/collection/listCollection

270 KB
Binary file not shown.

0 commit comments

Comments
 (0)