From 59344e6464b9461f614285d282c5bda9065e8738 Mon Sep 17 00:00:00 2001 From: "E. Kozlovskaya" Date: Mon, 4 Jan 2021 19:16:29 +0700 Subject: [PATCH] Add profession bindings and missing for user --- iFacility/db/database.cpp | 44 ++++++++++++++++++++++++++++++++++++-- iFacility/db/database.h | 5 ++++- iFacility/objects/user.cpp | 7 ------ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/iFacility/db/database.cpp b/iFacility/db/database.cpp index d0ed8d3..5e796c9 100644 --- a/iFacility/db/database.cpp +++ b/iFacility/db/database.cpp @@ -52,11 +52,51 @@ QVector Database::getUsersByType(UserType type) { return findUserByPredicat(pred); } -QVector Database::getUsersByProfession(Profession prof) { - auto pred = [prof](User u) { return u.hasProfession(prof.pID()); }; +QVector Database::getUsersByProfession(PID pid) { + auto pred = [pid](User u) { return u.hasProfession(pid); }; return findUserByPredicat(pred); } +bool Database::removeUser(UID uid) { + auto prof = getUser(uid); + if (prof == nullptr) { + return false; + } + auto pred = [uid](User u) { return u.uID() == uid; }; + mUsers.erase(std::remove_if(mUsers.begin(), mUsers.end(), pred), + mUsers.end()); + return true; +} + +bool Database::addProfession(Profession prof) { + if (getProfession(prof.pID()) != nullptr) { + return false; + } + mProfessions.push_back(prof); + return true; +} + +Profession* Database::getProfession(PID pid) { + auto it = std::find_if(mProfessions.begin(), mProfessions.end(), + [pid](Profession p) { return p.pID() == pid; }); + return it == mProfessions.end()? nullptr : it; +} + +bool Database::removeProfession(PID pid) { + auto prof = getProfession(pid); + if (prof == nullptr) { + return false; + } + auto assignedUsers = getUsersByProfession(pid); + if (!assignedUsers.isEmpty()) { + return false; + } + auto pred = [pid](Profession p) { return p.pID() == pid; }; + mProfessions.erase(std::remove_if(mProfessions.begin(), mProfessions.end(), pred), + mProfessions.end()); + return true; +} + void Database::save() { QFile f(Database::mFilename); f.open(QIODevice::ReadOnly); diff --git a/iFacility/db/database.h b/iFacility/db/database.h index 73f09a0..5c863af 100644 --- a/iFacility/db/database.h +++ b/iFacility/db/database.h @@ -34,9 +34,12 @@ public: User* getUser(UID uid); User* getUser(QString login); QVector getUsersByType(UserType type); - QVector getUsersByProfession(Profession prof); + QVector getUsersByProfession(PID pid); + bool removeUser(UID uid); + bool addProfession(Profession prof); Profession* getProfession(PID pid); + bool removeProfession(PID pid); void save(); void load(); diff --git a/iFacility/objects/user.cpp b/iFacility/objects/user.cpp index b423a0d..2e1bb05 100644 --- a/iFacility/objects/user.cpp +++ b/iFacility/objects/user.cpp @@ -86,13 +86,6 @@ bool User::setCurrentProfession(PID pid) { } void User::removeProfession(PID pid) { -// QMutableVectorIterator i(mProfessions); -// while (i.hasNext()) { -// if (i.value().getProfession() == pid) { -// i.remove(); -// } -// } - auto pred = [pid](UserProfession p) { return p.getProfession() == pid; };