diff --git a/services/backup/docker-server/contents/server/src/Constants.h b/services/backup/docker-server/contents/server/src/Constants.h --- a/services/backup/docker-server/contents/server/src/Constants.h +++ b/services/backup/docker-server/contents/server/src/Constants.h @@ -24,8 +24,10 @@ #ifdef COMM_TEST_SERVICES const std::string LOG_TABLE_NAME = "backup-service-log-test"; +const std::string BACKUP_TABLE_NAME = "backup-service-backup-test"; #else const std::string LOG_TABLE_NAME = "backup-service-log"; +const std::string BACKUP_TABLE_NAME = "backup-service-backup"; #endif } // namespace network diff --git a/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.h b/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.h new file mode 100644 --- /dev/null +++ b/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.h @@ -0,0 +1,61 @@ +#pragma once + +#include "Item.h" + +#include + +namespace comm { +namespace network { +namespace database { + +class BackupItem : public Item { + + std::string userID; + uint64_t created; + std::string backupID; + std::string recoveryData; + std::string encryptedBackupKey; + std::string compactionHolder; + std::vector attachmentHolders; + + void validate() const override; + +public: + static std::string tableName; + static const std::string FIELD_USER_ID; + static const std::string FIELD_CREATED; + static const std::string FIELD_BACKUP_ID; + static const std::string FIELD_RECOVERY_DATA; + static const std::string FIELD_ENCRYPTED_BACKUP_KEY; + static const std::string FIELD_COMPACTION_HOLDER; + static const std::string FIELD_ATTACHMENT_HOLDERS; + + BackupItem() { + } + BackupItem( + std::string userID, + uint64_t created, + std::string backupID, + std::string recoveryData, + std::string encryptedBackupKey, + std::string compactionHolder, + std::vector attachmentHolders); + BackupItem(const AttributeValues &itemFromDB); + + void assignItemFromDatabase(const AttributeValues &itemFromDB) override; + + std::string getTableName() const override; + std::string getPrimaryKey() const override; + + std::string getUserID() const; + uint64_t getCreated() const; + std::string getBackupID() const; + std::string getRecoveryData() const; + std::string getEncryptedBackupKey() const; + std::string getCompactionHolder() const; + std::vector getAttachmentHolders() const; +}; + +} // namespace database +} // namespace network +} // namespace comm diff --git a/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.cpp b/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.cpp new file mode 100644 --- /dev/null +++ b/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.cpp @@ -0,0 +1,120 @@ +#include "BackupItem.h" + +#include "Constants.h" + +namespace comm { +namespace network { +namespace database { + +const std::string BackupItem::FIELD_USER_ID = "userID"; +const std::string BackupItem::FIELD_CREATED = "created"; +const std::string BackupItem::FIELD_BACKUP_ID = "backupID"; +const std::string BackupItem::FIELD_RECOVERY_DATA = "recoveryData"; +const std::string BackupItem::FIELD_ENCRYPTED_BACKUP_KEY = "encryptedBackupKey"; +const std::string BackupItem::FIELD_COMPACTION_HOLDER = "compactionHolder"; +const std::string BackupItem::FIELD_ATTACHMENT_HOLDERS = "attachmentHolders"; + +std::string BackupItem::tableName = BACKUP_TABLE_NAME; + +BackupItem::BackupItem( + std::string userID, + uint64_t created, + std::string backupID, + std::string recoveryData, + std::string encryptedBackupKey, + std::string compactionHolder, + std::vector attachmentHolders) + : userID(userID), + created(created), + backupID(backupID), + recoveryData(recoveryData), + encryptedBackupKey(encryptedBackupKey), + compactionHolder(compactionHolder), + attachmentHolders(attachmentHolders) { + this->validate(); +} + +BackupItem::BackupItem(const AttributeValues &itemFromDB) { + this->assignItemFromDatabase(itemFromDB); +} + +void BackupItem::validate() const { + if (!this->userID.size()) { + throw std::runtime_error("userID empty"); + } + if (!this->created) { + throw std::runtime_error("created not provided"); + } + if (!this->backupID.size()) { + throw std::runtime_error("backupID empty"); + } + if (!this->recoveryData.size()) { + throw std::runtime_error("recoveryData empty"); + } + if (!this->encryptedBackupKey.size()) { + throw std::runtime_error("encryptedBackupKey empty"); + } + if (!this->compactionHolder.size()) { + throw std::runtime_error("compactionHolder empty"); + } +} + +void BackupItem::assignItemFromDatabase(const AttributeValues &itemFromDB) { + try { + this->userID = itemFromDB.at(BackupItem::FIELD_USER_ID).GetS(); + this->created = std::stoll( + std::string(itemFromDB.at(BackupItem::FIELD_CREATED).GetS()).c_str()); + this->backupID = itemFromDB.at(BackupItem::FIELD_BACKUP_ID).GetS(); + this->recoveryData = itemFromDB.at(BackupItem::FIELD_RECOVERY_DATA).GetS(); + this->encryptedBackupKey = + itemFromDB.at(BackupItem::FIELD_ENCRYPTED_BACKUP_KEY).GetS(); + this->compactionHolder = + itemFromDB.at(BackupItem::FIELD_COMPACTION_HOLDER).GetS(); + this->attachmentHolders = + itemFromDB.at(BackupItem::FIELD_ATTACHMENT_HOLDERS).GetSS(); + } catch (std::logic_error &e) { + throw std::runtime_error( + "invalid backup item provided, " + std::string(e.what())); + } + this->validate(); +} + +std::string BackupItem::getTableName() const { + return BackupItem::tableName; +} + +std::string BackupItem::getPrimaryKey() const { + return BackupItem::FIELD_USER_ID; +} + +std::string BackupItem::getUserID() const { + return this->userID; +} + +uint64_t BackupItem::getCreated() const { + return this->created; +} + +std::string BackupItem::getBackupID() const { + return this->backupID; +} + +std::string BackupItem::getRecoveryData() const { + return this->recoveryData; +} + +std::string BackupItem::getEncryptedBackupKey() const { + return this->encryptedBackupKey; +} + +std::string BackupItem::getCompactionHolder() const { + return this->compactionHolder; +} + +std::vector BackupItem::getAttachmentHolders() const { + return this->attachmentHolders; +} + +} // namespace database +} // namespace network +} // namespace comm diff --git a/services/backup/docker-server/contents/server/src/DatabaseEntities/DatabaseEntitiesTools.h b/services/backup/docker-server/contents/server/src/DatabaseEntities/DatabaseEntitiesTools.h --- a/services/backup/docker-server/contents/server/src/DatabaseEntities/DatabaseEntitiesTools.h +++ b/services/backup/docker-server/contents/server/src/DatabaseEntities/DatabaseEntitiesTools.h @@ -3,6 +3,7 @@ #include "Item.h" #include "LogItem.h" +#include "BackupItem.h" #include #include