Skip to content

Commit be791cf

Browse files
committed
Feat: List Document
1 parent fcb4d9c commit be791cf

File tree

6 files changed

+42
-1
lines changed

6 files changed

+42
-1
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ deleteCollection: $(SRCS) $(EXAMPLES_DIR)/database/collection/deleteCollection.c
7171
# Database-Collection-Document
7272
createDocument: $(SRCS) $(EXAMPLES_DIR)/database/collection/document/createDocument.cpp
7373
$(CXX) $(CXXFLAGS) -o tests/document/createDocument $(SRCS) $(EXAMPLES_DIR)/database/collection/document/createDocument.cpp $(LDFLAGS)
74+
listDocument: $(SRCS) $(EXAMPLES_DIR)/database/collection/document/listDocument.cpp
75+
$(CXX) $(CXXFLAGS) -o tests/document/listDocument $(SRCS) $(EXAMPLES_DIR)/database/collection/document/listDocument.cpp $(LDFLAGS)
7476

7577
#Collection-Attribute
7678
listAttributes: $(SRCS) $(EXAMPLES_DIR)/database/collection/attribute/listAttributes.cpp
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "Appwrite.hpp"
2+
#include <iostream>
3+
4+
int main() {
5+
std::string projectId = "66fbb5a100070a3a1d19";
6+
std::string apiKey = "";
7+
Appwrite appwrite(projectId);
8+
Databases& databases = appwrite.getDatabases();
9+
10+
databases.setup(apiKey, projectId);
11+
12+
try {
13+
std::string response = databases.listCollection();
14+
std::cout << "Document listed successfully! \nResponse: " << response << std::endl;
15+
} catch (const AppwriteException& ex) {
16+
std::cerr << "Exception: " << ex.what() << std::endl;
17+
}
18+
19+
return 0;
20+
}

include/classes/Databases.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class Databases {
4646

4747
// document
4848
std::string createDocument(const std::string& databaseId, const std::string& collectionId, const std::string& documentId, const json& data);
49-
49+
std::string listDocument(const std::string& databaseId, const std::string& collectionId);
50+
5051
private:
5152
std::string apiKey;
5253
std::string projectId;

src/services/Databases.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,21 @@ std::string Databases::createDocument(const std::string& databaseId, const std::
642642
throw AppwriteException("Error creating document. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
643643
}
644644
}
645+
646+
std::string Databases::listDocument(const std::string& databaseId, const std::string& collectionId){
647+
Validator::validateDatabaseParams(databaseId, collectionId);
648+
649+
std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections/" + collectionId + "/documentss";
650+
651+
std::vector<std::string> headers = Config::getHeaders(projectId);
652+
headers.push_back("X-Appwrite-Key: " + apiKey);
653+
654+
std::string response;
655+
int statusCode = Utils::getRequest(url, headers, response);
656+
657+
if (statusCode == HttpStatus::OK) {
658+
return response;
659+
} else {
660+
throw AppwriteException("Error fetching documents. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response);
661+
}
662+
}

tests/createDocument

-1.14 MB
Binary file not shown.

tests/document/listDocument

1.3 MB
Binary file not shown.

0 commit comments

Comments
 (0)