Skip to content

Commit

Permalink
Merge pull request #125 from fxdeniz/issue_124
Browse files Browse the repository at this point in the history
Issue 124
  • Loading branch information
fxdeniz authored Oct 11, 2022
2 parents bdd81ee + 9696a06 commit 7cfc628
Show file tree
Hide file tree
Showing 28 changed files with 917 additions and 1,032 deletions.
70 changes: 56 additions & 14 deletions Backend/FileStorageSubSystem/FileStorageManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ bool FileStorageManager::addNewFile(const QString &pathToFile,
if(!rowUnRegisteredFileEvent->isExistInDB())
return false;

bool isFolderCreated = this->addNewFolder(symbolDirectory);
bool isFolderCreated = this->addNewFolder(symbolDirectory, userDirectory);

if(!isFolderCreated)
return false;
Expand Down Expand Up @@ -406,17 +406,15 @@ bool FileStorageManager::isFileExistByUserFilePath(const QString &userFilePath)
bool FileStorageManager::isFolderExistByUserFolderPath(const QString &userFolderPath) const
{
bool result = false;
auto queryResult = QueryFileRecord(this->db).selectUserFolderPathListFromAllFiles();

// TODO: Replace for loop with db query.
for(const QString &currentPath : queryResult)
{
if(userFolderPath == currentPath)
{
result = true;
break;
}
}
QString dir = userFolderPath;
if(!dir.endsWith(QDir::separator()))
dir.append(QDir::separator());

auto queryResult = QueryFolderRecord(this->db).selectRowByUserDirectory(dir);

if(queryResult->isExistInDB())
result = true;

return result;
}
Expand Down Expand Up @@ -589,10 +587,11 @@ const QString &FileStorageManager::rootFolderPath()
return CONST_SYMBOL_DIRECTORY_SEPARATOR;
}

bool FileStorageManager::addNewFolder(const QString &directory)
// TODO Remove when V2_DialogAddNewFolder compeleted
bool FileStorageManager::addNewFolder(const QString &symbolDirectory)
{
QString dir = directory;
if(!directory.endsWith(CONST_SYMBOL_DIRECTORY_SEPARATOR))
QString dir = symbolDirectory;
if(!symbolDirectory.endsWith(CONST_SYMBOL_DIRECTORY_SEPARATOR))
dir.append(CONST_SYMBOL_DIRECTORY_SEPARATOR);

if(!this->isFolderSymbolExist(dir))
Expand Down Expand Up @@ -632,6 +631,49 @@ bool FileStorageManager::addNewFolder(const QString &directory)
return true;
}

bool FileStorageManager::addNewFolder(const QString &symbolDirectory, const QString &userDirectory)
{
QString dir = symbolDirectory;
if(!symbolDirectory.endsWith(CONST_SYMBOL_DIRECTORY_SEPARATOR))
dir.append(CONST_SYMBOL_DIRECTORY_SEPARATOR);

if(!this->isFolderSymbolExist(dir))
{
dir.truncate(dir.lastIndexOf(CONST_SYMBOL_DIRECTORY_SEPARATOR));

QStringList tokenList = dir.split(CONST_SYMBOL_DIRECTORY_SEPARATOR);
PtrTo_RowFolderRecord currentFolder;

auto *rowInserter = new RowInserter(this->db);
ScopedPtrTo_RowFolderRecordInserter folderInserter(rowInserter);

for(auto const &token : tokenList)
{
if(token.isEmpty())
{
currentFolder = folderInserter->insertRootFolder(CONST_SYMBOL_DIRECTORY_SEPARATOR);

if(!currentFolder->isExistInDB())
return false;
}
else
{
QString suffix = token + CONST_SYMBOL_DIRECTORY_SEPARATOR;
bool isChildAdded = currentFolder->addChildFolder(suffix, userDirectory);

if(!isChildAdded)
return false;
else
currentFolder = currentFolder->getChildFolderBySuffix(suffix);
}
}

return true;
}

return true;
}

bool FileStorageManager::markFolderAsFavorite(const QString &directory, bool status)
{
auto rowFolder = QueryFolderRecord(this->db).selectRowByDirectory(directory);
Expand Down
8 changes: 7 additions & 1 deletion Backend/FileStorageSubSystem/FileStorageManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ class FileStorageManager
SaveGroupItemMetaData getSaveGroupItemMetaData(const QString &pathToSymbolFile, qlonglong versionNumber) const;

static const QString &rootFolderPath();
bool addNewFolder(const QString &directory);

// TODO Remove when V2_DialogAddNewFolder compeleted
// NOTE this method maybe converted to addRootSymbolFolder(void);
bool addNewFolder(const QString &symbolDirectory);

bool addNewFolder(const QString &symbolDirectory, const QString &userDirectory);

bool markFolderAsFavorite(const QString &directory, bool status);
bool isFolderSymbolExist(const QString &directory) const;
bool deleteFolder(const QString &directory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <QSqlRecord>
#include <QSqlQuery>
#include <QDir>

QueryFolderRecord::QueryFolderRecord(const QSqlDatabase &db) : BaseSqlPrimitive(db, TABLE_NAME_FOLDER_RECORD)
{
Expand All @@ -23,6 +24,14 @@ PtrTo_RowFolderRecord QueryFolderRecord::selectRowByDirectory(const QString &dir
return result;
}

PtrTo_RowFolderRecord QueryFolderRecord::selectRowByUserDirectory(const QString &userDirectory) const
{
auto result = this->queryTemplateSelectRowByKey<QString>(TABLE_FOLDER_RECORD_COLNAME_USER_DIRECTORY, // 1
userDirectory); // 2

return result;
}

QList<PtrTo_RowFolderRecord> QueryFolderRecord::selectRowsByParentDirectory(const QString &parentDirectory) const
{
QList<PtrTo_RowFolderRecord> result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class QueryFolderRecord : public BaseSqlPrimitive

PtrTo_RowFolderRecord selectRowByID(qlonglong folderID) const;
PtrTo_RowFolderRecord selectRowByDirectory(const QString &directory) const;
PtrTo_RowFolderRecord selectRowByUserDirectory(const QString &userDirectory) const;

QList<PtrTo_RowFolderRecord> selectRowsByParentDirectory(const QString &parentDirectory) const;
QList<PtrTo_RowFolderRecord> selectRowsByMatchingDirectory(const QString &searchTerm) const;
Expand Down
69 changes: 57 additions & 12 deletions Backend/FileStorageSubSystem/SqlPrimitives/RowFolderRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@ RowFolderRecord::RowFolderRecord() : BaseRow(TABLE_NAME_FOLDER_RECORD)
{
this->parentDirectory = INVALID_FIELD_VALUE_QSTRING;
this->suffixDirectory = INVALID_FIELD_VALUE_QSTRING;
this->userDirectory = INVALID_FIELD_VALUE_QSTRING;
}

RowFolderRecord::RowFolderRecord(const QSqlDatabase &db, const QSqlRecord &record)
: BaseRow(db, record.value(TABLE_FOLDER_RECORD_COLNAME_FOLDER_ID).toLongLong(), TABLE_NAME_FOLDER_RECORD)
{
if(record.value(TABLE_FOLDER_RECORD_COLNAME_PARENT_DIRECTORY).isNull())
{
this->parentDirectory = INVALID_FIELD_VALUE_QSTRING;
this->userDirectory = INVALID_FIELD_VALUE_QSTRING;
}
else
{
this->parentDirectory = record.value(TABLE_FOLDER_RECORD_COLNAME_PARENT_DIRECTORY).toString();
this->userDirectory = record.value(TABLE_FILE_RECORD_COLNAME_USER_DIRECTORY).toString();
}

this->suffixDirectory = record.value(TABLE_FOLDER_RECORD_COLNAME_SUFFIX_DIRECTORY).toString();
}
Expand Down Expand Up @@ -62,6 +69,43 @@ const QString &RowFolderRecord::getSuffixDirectory() const
return this->suffixDirectory;
}

bool RowFolderRecord::setSuffixDirectory(const QString &newSuffixDirectory)
{
if(!this->isExistInDB())
return false;

this->queryTemplateUpdateColumnValue<QString>(TABLE_FOLDER_RECORD_COLNAME_SUFFIX_DIRECTORY, newSuffixDirectory);

if(this->getLastError().type() == QSqlError::ErrorType::NoError)
{
this->suffixDirectory = newSuffixDirectory;
return true;
}

return false;
}

const QString &RowFolderRecord::getUserDirectory() const
{
return this->userDirectory;
}

bool RowFolderRecord::setUserDirectory(const QString &newUserDirectory)
{
if(!this->isExistInDB())
return false;

this->queryTemplateUpdateColumnValue<QString>(TABLE_FOLDER_RECORD_COLNAME_USER_DIRECTORY, newUserDirectory);

if(this->getLastError().type() == QSqlError::ErrorType::NoError)
{
this->userDirectory = newUserDirectory;
return true;
}

return false;
}

PtrTo_RowFolderRecord RowFolderRecord::getChildFolderBySuffix(const QString &suffixDir) const
{
QueryFolderRecord queries(this->getDb());
Expand Down Expand Up @@ -138,32 +182,33 @@ QString RowFolderRecord::toString() const
result += " | " + TABLE_FOLDER_RECORD_COLNAME_DIRECTORY + " = " + this->getDirectory();
result += " | " + TABLE_FOLDER_RECORD_COLNAME_PARENT_DIRECTORY + " = " + this->getParentDirectory();
result += " | " + TABLE_FOLDER_RECORD_COLNAME_SUFFIX_DIRECTORY + " = " + this->getSuffixDirectory();
result += " | " + TABLE_FOLDER_RECORD_COLNAME_USER_DIRECTORY + " = " + this->getUserDirectory();

return result;
}

bool RowFolderRecord::setSuffixDirectory(const QString &newSuffixDirectory)
// TODO Remove when V2_DialogAddNewFolder compeleted
bool RowFolderRecord::addChildFolder(const QString &suffixDirectory)
{
if(!this->isExistInDB())
return false;
auto *rowInserter = new RowInserter(this->getDb());
ScopedPtrTo_RowFolderRecordInserter folderInserter(rowInserter);

this->queryTemplateUpdateColumnValue<QString>(TABLE_FOLDER_RECORD_COLNAME_SUFFIX_DIRECTORY, newSuffixDirectory);
auto childRow = folderInserter->insertChildFolder(this->getDirectory(), suffixDirectory);

if(this->getLastError().type() == QSqlError::ErrorType::NoError)
{
this->suffixDirectory = newSuffixDirectory;
return true;
}
this->setLastError(folderInserter->getLastError());

return false;
if(childRow->isExistInDB())
return true;
else
return false;
}

bool RowFolderRecord::addChildFolder(const QString &suffixDirectory)
bool RowFolderRecord::addChildFolder(const QString &suffixDirectory, const QString &userDirectory)
{
auto *rowInserter = new RowInserter(this->getDb());
ScopedPtrTo_RowFolderRecordInserter folderInserter(rowInserter);

auto childRow = folderInserter->insertChildFolder(this->getDirectory(), suffixDirectory);
auto childRow = folderInserter->insertChildFolder(this->getDirectory(), suffixDirectory, userDirectory);

this->setLastError(folderInserter->getLastError());

Expand Down
7 changes: 7 additions & 0 deletions Backend/FileStorageSubSystem/SqlPrimitives/RowFolderRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class RowFolderRecord : public BaseRow
QString getDirectory() const;
const QString &getParentDirectory() const;
const QString &getSuffixDirectory() const;
const QString &getUserDirectory() const;
bool getIsFavorite() const;
PtrTo_RowFolderRecord getChildFolderBySuffix(const QString &suffixDir) const;
QList<PtrTo_RowFileRecord> getAllChildRowFileRecords() const;
Expand All @@ -26,9 +27,14 @@ class RowFolderRecord : public BaseRow

bool setParentDirectory(const QString &newParentDirectory);
bool setSuffixDirectory(const QString &newSuffixDirectory);
bool setUserDirectory(const QString &newUserDirectory);
bool setIsFavorite(bool newIsFavorite);

// TODO Remove when V2_DialogAddNewFolder compeleted
bool addChildFolder(const QString &suffixDirectory);

bool addChildFolder(const QString &suffixDirectory, const QString &userDirectory);

private:
RowFolderRecord(const QSqlDatabase &db, const QSqlRecord &record);

Expand All @@ -39,6 +45,7 @@ class RowFolderRecord : public BaseRow

QString parentDirectory;
QString suffixDirectory;
QString userDirectory;
bool isFavorite;
};

Expand Down
25 changes: 25 additions & 0 deletions Backend/FileStorageSubSystem/SqlPrimitives/RowInserter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ PtrTo_RowFolderRecord RowInserter::insertChildFolder(const QString &parentDir, c
return result;
}

PtrTo_RowFolderRecord RowInserter::insertChildFolder(const QString &parentDir, const QString &suffixDir, const QString &userDir)
{
QSqlQuery query(this->getDb());
QString queryTemplate = "INSERT INTO %1 (%2, %3, %4) "
"VALUES(:2, :3, :4);" ;

queryTemplate = queryTemplate.arg(TABLE_NAME_FOLDER_RECORD, // 1
TABLE_FOLDER_RECORD_COLNAME_PARENT_DIRECTORY, // 2
TABLE_FOLDER_RECORD_COLNAME_SUFFIX_DIRECTORY, // 3
TABLE_FOLDER_RECORD_COLNAME_USER_DIRECTORY); // 4

query.prepare(queryTemplate);
query.bindValue(":2", parentDir);
query.bindValue(":3", suffixDir);
query.bindValue(":4", userDir);
query.exec();

this->setLastError(query.lastError());

QueryFolderRecord queries(this->getDb());
PtrTo_RowFolderRecord result = queries.selectRowByDirectory(parentDir + suffixDir);

return result;
}

PtrTo_RowFileRecord RowInserter::insertActiveFile(const QString &fileName,
const QString &fileExtension,
const QString &symbolDiectory,
Expand Down
18 changes: 16 additions & 2 deletions Backend/FileStorageSubSystem/SqlPrimitives/RowInserter.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ class I_InsertFolderRecord : virtual public BaseSqlPrimitive
{
public:
virtual PtrTo_RowFolderRecord insertRootFolder(const QString &rootDir) = 0;
virtual PtrTo_RowFolderRecord insertChildFolder(const QString &parentDir, const QString &suffixDir) = 0;

// TODO Remove when V2_DialogAddNewFolder compeleted
virtual PtrTo_RowFolderRecord insertChildFolder(const QString &parentDir,
const QString &suffixDir) = 0;

virtual PtrTo_RowFolderRecord insertChildFolder(const QString &parentDir,
const QString &suffixDir,
const QString &userDir) = 0;
};


Expand Down Expand Up @@ -74,7 +81,14 @@ class RowInserter : public I_InsertFolderRecord,

//I_InsertFolderRecord
PtrTo_RowFolderRecord insertRootFolder(const QString &rootDir) override;
PtrTo_RowFolderRecord insertChildFolder(const QString &parentDir, const QString &suffixDir) override;

// TODO Remove when V2_DialogAddNewFolder compeleted
PtrTo_RowFolderRecord insertChildFolder(const QString &parentDir,
const QString &suffixDir) override;

PtrTo_RowFolderRecord insertChildFolder(const QString &parentDir,
const QString &suffixDir,
const QString &userDir) override;

//I_InsertFileRecord
PtrTo_RowFileRecord insertActiveFile(const QString &fileName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ inline const QString TABLE_NAME_FOLDER_RECORD = "FolderRecord";
inline const QString TABLE_FOLDER_RECORD_COLNAME_FOLDER_ID = "folder_id";
inline const QString TABLE_FOLDER_RECORD_COLNAME_PARENT_DIRECTORY = "parent_directory";
inline const QString TABLE_FOLDER_RECORD_COLNAME_SUFFIX_DIRECTORY = "suffix_directory";
inline const QString TABLE_FOLDER_RECORD_COLNAME_USER_DIRECTORY = "user_directory";
inline const QString TABLE_FOLDER_RECORD_COLNAME_DIRECTORY = "directory";
inline const QString TABLE_FOLDER_RECORD_COLNAME_IS_FAVORITE = "is_favorite";

Expand Down
Loading

0 comments on commit 7cfc628

Please sign in to comment.