Skip to content

Commit

Permalink
feat:create and list indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
pooranjoyb committed Oct 22, 2024
1 parent afdc3e7 commit beae4f9
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ updateIPaddressAttribute: $(SRCS) $(EXAMPLES_DIR)/database/collection/attribute/
updateStringAttribute: $(SRCS) $(EXAMPLES_DIR)/database/collection/attribute/updateStringAttribute.cpp
$(CXX) $(CXXFLAGS) -o tests/attribute/updateStringAttribute $(SRCS) $(EXAMPLES_DIR)/database/collection/attribute/updateStringAttribute.cpp $(LDFLAGS)

# Collection-Indexes
listIndexes: $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/listIndexes.cpp
$(CXX) $(CXXFLAGS) -o tests/indexes/listIndexes $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/listIndexes.cpp $(LDFLAGS)
createIndex: $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/createIndex.cpp
$(CXX) $(CXXFLAGS) -o tests/indexes/createIndex $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/createIndex.cpp $(LDFLAGS)



# Storage
createBucket: $(SRCS) $(EXAMPLES_DIR)/storage/createBucket.cpp
Expand Down
31 changes: 31 additions & 0 deletions examples/database/collection/indexes/createIndex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "Appwrite.hpp"
#include <iostream>

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

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

std::string databaseId = "database123";
std::string collectionId = "test1234";
std::string key = "index_from_cpp";

// we need to use type as "key" for arrayed attributes
std::string type = "key";
std::vector<std::string> attributes = {"new_enum_attribute"};

database.setup(apiKey, projectId);

try {
std::string response = database.createIndex(
databaseId, collectionId,key, type, attributes
);
std::cout << "Index created successfully! \nResponse: " << response << std::endl;
} catch (const AppwriteException& ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
}

return 0;
}
23 changes: 23 additions & 0 deletions examples/database/collection/indexes/listIndexes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "Appwrite.hpp"
#include <iostream>

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

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

databases.setup(apiKey, projectId);

try {
std::string response = databases.listIndexes(databaseId, collectionId);
std::cout << "Indexes listed successfully! \nResponse: " << response << std::endl;
} catch (const AppwriteException& ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
}

return 0;
}
5 changes: 5 additions & 0 deletions include/classes/Databases.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class Databases {
std::string deleteDocument(const std::string& databaseId, const std::string& collectionId, const std::string& documentId);
std::string getDocument(const std::string& databaseId, const std::string& collectionId, const std::string& documentId);

//indexes
std::string listIndexes(const std::string& databaseId, const std::string& collectionId);
std::string createIndex(const std::string& databaseId, const std::string& collectionId, const std::string& key, const std::string& type, const std::vector<std::string> &attributes);


private:
std::string apiKey;
std::string projectId;
Expand Down
1 change: 1 addition & 0 deletions include/enums/HttpStatus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ enum HttpStatus {
OK = 200,
CREATED = 201,
ATTRIBUTE_CREATED = 202,
INDEX_CREATED = 202,
DELETED = 204,
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
Expand Down
51 changes: 51 additions & 0 deletions src/services/Databases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,4 +700,55 @@ std::string Databases::getDocument(const std::string& databaseId, const std::str
throw AppwriteException("Error fetching document. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
}

}

std::string Databases::listIndexes(const std::string& databaseId, const std::string& collectionId){
Validator::validateDatabaseParams(databaseId, collectionId);

std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections/" + collectionId + "/indexes";

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 indexes. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
}
}

std::string Databases::createIndex(const std::string& databaseId, const std::string& collectionId, const std::string& key, const std::string& type, const std::vector<std::string> &attributes){
if (databaseId.empty()) {
throw AppwriteException("Missing required parameter: 'databaseId'");
}
if (collectionId.empty()) {
throw AppwriteException("Missing required parameter: 'collectionId'");
}

std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections/" + collectionId + "/indexes";

json payloadJson = {
{"databaseId", databaseId},
{"collectionId", collectionId},
{"key", key},
{"type", type},
{"attributes", attributes}
};

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

std::string response;

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

if (statusCode == HttpStatus::INDEX_CREATED) {
return response;
} else {
throw AppwriteException("Error creating index. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
}
}
Binary file added tests/indexes/createIndex
Binary file not shown.
Binary file added tests/indexes/listIndexes
Binary file not shown.

0 comments on commit beae4f9

Please sign in to comment.