Skip to content

Commit c79f4ab

Browse files
committed
add deleteFriend(), modifyFriendDisplayName(), modifyPassword() and
modifyUserProfile()
1 parent 01fe2cc commit c79f4ab

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

db.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,56 @@ bool DB::makeFriend(quint32 uid, quint32 friendID, QString displayName)
118118
q.addBindValue(displayName);
119119
return q.exec();
120120
}
121+
122+
bool DB::removeFriend(quint32 uid, quint32 friendID)
123+
{
124+
prepare();
125+
126+
QSqlQuery q;
127+
128+
q.prepare("delete from tbl_friend where user_id = ? and friend_id = ?");
129+
q.addBindValue(uid);
130+
q.addBindValue(friendID);
131+
return q.exec();
132+
}
133+
134+
bool DB::modifyFriendDisplayName(quint32 uid, quint32 friendID, QString displayName)
135+
{
136+
prepare();
137+
138+
QSqlQuery q;
139+
140+
q.prepare("update tbl_friend set display_name = ? "
141+
"where user_id = ? and friend_id = ?");
142+
q.addBindValue(displayName);
143+
q.addBindValue(uid);
144+
q.addBindValue(friendID);
145+
return q.exec();
146+
}
147+
148+
bool DB::modifyPassword(quint32 uid, QString password)
149+
{
150+
prepare();
151+
152+
QSqlQuery q;
153+
154+
q.prepare("update tbl_user set password = ? where id = ?");
155+
q.addBindValue(password);
156+
q.addBindValue(uid);
157+
return q.exec();
158+
}
159+
160+
bool DB::modifyUserProfile(User user)
161+
{
162+
prepare();
163+
164+
QSqlQuery q;
165+
166+
q.prepare("update tbl_user set nickname = ?, gender = ?, address = ? "
167+
"where id = ?");
168+
q.addBindValue(user.nickname);
169+
q.addBindValue(user.gender);
170+
q.addBindValue(user.address);
171+
q.addBindValue(user.id);
172+
return q.exec();
173+
}

db.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class DB
2525
bool getFriendList(quint32 id, QList<User> &userList, QList<QString> &displayNameList);
2626
QString getFriendDisplayName(quint32 uid, quint32 friendID);
2727
bool makeFriend(quint32 uid, quint32 friendID, QString displayName);
28+
bool removeFriend(quint32 uid, quint32 friendID);
29+
bool modifyFriendDisplayName(quint32 uid, quint32 friendID, QString displayName);
30+
bool modifyPassword(quint32 uid, QString password);
31+
bool modifyUserProfile(User user);
2832

2933
private:
3034
void prepare();

server.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,23 @@ bool Server::process(Connection &conn, QDataStream &in, QDataStream &out)
142142
case 0x04:
143143
return getUserProfile(conn, in, out);
144144

145+
case 0x05:
146+
return deleteFriend(conn, in, out);
147+
145148
case 0x06:
146149
return addFriend(conn, in, out);
147150

148151
case 0x07:
149152
return getFriendIPAndPort(conn, in, out);
153+
154+
case 0xE0:
155+
return modifyFriendDisplayName(conn, in, out);
156+
157+
case 0xE1:
158+
return modifyPassword(conn, in, out);
159+
160+
case 0xE2:
161+
return modifyUserProfile(conn, in, out);
150162
}
151163
return true;
152164
}
@@ -292,6 +304,45 @@ bool Server::getFriendIPAndPort(Connection &conn, QDataStream &in, QDataStream &
292304
return true;
293305
}
294306

307+
bool Server::deleteFriend(Connection &conn, QDataStream &in, QDataStream &out)
308+
{
309+
quint32 uid;
310+
311+
in >> uid;
312+
out << db.removeFriend(conn.account, uid);
313+
return true;
314+
}
315+
316+
bool Server::modifyFriendDisplayName(Connection &conn, QDataStream &in, QDataStream &out)
317+
{
318+
quint32 uid;
319+
QString displayName;
320+
321+
in >> uid >> displayName;
322+
out << db.modifyFriendDisplayName(conn.account, uid, displayName);
323+
return true;
324+
}
325+
326+
bool Server::modifyPassword(Connection &conn, QDataStream &in, QDataStream &out)
327+
{
328+
QString password;
329+
330+
in >> password;
331+
password = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Sha1).toHex();
332+
out << db.modifyPassword(conn.account, password);
333+
return true;
334+
}
335+
336+
bool Server::modifyUserProfile(Connection &conn, QDataStream &in, QDataStream &out)
337+
{
338+
User user;
339+
340+
in >> user.nickname >> user.gender >> user.address;
341+
user.id = conn.account;
342+
out << db.modifyUserProfile(user);
343+
return true;
344+
}
345+
295346
void Server::clientDisconnected()
296347
{
297348
QTcpSocket *socket;

server.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class Server : public QObject
4949
bool getUserProfile(Connection &conn, QDataStream &in, QDataStream &out);
5050
bool addFriend(Connection &conn, QDataStream &in, QDataStream &out);
5151
bool getFriendIPAndPort(Connection &conn, QDataStream &in, QDataStream &out);
52+
bool deleteFriend(Connection &conn, QDataStream &in, QDataStream &out);
53+
bool modifyFriendDisplayName(Connection &conn, QDataStream &in, QDataStream &out);
54+
bool modifyPassword(Connection &conn, QDataStream &in, QDataStream &out);
55+
bool modifyUserProfile(Connection &conn, QDataStream &in, QDataStream &out);
5256

5357
private:
5458
Config config;

0 commit comments

Comments
 (0)