diff --git a/services/tunnelbroker/src/Database/DatabaseManager.h b/services/tunnelbroker/src/Database/DatabaseManager.h --- a/services/tunnelbroker/src/Database/DatabaseManager.h +++ b/services/tunnelbroker/src/Database/DatabaseManager.h @@ -3,6 +3,7 @@ #include "AwsTools.h" #include "Constants.h" #include "DatabaseEntitiesTools.h" +#include "DatabaseManagerBase.h" #include "Tools.h" #include @@ -22,16 +23,7 @@ namespace network { namespace database { -class DatabaseManager { - - void innerPutItem( - std::shared_ptr item, - const Aws::DynamoDB::Model::PutItemRequest &request); - template - std::shared_ptr - innerFindItem(Aws::DynamoDB::Model::GetItemRequest &request); - void innerRemoveItem(const Item &item, const std::string &key); - +class DatabaseManager : public DatabaseManagerBase { public: static DatabaseManager &getInstance(); bool isTableAvailable(const std::string &tableName); diff --git a/services/tunnelbroker/src/Database/DatabaseManager.cpp b/services/tunnelbroker/src/Database/DatabaseManager.cpp --- a/services/tunnelbroker/src/Database/DatabaseManager.cpp +++ b/services/tunnelbroker/src/Database/DatabaseManager.cpp @@ -20,50 +20,6 @@ return result.IsSuccess(); } -void DatabaseManager::innerPutItem( - std::shared_ptr item, - const Aws::DynamoDB::Model::PutItemRequest &request) { - const Aws::DynamoDB::Model::PutItemOutcome outcome = - getDynamoDBClient()->PutItem(request); - if (!outcome.IsSuccess()) { - throw std::runtime_error(outcome.GetError().GetMessage()); - } -} - -template -std::shared_ptr -DatabaseManager::innerFindItem(Aws::DynamoDB::Model::GetItemRequest &request) { - std::shared_ptr item = createItemByType(); - request.SetTableName(item->getTableName()); - const Aws::DynamoDB::Model::GetItemOutcome &outcome = - getDynamoDBClient()->GetItem(request); - if (!outcome.IsSuccess()) { - throw std::runtime_error(outcome.GetError().GetMessage()); - } - const AttributeValues &outcomeItem = outcome.GetResult().GetItem(); - if (!outcomeItem.size()) { - return nullptr; - } - item->assignItemFromDatabase(outcomeItem); - return std::move(item); -} - -void DatabaseManager::innerRemoveItem( - const Item &item, - const std::string &key) { - Aws::DynamoDB::Model::DeleteItemRequest request; - request.SetTableName(item.getTableName()); - request.AddKey( - item.getPrimaryKey().partitionKey, - Aws::DynamoDB::Model::AttributeValue(key)); - - const Aws::DynamoDB::Model::DeleteItemOutcome &outcome = - getDynamoDBClient()->DeleteItem(request); - if (!outcome.IsSuccess()) { - throw std::runtime_error(outcome.GetError().GetMessage()); - } -} - void DatabaseManager::putSessionItem(const DeviceSessionItem &item) { Aws::DynamoDB::Model::PutItemRequest request; request.SetTableName(item.getTableName()); @@ -109,7 +65,11 @@ } void DatabaseManager::removeSessionItem(const std::string &sessionID) { - this->innerRemoveItem(*(createItemByType()), sessionID); + std::shared_ptr item = this->findSessionItem(sessionID); + if (item == nullptr) { + throw std::runtime_error("removing item failed: item not found"); + } + this->innerRemoveItem(*item); } void DatabaseManager::putSessionSignItem(const SessionSignItem &item) { @@ -138,7 +98,11 @@ } void DatabaseManager::removeSessionSignItem(const std::string &deviceID) { - this->innerRemoveItem(*(createItemByType()), deviceID); + std::shared_ptr item = this->findSessionSignItem(deviceID); + if (item == nullptr) { + throw std::runtime_error("removing item failed: item not found"); + } + this->innerRemoveItem(*item); } void DatabaseManager::putPublicKeyItem(const PublicKeyItem &item) { @@ -163,7 +127,11 @@ } void DatabaseManager::removePublicKeyItem(const std::string &deviceID) { - this->innerRemoveItem(*(createItemByType()), deviceID); + std::shared_ptr item = this->findPublicKeyItem(deviceID); + if (item == nullptr) { + throw std::runtime_error("removing item failed: item not found"); + } + this->innerRemoveItem(*item); } void DatabaseManager::putMessageItem(const MessageItem &item) { @@ -233,7 +201,11 @@ } void DatabaseManager::removeMessageItem(const std::string &messageID) { - this->innerRemoveItem(*(createItemByType()), messageID); + std::shared_ptr item = this->findMessageItem(messageID); + if (item == nullptr) { + throw std::runtime_error("removing item failed: item not found"); + } + this->innerRemoveItem(*item); } } // namespace database