Skip to content

Commit

Permalink
Bug Fixed: listCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
sristy17 committed Oct 27, 2024
1 parent 0b7ab5a commit fd54471
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 43 deletions.
8 changes: 5 additions & 3 deletions examples/database/collection/listCollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@

int main() {
std::string projectId = "66fbb5a100070a3a1d19";
std::string apiKey = "";
std::string apiKey = "";
std::string databaseId = "database123";

Appwrite appwrite(projectId);
Databases& databases = appwrite.getDatabases();

databases.setup(apiKey, projectId);

try {
std::string response = databases.listCollection();
std::cout << "Collection listed successfully! \nResponse: " << response << std::endl;
std::string response = databases.listCollection(databaseId);
std::cout << "Collections listed successfully! \nResponse: " << response << std::endl;
} catch (const AppwriteException& ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
}
Expand Down
2 changes: 1 addition & 1 deletion include/classes/Databases.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Databases {


// collections
std::string listCollection();
std::string listCollection(const std::string& databaseId);
std::string createCollection(const std::string& databaseId, const std::string& collectionId, const std::string& name, bool enabled);
std::string getCollection(const std::string& databaseId, const std::string& collectionId);
std::string updateCollection(const std::string& databaseId,const std::string& collectionId, const std::string& name, bool enabled);
Expand Down
75 changes: 36 additions & 39 deletions src/services/Databases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,36 @@ std::string Databases::update(const std::string &databaseId, const std::string &
}
}

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

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

std::string response;

int statusCode = Utils::getRequest(url, headers, response);

if (statusCode == HttpStatus::OK) {
return response;
} else {
throw AppwriteException("Error fetching database usage. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
}
else {
}

//collection
std::string Databases::listCollection(const std::string& databaseId) {
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections";

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

std::string response;
int statusCode = Utils::getRequest(url, headers, response);

if (statusCode == HttpStatus::OK) {
return response;
} else {
throw AppwriteException("Error listing collections. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
}
}
Expand Down Expand Up @@ -205,6 +220,22 @@ std::string Databases::deleteCollection(const std::string& databaseId, const std
}
}

std::string Databases::getCollectionUsage(const std::string& databaseId, const std::string& collectionId, const std::string& range) {
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections/" + collectionId + "/usage?range=" + range;
std::cout << "Request URL: " << url << std::endl;

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

std::string response;
int statusCode = Utils::getRequest(url, headers, response);

if (statusCode == HttpStatus::OK) {
return response;
} else {
throw AppwriteException("Error fetching collection usage. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
}
}
//attribute
std::string Databases::createBooleanAttribute(const std::string& databaseId, const std::string& collectionId, const std::string& attributeId, bool defaultValue, bool required) {

Expand Down Expand Up @@ -794,37 +825,3 @@ std::string Databases::getIndexes(const std::string& databaseId, const std::stri
}

}

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

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

std::string response;
int statusCode = Utils::getRequest(url, headers, response);

if (statusCode == HttpStatus::OK) {
return response;
} else {
throw AppwriteException("Error fetching database usage. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
}
}

std::string Databases::getCollectionUsage(const std::string& databaseId, const std::string& collectionId, const std::string& range) {
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections/" + collectionId + "/usage?range=" + range;
std::cout << "Request URL: " << url << std::endl;

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

std::string response;
int statusCode = Utils::getRequest(url, headers, response);

if (statusCode == HttpStatus::OK) {
return response;
} else {
throw AppwriteException("Error fetching collection usage. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
}
}
Binary file modified tests/collection/listCollection
Binary file not shown.

0 comments on commit fd54471

Please sign in to comment.