Add profession bindings and missing for user
This commit is contained in:
parent
91a5de78e9
commit
59344e6464
3 changed files with 46 additions and 10 deletions
|
|
@ -52,11 +52,51 @@ QVector<User*> Database::getUsersByType(UserType type) {
|
||||||
return findUserByPredicat(pred);
|
return findUserByPredicat(pred);
|
||||||
}
|
}
|
||||||
|
|
||||||
QVector<User*> Database::getUsersByProfession(Profession prof) {
|
QVector<User*> Database::getUsersByProfession(PID pid) {
|
||||||
auto pred = [prof](User u) { return u.hasProfession(prof.pID()); };
|
auto pred = [pid](User u) { return u.hasProfession(pid); };
|
||||||
return findUserByPredicat(pred);
|
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() {
|
void Database::save() {
|
||||||
QFile f(Database::mFilename);
|
QFile f(Database::mFilename);
|
||||||
f.open(QIODevice::ReadOnly);
|
f.open(QIODevice::ReadOnly);
|
||||||
|
|
|
||||||
|
|
@ -34,9 +34,12 @@ public:
|
||||||
User* getUser(UID uid);
|
User* getUser(UID uid);
|
||||||
User* getUser(QString login);
|
User* getUser(QString login);
|
||||||
QVector<User*> getUsersByType(UserType type);
|
QVector<User*> getUsersByType(UserType type);
|
||||||
QVector<User*> getUsersByProfession(Profession prof);
|
QVector<User*> getUsersByProfession(PID pid);
|
||||||
|
bool removeUser(UID uid);
|
||||||
|
|
||||||
|
bool addProfession(Profession prof);
|
||||||
Profession* getProfession(PID pid);
|
Profession* getProfession(PID pid);
|
||||||
|
bool removeProfession(PID pid);
|
||||||
|
|
||||||
void save();
|
void save();
|
||||||
void load();
|
void load();
|
||||||
|
|
|
||||||
|
|
@ -86,13 +86,6 @@ bool User::setCurrentProfession(PID pid) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void User::removeProfession(PID pid) {
|
void User::removeProfession(PID pid) {
|
||||||
// QMutableVectorIterator<UserProfession> i(mProfessions);
|
|
||||||
// while (i.hasNext()) {
|
|
||||||
// if (i.value().getProfession() == pid) {
|
|
||||||
// i.remove();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
auto pred = [pid](UserProfession p) {
|
auto pred = [pid](UserProfession p) {
|
||||||
return p.getProfession() == pid;
|
return p.getProfession() == pid;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue