Skip to content

feat: update documents #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ BINS = \
listDocument \
getDocument \
deleteDocument \
updateDocument \
listAttributes \
createBooleanAttribute \
createEmailAttribute \
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions examples/database/collection/document/updateDocument.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "Appwrite.hpp"
#include <json.hpp>
#include <iostream>

int main()
{
std::string projectId = "66fbb5a100070a3a1d19";
std::string apiKey = "";
std::string databaseId = "database123";
std::string collectionId = "test1234";
std::string documentId = "newDocument";
json data = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create the payload using the raw strings

{"new_email_attribute", "[email protected]"},
{"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;
}
1 change: 1 addition & 0 deletions include/classes/Databases.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions src/services/Databases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the nholman/json dependency

{"data", data}
};

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::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);
Expand Down