Skip to content

Commit

Permalink
Unassign books from deleted descendets from deleted folder
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLazarescu committed Feb 8, 2024
1 parent 79a4268 commit ac61850
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/adapters/controllers/folder_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,15 @@ bool FolderController::createFolder(QString name, QString color, QString icon,
return success;
}

bool FolderController::deleteFolder(QString uuid)
QVariantList FolderController::deleteFolder(QString uuid)
{
return m_folderService->deleteFolder(QUuid(uuid));
auto uuids = m_folderService->deleteFolder(QUuid(uuid));

QVariantList list;
for(auto& id : uuids)
list.append(QVariant::fromValue(id));

return list;
}

void FolderController::updateFolder(QString uuid, QString name, QString color,
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/controllers/folder_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ADAPTERS_EXPORT FolderController : public IFolderController
dtos::FolderDto getFolder(QString uuid) override;
bool createFolder(QString name, QString color, QString icon,
QString description, QString parent = "") override;
bool deleteFolder(QString uuid) override;
QVariantList deleteFolder(QString uuid) override;
void updateFolder(QString uuid, QString name, QString color, QString icon,
QString description) override;
bool moveFolder(QString uuid, QString destUuid) override;
Expand Down
3 changes: 2 additions & 1 deletion src/adapters/interfaces/controllers/i_folder_controller.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <QObject>
#include <QVariantList>
#include "adapters_export.hpp"
#include "folder_dto.hpp"
#include "folders_proxy_model.hpp"
Expand Down Expand Up @@ -34,7 +35,7 @@ class ADAPTERS_EXPORT IFolderController : public QObject
Q_INVOKABLE virtual bool createFolder(QString name, QString color,
QString icon, QString description,
QString parent = "") = 0;
Q_INVOKABLE virtual bool deleteFolder(QString uuid) = 0;
Q_INVOKABLE virtual QVariantList deleteFolder(QString uuid) = 0;
Q_INVOKABLE virtual void updateFolder(QString uuid, QString name,
QString color, QString icon,
QString description) = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/application/interfaces/services/i_folder_service.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <QList>
#include <QObject>
#include <QString>
#include "application_export.hpp"
Expand All @@ -22,7 +23,7 @@ class APPLICATION_EXPORT IFolderService : public QObject
virtual domain::entities::Folder* getFolder(const QUuid& uuid) = 0;
virtual bool createFolder(const QString& name, QString color, QString icon,
QString description, const QUuid& parent) = 0;
virtual bool deleteFolder(const QUuid& uuid) = 0;
virtual QList<QString> deleteFolder(const QUuid& uuid) = 0;
virtual void updateFolder(const domain::entities::Folder& folder) = 0;
virtual bool moveFolder(const QUuid& uuid, const QUuid& destUuid) = 0;

Expand Down
24 changes: 21 additions & 3 deletions src/application/services/folder_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,28 @@ bool FolderService::createFolder(const QString& name, QString color,
return true;
}

bool FolderService::deleteFolder(const QUuid& uuid)
QList<QString> FolderService::deleteFolder(const QUuid& uuid)
{
auto folder = getFolder(uuid);
if(folder == nullptr)
return false;
return {};

auto parent = folder->getParent();
auto indexInParent = folder->getIndexInParent();

// We return a list of all the folders that are deleted (since all
// descendents of the folder are also deleted).
auto listOfDescendents = getUuidsOfAllDescendents(*folder);
listOfDescendents.push_front(uuid.toString(QUuid::WithoutBraces));

emit beginRemoveFolder(parent, indexInParent);
parent->removeChild(uuid);
emit endRemoveFolder();

parent->decreaseChildIndiciesIfBiggerThan(indexInParent);
parent->updateLastModified();
saveChanges();
return true;
return listOfDescendents;
}

void FolderService::updateFolder(const domain::entities::Folder& folder)
Expand Down Expand Up @@ -307,4 +312,17 @@ void FolderService::updateFoldersRecursively(Folder* current,
}
}

QList<QString> FolderService::getUuidsOfAllDescendents(const Folder& folder)
{
QList<QString> uuids;
for(auto& child : folder.getChildren())
{
uuids.push_back(child->getUuid().toString(QUuid::WithoutBraces));
auto childUuids = getUuidsOfAllDescendents(*child);
uuids.append(childUuids);
}

return uuids;
}

} // namespace application::services
4 changes: 3 additions & 1 deletion src/application/services/folder_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class APPLICATION_EXPORT FolderService : public IFolderService
domain::entities::Folder* getFolder(const QUuid& uuid) override;
bool createFolder(const QString& name, QString color, QString icon,
QString description, const QUuid& parent) override;
bool deleteFolder(const QUuid& uuid) override;
QList<QString> deleteFolder(const QUuid& uuid) override;
void updateFolder(const domain::entities::Folder& folder) override;
bool moveFolder(const QUuid& uuid, const QUuid& destUuid) override;

Expand All @@ -39,6 +39,8 @@ private slots:
void saveChanges();
void updateFoldersRecursively(domain::entities::Folder* current,
domain::entities::Folder& remoteFolder);
QList<QString> getUuidsOfAllDescendents(
const domain::entities::Folder& folder);

IFolderStorageGateway* m_folderStorageGateway;
ILocalLibraryTracker* m_localLibraryTracker;
Expand Down
10 changes: 8 additions & 2 deletions src/presentation/homePage/folderSidebar/MDeleteFolderPopup.qml
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,14 @@ Popup {
text: qsTr("Delete")

onClicked: {
FolderController.deleteFolder(uuid)
LibraryController.removeAllBooksFromFolderWithId(uuid)
// Since all the sub folders of the deleted folder will also be deleted,
// we need to un-assign their books as well.
let deletedFolders = FolderController.deleteFolder(uuid)
for (var i = 0; i < deletedFolders.length; i++) {
LibraryController.removeAllBooksFromFolderWithId(
deletedFolders[i])
}

root.close()
}
}
Expand Down

0 comments on commit ac61850

Please sign in to comment.