Introduce update delete and improve resilience

This commit is contained in:
Hepatica
2024-08-18 02:46:45 +02:00
parent 4e10c139f8
commit 74abaf534b
4 changed files with 533 additions and 104 deletions

View File

@@ -37,6 +37,163 @@ public:
return response;
}
std::string increase_user_app_count_(std::string user_id)
{
std::string json_data = "{"
"\"dataSource\": \"SpCloudCluster\","
"\"database\": \"SpCloud\","
"\"collection\": \"AllowedUsers\","
"\"filter\": {"
"\"discord_id\": \"" + user_id + "\""
"},"
"\"update\": {"
"\"$inc\": { \"app_count\": 1 }"
"}"
"}";
std::string command =
"curl --location 'https://eu-central-1.aws.data.mongodb-api.com/app/data-zvcqvrr/endpoint/data/v1/action/updateOne' "
"--header 'Content-Type: application/json' "
"--header 'Access-Control-Request-Headers: *' "
"--header 'api-key: Q1NfSCrruUAzsxdrjhZd3sjSwiqbdSFmCLeaCatZiuohUXsvEq9RtEAeG0JL2Jd7' "
"--data '" + json_data + "'";
auto request = std::async(std::launch::async, &MongoDbService::execute_command, this, command);
std::string response = request.get();
return response;
}
std::string decrease_user_app_count(std::string user_id)
{
std::string json_data = "{"
"\"dataSource\": \"SpCloudCluster\","
"\"database\": \"SpCloud\","
"\"collection\": \"AllowedUsers\","
"\"filter\": {"
"\"discord_id\": \"" + user_id + "\""
"},"
"\"update\": {"
"\"$inc\": { \"app_count\": -1 }"
"}"
"}";
std::string command =
"curl --location 'https://eu-central-1.aws.data.mongodb-api.com/app/data-zvcqvrr/endpoint/data/v1/action/updateOne' "
"--header 'Content-Type: application/json' "
"--header 'Access-Control-Request-Headers: *' "
"--header 'api-key: Q1NfSCrruUAzsxdrjhZd3sjSwiqbdSFmCLeaCatZiuohUXsvEq9RtEAeG0JL2Jd7' "
"--data '" + json_data + "'";
auto request = std::async(std::launch::async, &MongoDbService::execute_command, this, command);
std::string response = request.get();
return response;
}
std::string delete_document( std::string collection, std::string filter_field, std::string filter_value)
{
std::string json_data = "{"
"\"dataSource\": \"SpCloudCluster\","
"\"database\": \"SpCloud\","
"\"collection\": \"" + collection + "\","
"\"filter\": {"
"\"" + filter_field + "\": \"" + filter_value + "\""
"}"
"}";
std::string command =
"curl --location 'https://eu-central-1.aws.data.mongodb-api.com/app/data-zvcqvrr/endpoint/data/v1/action/deleteOne' "
"--header 'Content-Type: application/json' "
"--header 'api-key: Q1NfSCrruUAzsxdrjhZd3sjSwiqbdSFmCLeaCatZiuohUXsvEq9RtEAeG0JL2Jd7' "
"--data '" + json_data + "'";
auto request = std::async(std::launch::async, &MongoDbService::execute_command, this, command);
std::string response = request.get();
return response;
}
std::string get_user_id_by_auth_token(std::string auth_token)
{
std::string json_data = R"({
"dataSource": "SpCloudCluster",
"database": "SpCloud",
"collection": "AllowedUsers",
"filter": {
"auth_token": ")" + auth_token + R"("
}
})";
std::string command =
"curl --location 'https://eu-central-1.aws.data.mongodb-api.com/app/data-zvcqvrr/endpoint/data/v1/action/findOne' "
"--header 'Content-Type: application/json' "
"--header 'api-key: Q1NfSCrruUAzsxdrjhZd3sjSwiqbdSFmCLeaCatZiuohUXsvEq9RtEAeG0JL2Jd7' "
"--data-raw '" + json_data + "'";
auto request = std::async(std::launch::async, &MongoDbService::execute_command, this, command);
std::string response = request.get();
size_t pos_user_id = response.find("\"discord_id\":");
if (pos_user_id == std::string::npos) {
// Handle the case where "discord_id" is not found in the response
return ""; // or some other error handling
}
pos_user_id += 13; // 12 characters for "discord_id": and 1 for the opening quote
// Find the end of the user_id, either at a comma or the closing bracket
size_t end_pos = response.find_first_of(",}", pos_user_id);
if (end_pos == std::string::npos) {
// Handle malformed JSON
return ""; // or some other error handling
}
// Extract the user_id from the response, making sure to strip any surrounding quotes
std::string user_id = response.substr(pos_user_id, end_pos - pos_user_id);
if (!user_id.empty() && user_id.front() == '"' && user_id.back() == '"') {
user_id = user_id.substr(1, user_id.length() - 2);
}
return user_id;
}
std::string is_user_app_owner(std::string auth_token, std::string name_app)//Todo check it
{
std::string user_id = get_user_id_by_auth_token(auth_token);
std::string json_data = R"({
"dataSource": "SpCloudCluster",
"database": "SpCloud",
"collection": "Apps",
"filter": {
"user_id": ")" + user_id + R"(",
"name": ")" + name_app + R"("
}
})";
std::string command = "curl --location 'https://eu-central-1.aws.data.mongodb-api.com/app/data-zvcqvrr/endpoint/data/v1/action/findOne' "
"--header 'Content-Type: application/json' "
"--header 'api-key: Q1NfSCrruUAzsxdrjhZd3sjSwiqbdSFmCLeaCatZiuohUXsvEq9RtEAeG0JL2Jd7' "
"--data-raw '" + json_data + "'";
auto request = std::async(std::launch::async, &MongoDbService::execute_command, this, command);
std::string response = request.get();
if (response != "{\"document\":null}")
{
return "Success";
}
return "User isn't app owner";
}
std::string is_user_can_publish(std::string auth_token)
{
std::string json_data = R"({
@@ -86,6 +243,38 @@ public:
return "Success";
}
std::string is_user_banned(std::string auth_token)
{
std::string json_data = R"({
"dataSource": "SpCloudCluster",
"database": "SpCloud",
"collection": "AllowedUsers",
"filter": {
"auth_token": ")" + auth_token + R"("
}
})";
std::string command = "curl --location 'https://eu-central-1.aws.data.mongodb-api.com/app/data-zvcqvrr/endpoint/data/v1/action/findOne' "
"--header 'Content-Type: application/json' "
"--header 'api-key: Q1NfSCrruUAzsxdrjhZd3sjSwiqbdSFmCLeaCatZiuohUXsvEq9RtEAeG0JL2Jd7' "
"--data-raw '" + json_data + "'";
auto request = std::async(std::launch::async, &MongoDbService::execute_command, this, command);
std::string response = request.get();
size_t pos_is_user_banned = response.find("\"is_banned\":");
std::string is_banned_value = response.substr(pos_is_user_banned + 12, response.find_first_of(",}", pos_is_user_banned) - pos_is_user_banned - 12);
if (is_banned_value == "true")
{
return "Fail publish: user is banned";
}
return "Success";
}
std::string is_app_name_free(std::string name)
{
std::string json_data = R"({
@@ -142,6 +331,29 @@ public:
return response;
}
std::string get_app_list(std::string user_id)//Todo test this method
{
std::string json_data = R"({
"dataSource": "SpCloudCluster",
"database": "SpCloud",
"collection": "Apps",
"filter": {
"user_id": ")" + user_id + R"("
}
})";
std::string command = "curl --location 'https://eu-central-1.aws.data.mongodb-api.com/app/data-zvcqvrr/endpoint/data/v1/action/find' "
"--header 'Content-Type: application/json' "
"--header 'api-key: Q1NfSCrruUAzsxdrjhZd3sjSwiqbdSFmCLeaCatZiuohUXsvEq9RtEAeG0JL2Jd7' "
"--data-raw '" + json_data + "'";
auto request = std::async(std::launch::async, &MongoDbService::execute_command, this, command);
std::string response = request.get();
return response;
}
std::string execute_command(const std::string& command) {
std::array<char, 128> buffer;
std::string result;