-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.cpp
120 lines (98 loc) · 3.63 KB
/
user.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "user.h"
User::User()
{
}
User::User(int id, QString firstname, QString lastname, QString birthDate, QIcon* photo, QSqlDatabase* database, QSqlQuery* query)
{
this->id = id;
this->firstname = firstname;
this->lastname = lastname;
this->birthDate = birthDate;
this->photo = photo;
if (!query->exec("SELECT category.idCategory, category.text FROM Category, Category_User, User WHERE Category_User.category = category.idCategory AND Category_User.user = User.idUser AND User.idUser = "
+ QString::number(this->id) + ";")
) qWarning() << "ERROR: no category found";
// Initialisation des catégories
int value = 0;
int idCategory[4];
QString text[4];
// Récupération des valeurs de la réponse
while(query->next())
{
idCategory[value] = query->value("idCategory").toInt();
text[value] = query->value("text").toString();
value++;
}
// Création des catégories de l'utilisateur
for (int i = 0; i < 4; i++) this->category[i].InitiateCategory(query, idCategory[i], text[i]);
}
User::~User()
{
}
int User::GetId()
{
return this->id;
}
QString User::GetFirstname()
{
return this->firstname;
}
QString User::GetLastname()
{
return this->lastname;
}
QString User::GetBirthDate()
{
return this->birthDate;
}
Category* User::GetCategory()
{
return this->category;
}
QIcon* User::GetPhoto()
{
return this->photo;
}
void User::SetFirstname(QString firstname, QSqlQuery* query)
{
this->firstname = firstname;
// La modification est aussi apportée à la base de données
if (!query->exec("UPDATE User SET firstname = \"" + this->firstname + "\" WHERE idUser = " + QString::number(this->id) + ";")) qWarning() << "ERROR : Set firstname for user id = " + this->id;
qDebug() << "UPDATE User SET firstname = \"" + this->firstname + "\" WHERE idUser = " + QString::number(this->id) + ";";
}
void User::SetLastname(QString lastname, QSqlQuery* query)
{
this->lastname = lastname;
// La modification est aussi apportée à la base de données
if (!query->exec("UPDATE User SET lastname = '" + this->lastname + "' WHERE idUser = '" + QString::number(this->id) + "';")) qWarning() << "ERROR : Set lastname for user id = " + this->id;
}
void User::SetBirthDate(QString birthDate, QSqlQuery* query)
{
this->birthDate = birthDate;
// La modification est aussi apportée à la base de données
if (!query->exec("UPDATE User SET birthDate = \"" + this->birthDate + "\" WHERE idUser = '" + QString::number(this->id) + "';")) qWarning() << "ERROR : Set birthdate for user id = " + this->id;
}
void User::SetCategory(QString* category, QSqlQuery* query)
{
// Le texte des 4 catégories est changé
for (int i = 0; i < 4; i++) this->category[i].SetText(category[i]);
// La modification est aussi apportée à la base de données
for each (Category category in this->category) {
if (!query->exec("UPDATE Category SET text = \"" + category.GetText() + "\" WHERE idCategory = " + QString::number(category.GetId()) + ";")) qWarning() << "ERROR : Set category for user id = " + this->id;
}
}
void User::SetPhoto(QString urlPhoto, QSqlQuery* query)
{
this->photo = new QIcon(urlPhoto);
// La modification est aussi apportée à la base de données
if (!query->exec("UPDATE User SET urlPhoto = \"" + urlPhoto + "\" WHERE idUser = " + QString::number(this->id) + ";")) qWarning() << "ERROR : Set urlPhoto for user id = " + this->id;
}
User& User::operator=(const User& user)
{
this->firstname = user.firstname;
this->lastname = user.lastname;
this->birthDate = user.birthDate;
*this->category = *user.category;
this->photo = user.photo;
return *this;
}