diff --git a/Makefile b/Makefile index f596771..9f332e5 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,7 @@ BINS = \ listDocument \ getDocument \ deleteDocument \ + updateDocument \ listAttributes \ createBooleanAttribute \ createEmailAttribute \ @@ -125,6 +126,8 @@ deleteDocument: $(SRCS) $(EXAMPLES_DIR)/database/collection/document/deleteDocum $(CXX) $(CXXFLAGS) -o ./tests/document/deleteDocument $(SRCS) $(EXAMPLES_DIR)/database/collection/document/deleteDocument.cpp $(LDFLAGS) getDocument: $(SRCS) $(EXAMPLES_DIR)/database/collection/document/getDocument.cpp $(CXX) $(CXXFLAGS) -o ./tests/document/getDocument $(SRCS) $(EXAMPLES_DIR)/database/collection/document/getDocument.cpp $(LDFLAGS) +updateDocument: $(SRCS) $(EXAMPLES_DIR)/database/collection/document/updateDocument.cpp + $(CXX) $(CXXFLAGS) -o ./tests/document/updateDocument $(SRCS) $(EXAMPLES_DIR)/database/collection/document/updateDocument.cpp $(LDFLAGS) #Collection-Attribute listAttributes: $(SRCS) $(EXAMPLES_DIR)/database/collection/attribute/listAttributes.cpp diff --git a/examples/database/collection/document/updateDocument.cpp b/examples/database/collection/document/updateDocument.cpp new file mode 100644 index 0000000..4de25a9 --- /dev/null +++ b/examples/database/collection/document/updateDocument.cpp @@ -0,0 +1,34 @@ +#include "Appwrite.hpp" +#include +#include + +int main() +{ + std::string projectId = "66fbb5a100070a3a1d19"; + std::string apiKey = ""; + std::string databaseId = "database123"; + std::string collectionId = "test1234"; + std::string documentId = "newDocument"; + json data = { + {"new_email_attribute", "pou@email.com"}, + {"new_enum_attribute", {"element1234"}}, + {"UpdatedFloat123", 9}, + {"UpdatedInteger123", 123}, + {"UpdatedIPaddress123", "192.168.1.1"}, + {"UpdatedString123", {"abc", "def"}} + }; + + Appwrite appwrite(projectId, apiKey); + + try + { + std::string response = appwrite.getDatabases().updateDocument(databaseId, collectionId, documentId, data); + std::cout << "Document updated successfully! \nResponse: " << response << std::endl; + } + catch (const AppwriteException &ex) + { + std::cerr << "Exception: " << ex.what() << std::endl; + } + + return 0; +} diff --git a/include/classes/Databases.hpp b/include/classes/Databases.hpp index 0148e58..7297cb6 100644 --- a/include/classes/Databases.hpp +++ b/include/classes/Databases.hpp @@ -52,6 +52,7 @@ class Databases { std::string listDocument(const std::string& databaseId, const std::string& collectionId); 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); + std::string updateDocument(const std::string& databaseId, const std::string& collectionId, const std::string& documentId, const json& data); //indexes std::string listIndexes(const std::string& databaseId, const std::string& collectionId); diff --git a/src/services/Databases.cpp b/src/services/Databases.cpp index 9691c16..fbe4802 100644 --- a/src/services/Databases.cpp +++ b/src/services/Databases.cpp @@ -731,6 +731,42 @@ std::string Databases::getDocument(const std::string& databaseId, const std::str } +std::string Databases::updateDocument(const std::string& databaseId, const std::string& collectionId, const std::string& documentId, const json& data) { + + if (databaseId.empty()) { + throw AppwriteException("Missing required parameter: 'databaseId'"); + } + if (collectionId.empty()) { + throw AppwriteException("Missing required parameter: 'collectionId'"); + } + if (documentId.empty()) { + throw AppwriteException("Missing required parameter: 'documentId'"); + } + if (data.is_null()) { + throw AppwriteException("Missing required parameter: 'data'"); + } + + std::string url = Config::API_BASE_URL + "/databases/" + databaseId + "/collections/" + collectionId + "/documents/" + documentId; + + json payloadJson = { + {"data", data} + }; + + std::string payload = payloadJson.dump(); + std::vector headers = Config::getHeaders(projectId); + headers.push_back("X-Appwrite-Key: " + apiKey); + + std::string response; + + int statusCode = Utils::patchRequest(url, payload, headers, response); + + if (statusCode == HttpStatus::OK) { + return response; + } else { + throw AppwriteException("Error updating document. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response); + } +} + //idexes std::string Databases::listIndexes(const std::string& databaseId, const std::string& collectionId){ Validator::validateDatabaseParams(databaseId, collectionId);