Skip to content

Commit 7fda97f

Browse files
committed
Feat: Get and Delete Indexes
1 parent c6477ab commit 7fda97f

File tree

7 files changed

+98
-5
lines changed

7 files changed

+98
-5
lines changed

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ listIndexes: $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/listIndexes.cpp
116116
$(CXX) $(CXXFLAGS) -o tests/indexes/listIndexes $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/listIndexes.cpp $(LDFLAGS)
117117
createIndex: $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/createIndex.cpp
118118
$(CXX) $(CXXFLAGS) -o tests/indexes/createIndex $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/createIndex.cpp $(LDFLAGS)
119-
120-
119+
deleteIndex: $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/deleteIndex.cpp
120+
$(CXX) $(CXXFLAGS) -o tests/indexes/deleteIndex $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/deleteIndex.cpp $(LDFLAGS)
121+
getIndex: $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/getIndex.cpp
122+
$(CXX) $(CXXFLAGS) -o tests/indexes/getIndex $(SRCS) $(EXAMPLES_DIR)/database/collection/indexes/getIndex.cpp $(LDFLAGS)
121123

122124
# Storage
123125
createBucket: $(SRCS) $(EXAMPLES_DIR)/storage/createBucket.cpp
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "Appwrite.hpp"
2+
#include <iostream>
3+
4+
int main() {
5+
std::string projectId = "66fbb5a100070a3a1d19";
6+
std::string apiKey = "";
7+
std::string databaseId = "database123";
8+
std::string collectionId = "test1234";
9+
std::string indexKey = "index_from_cpp";
10+
11+
Appwrite appwrite(projectId);
12+
Databases& databases = appwrite.getDatabases();
13+
14+
databases.setup(apiKey, projectId);
15+
16+
try {
17+
std::string response = databases.deleteIndexes(databaseId, collectionId, indexKey);
18+
std::cout << "Index deleted successfully! \nResponse: " << response << std::endl;
19+
} catch (const AppwriteException& ex) {
20+
std::cerr << "Exception: " << ex.what() << std::endl;
21+
}
22+
23+
return 0;
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "Appwrite.hpp"
2+
#include <iostream>
3+
4+
int main() {
5+
std::string projectId = "66fbb5a100070a3a1d19";
6+
std::string apiKey = "";
7+
std::string databaseId = "database123";
8+
std::string collectionId = "test1234";
9+
std::string indexKey = "index_1";
10+
11+
Appwrite appwrite(projectId);
12+
Databases& databases = appwrite.getDatabases();
13+
14+
databases.setup(apiKey, projectId);
15+
16+
try {
17+
std::string response = databases.getCollection(databaseId, collectionId);
18+
std::cout << "Index fetched successfully! \nResponse: " << response << std::endl;
19+
} catch (const AppwriteException& ex) {
20+
std::cerr << "Exception: " << ex.what() << std::endl;
21+
}
22+
23+
return 0;
24+
}

include/classes/Databases.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ class Databases {
5252

5353
//indexes
5454
std::string listIndexes(const std::string& databaseId, const std::string& collectionId);
55-
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);
56-
55+
std::string createIndexes(const std::string& databaseId, const std::string& collectionId, const std::string& key, const std::string& type, const std::vector<std::string> &attributes);
56+
std::string deleteIndexes(const std::string& databaseId, const std::string& collectionId, const std::string& key);
57+
std::string getIndexes(const std::string& databaseId, const std::string& collectionId, const std::string& key);
5758

5859
private:
5960
std::string apiKey;

src/services/Databases.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ std::string Databases::getDocument(const std::string& databaseId, const std::str
702702

703703
}
704704

705+
//idexes
705706
std::string Databases::listIndexes(const std::string& databaseId, const std::string& collectionId){
706707
Validator::validateDatabaseParams(databaseId, collectionId);
707708

@@ -720,7 +721,7 @@ std::string Databases::listIndexes(const std::string& databaseId, const std::str
720721
}
721722
}
722723

723-
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){
724+
std::string Databases::createIndexes(const std::string& databaseId, const std::string& collectionId, const std::string& key, const std::string& type, const std::vector<std::string> &attributes){
724725
if (databaseId.empty()) {
725726
throw AppwriteException("Missing required parameter: 'databaseId'");
726727
}
@@ -751,4 +752,45 @@ std::string Databases::createIndex(const std::string& databaseId, const std::str
751752
} else {
752753
throw AppwriteException("Error creating index. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
753754
}
755+
}
756+
757+
std::string Databases::deleteIndexes(const std::string& databaseId, const std::string& collectionId, const std::string& key) {
758+
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections/" + collectionId + "/indexes/" + key;
759+
760+
std::vector<std::string> headers = Config::getHeaders(projectId);
761+
headers.push_back("X-Appwrite-Key: " + apiKey);
762+
763+
std::string response;
764+
765+
int statusCode = Utils::deleteRequest(url, headers, response);
766+
767+
if (statusCode == HttpStatus::DELETED) {
768+
return response;
769+
} else {
770+
throw AppwriteException("Error deleting index. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
771+
}
772+
}
773+
774+
std::string Databases::getIndexes(const std::string& databaseId, const std::string& collectionId, const std::string& key) {
775+
776+
if (databaseId.empty()) {
777+
throw AppwriteException("Missing required parameter: 'index key'");
778+
}
779+
780+
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections/" + collectionId + "/indexes/" + key;
781+
782+
std::vector<std::string> headers = Config::getHeaders(projectId);
783+
headers.push_back("X-Appwrite-Key: " + apiKey);
784+
785+
std::string response;
786+
787+
int statusCode = Utils::getRequest(url, headers, response);
788+
789+
if (statusCode == HttpStatus::OK) {
790+
return response;
791+
}
792+
else {
793+
throw AppwriteException("Error fetching index. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
794+
}
795+
754796
}

tests/indexes/deleteIndex

1.33 MB
Binary file not shown.

tests/indexes/getIndex

1.33 MB
Binary file not shown.

0 commit comments

Comments
 (0)