Add serialization and comparison to User class

This commit is contained in:
E. Kozlovskaya 2021-01-03 23:22:49 +07:00
parent 02869cdf6e
commit de2f8e01b2
2 changed files with 30 additions and 6 deletions

View file

@ -51,18 +51,18 @@ bool User::addProfession(const Profession &p) {
return false; return false;
} }
if (!mProfessions.contains(p)) { if (!mProfessions.contains(p.pID())) {
if (mProfessions.size() >= 4) { if (mProfessions.size() >= 4) {
mProfessions.remove(0); mProfessions.remove(0);
} }
mProfessions.push_back(p); mProfessions.push_back(p.pID());
} }
return true; return true;
} }
bool User::setCurrentProfession(const Profession &p) { bool User::setCurrentProfession(const Profession &p) {
if (!mProfessions.contains(p)) { if (!mProfessions.contains(p.pID())) {
return false; return false;
} }
@ -74,5 +74,25 @@ bool User::setCurrentProfession(const Profession &p) {
} }
void User::removeProfession(const Profession &p) { void User::removeProfession(const Profession &p) {
mProfessions.removeAll(p); mProfessions.removeAll(p.pID());
}
bool operator==(const User &l, const User &r) {
return l.mUID == r.mUID && l.mLogin == r.mLogin;
}
QDataStream& operator<<(QDataStream &stream, const User &usr) {
stream << usr.mUID
<< usr.mLogin << usr.mPassword
<< usr.mFirstName << usr.mSecondName << usr.mPatronymic
<< usr.mProfessions << usr.mCurrentProfession;
return stream;
}
QDataStream& operator>>(QDataStream &stream, User &usr) {
stream >> usr.mUID
>> usr.mLogin >> usr.mPassword
>> usr.mFirstName >> usr.mSecondName >> usr.mPatronymic
>> usr.mProfessions >> usr.mCurrentProfession;
return stream;
} }

View file

@ -8,7 +8,7 @@
#include "profession.h" #include "profession.h"
typedef QUuid UID; typedef QUuid UID;
typedef QVector<Profession> ProfessionsList; typedef QVector<PID> ProfessionsList;
class User { class User {
private: private:
@ -21,9 +21,9 @@ private:
ProfessionsList mProfessions; ProfessionsList mProfessions;
PID mCurrentProfession = 0; PID mCurrentProfession = 0;
public:
User() = default; User() = default;
public:
UID uID() const; UID uID() const;
QString getLogin() const; QString getLogin() const;
bool checkPassword(const QString &password); bool checkPassword(const QString &password);
@ -39,6 +39,10 @@ public:
bool addProfession(const Profession &p); bool addProfession(const Profession &p);
bool setCurrentProfession(const Profession &p); bool setCurrentProfession(const Profession &p);
void removeProfession(const Profession &p); void removeProfession(const Profession &p);
friend bool operator==(const User &l, const User &r);
friend QDataStream& operator<<(QDataStream &stream, const User &usr);
friend QDataStream& operator>>(QDataStream &stream, User &usr);
}; };
#endif // USER_H #endif // USER_H