diff --git a/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.h b/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.h --- a/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.h +++ b/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.h @@ -43,7 +43,8 @@ void assignItemFromDatabase(const AttributeValues &itemFromDB) override; std::string getTableName() const override; - std::string getPrimaryKey() const override; + PrimaryKey getPrimaryKey() const override; + PrimaryKeyValue getPrimaryKeyValue() const override; std::string getUserID() const; std::string getBackupID() const; diff --git a/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.cpp b/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.cpp --- a/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.cpp +++ b/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.cpp @@ -78,8 +78,12 @@ return BackupItem::tableName; } -std::string BackupItem::getPrimaryKey() const { - return BackupItem::FIELD_USER_ID; +PrimaryKey BackupItem::getPrimaryKey() const { + return PrimaryKey(BackupItem::FIELD_USER_ID, BackupItem::FIELD_BACKUP_ID); +} + +PrimaryKeyValue BackupItem::getPrimaryKeyValue() const { + return PrimaryKeyValue(this->userID, std::to_string(this->created)); } std::string BackupItem::getUserID() const { diff --git a/services/backup/docker-server/contents/server/src/DatabaseEntities/Item.h b/services/backup/docker-server/contents/server/src/DatabaseEntities/Item.h --- a/services/backup/docker-server/contents/server/src/DatabaseEntities/Item.h +++ b/services/backup/docker-server/contents/server/src/DatabaseEntities/Item.h @@ -3,6 +3,7 @@ #include #include +#include #include namespace comm { @@ -12,12 +13,34 @@ typedef Aws::Map AttributeValues; +struct PrimaryKeyBase { + PrimaryKeyBase(const std::string partitionKey) + : partitionKey(partitionKey), sortKey(nullptr) { + } + PrimaryKeyBase(const std::string partitionKey, const std::string sortKey) + : partitionKey(partitionKey), + sortKey(std::make_unique(sortKey)) { + } + + const std::string partitionKey; + std::unique_ptr sortKey; +}; + +struct PrimaryKey : PrimaryKeyBase { + using PrimaryKeyBase::PrimaryKeyBase; +}; + +struct PrimaryKeyValue : PrimaryKeyBase { + using PrimaryKeyBase::PrimaryKeyBase; +}; + class Item { virtual void validate() const = 0; public: virtual std::string getTableName() const = 0; - virtual std::string getPrimaryKey() const = 0; + virtual PrimaryKey getPrimaryKey() const = 0; + virtual PrimaryKeyValue getPrimaryKeyValue() const = 0; virtual void assignItemFromDatabase(const AttributeValues &itemFromDB) = 0; }; diff --git a/services/backup/docker-server/contents/server/src/DatabaseEntities/LogItem.h b/services/backup/docker-server/contents/server/src/DatabaseEntities/LogItem.h --- a/services/backup/docker-server/contents/server/src/DatabaseEntities/LogItem.h +++ b/services/backup/docker-server/contents/server/src/DatabaseEntities/LogItem.h @@ -40,7 +40,8 @@ void assignItemFromDatabase(const AttributeValues &itemFromDB) override; std::string getTableName() const override; - std::string getPrimaryKey() const override; + PrimaryKey getPrimaryKey() const override; + PrimaryKeyValue getPrimaryKeyValue() const override; std::string getBackupID() const; std::string getLogID() const; diff --git a/services/backup/docker-server/contents/server/src/DatabaseEntities/LogItem.cpp b/services/backup/docker-server/contents/server/src/DatabaseEntities/LogItem.cpp --- a/services/backup/docker-server/contents/server/src/DatabaseEntities/LogItem.cpp +++ b/services/backup/docker-server/contents/server/src/DatabaseEntities/LogItem.cpp @@ -69,8 +69,12 @@ return LogItem::tableName; } -std::string LogItem::getPrimaryKey() const { - return LogItem::FIELD_BACKUP_ID; +PrimaryKey LogItem::getPrimaryKey() const { + return PrimaryKey(LogItem::FIELD_BACKUP_ID, LogItem::FIELD_LOG_ID); +} + +PrimaryKeyValue LogItem::getPrimaryKeyValue() const { + return PrimaryKeyValue(this->backupID, this->logID); } std::string LogItem::getBackupID() const { diff --git a/services/backup/docker-server/contents/server/src/DatabaseManager.cpp b/services/backup/docker-server/contents/server/src/DatabaseManager.cpp --- a/services/backup/docker-server/contents/server/src/DatabaseManager.cpp +++ b/services/backup/docker-server/contents/server/src/DatabaseManager.cpp @@ -34,7 +34,8 @@ Aws::DynamoDB::Model::DeleteItemRequest request; request.SetTableName(item.getTableName()); request.AddKey( - item.getPrimaryKey(), Aws::DynamoDB::Model::AttributeValue(key)); + item.getPrimaryKey().partitionKey, + Aws::DynamoDB::Model::AttributeValue(key)); const Aws::DynamoDB::Model::DeleteItemOutcome &outcome = getDynamoDBClient()->DeleteItem(request); @@ -75,7 +76,7 @@ Aws::DynamoDB::Model::QueryRequest req; req.SetTableName(BackupItem::tableName); - req.SetKeyConditionExpression(item->getPrimaryKey() + " = :valueToMatch"); + req.SetKeyConditionExpression(BackupItem::FIELD_USER_ID + " = :valueToMatch"); AttributeValues attributeValues; attributeValues.emplace(":valueToMatch", userID); @@ -145,7 +146,7 @@ Aws::DynamoDB::Model::QueryRequest req; req.SetTableName(LogItem::tableName); - req.SetKeyConditionExpression(item->getPrimaryKey() + " = :valueToMatch"); + req.SetKeyConditionExpression(LogItem::FIELD_BACKUP_ID + " = :valueToMatch"); AttributeValues attributeValues; attributeValues.emplace(":valueToMatch", backupID);