-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRoomRepository.cpp
191 lines (168 loc) · 6.01 KB
/
RoomRepository.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "RoomRepository.h"
namespace DBService {
//RoomRepository::RoomRepository(DBConnection* connection_) : a_dbConnection(connection_) {}
RoomRepository::RoomRepository(const QString& connection_string_) : a_dbConnection(connection_string_) {}
RoomRepository::~RoomRepository() {}
QFuture<QList<DBEntity::DBRoom>> RoomRepository::getAllRooms() {
return QtConcurrent::run([this, query_string_ = "SELECT * from room;"]() {
QList<DBEntity::DBRoom> roomList;
try
{
a_dbConnection.databaseConnectionOpen();
if (a_dbConnection.getDatabase().isOpen()) {
QSqlQueryModel queryModel;
queryModel.setQuery(query_string_);
if (queryModel.lastError().isValid()) {
PLOG_ERROR << "Query error: " << queryModel.lastError().text();
return roomList;
}
const int rowCount = queryModel.rowCount();
for (int i = 0; i < rowCount; ++i) {
qint32 id = queryModel.record(i).value("id").toInt();
QString name = queryModel.record(i).value("name").toString();
QString description = queryModel.record(i).value("description").toString();
qint32 topic_id = queryModel.record(i).value("topic_id").toInt();
bool is_private = queryModel.record(i).value("is_private").toBool();
QString password = queryModel.record(i).value("password").toString();
bool is_deleted = queryModel.record(i).value("is_deleted").toBool();
DBEntity::DBRoom room(id, name, description, topic_id, is_private, password, is_deleted);
roomList.append(room);
}
a_dbConnection.databaseConnectionClose();
if (!roomList.isEmpty()) {
return roomList;
}
else {
PLOG_INFO << "No data found.";
}
}
else {
PLOG_ERROR << "Cannot connect to the data base.";
a_dbConnection.databaseConnectionClose();
}
}
catch (const std::exception& exception)
{
PLOG_ERROR << "Exception in getAllRooms method: " << exception.what();
}
return roomList;
});
}
QFuture<QList<DBEntity::DBRoom>> RoomRepository::getAllActiveRooms() {
return QtConcurrent::run([this, query_string_ = "SELECT * from room WHERE is_deleted=0;"]() {
QList<DBEntity::DBRoom> roomList;
try
{
a_dbConnection.databaseConnectionOpen();
if (a_dbConnection.getDatabase().isOpen()) {
QSqlQueryModel queryModel;
queryModel.setQuery(query_string_);
if (queryModel.lastError().isValid()) {
PLOG_ERROR << "Query error: " << queryModel.lastError().text();
return roomList;
}
const int rowCount = queryModel.rowCount();
for (int i = 0; i < rowCount; ++i) {
qint32 id = queryModel.record(i).value("id").toInt();
QString name = queryModel.record(i).value("name").toString();
QString description = queryModel.record(i).value("description").toString();
qint32 topic_id = queryModel.record(i).value("topic_id").toInt();
bool is_private = queryModel.record(i).value("is_private").toBool();
QString password = queryModel.record(i).value("password").toString();
bool is_deleted = queryModel.record(i).value("is_deleted").toBool();
DBEntity::DBRoom room(id, name, description, topic_id, is_private, password, is_deleted);
roomList.append(room);
}
a_dbConnection.databaseConnectionClose();
if (!roomList.isEmpty()) {
return roomList;
}
else {
PLOG_INFO << "No data found.";
}
}
else {
PLOG_ERROR << "Cannot connect to the data base.";
}
}
catch (const std::exception& exception)
{
PLOG_ERROR << "Exception in getAllActiveRooms method: " << exception.what();
}
return roomList;
});
}
QFuture<qint32> RoomRepository::createRoom( const DBEntity::DBRoom& room_) {
return QtConcurrent::run([this, query_string_ = "INSERT INTO room (name, description, topic_id, is_private, password, is_deleted) VALUES (:name, :description, :topic_id, :is_private, :password, :is_deleted)", room_]() {
try
{
a_dbConnection.databaseConnectionOpen();
if (a_dbConnection.getDatabase().isOpen()) {
QSqlQuery query;
query.prepare(query_string_);
query.bindValue(":name", room_.getName());
query.bindValue(":description", room_.getDescription());
query.bindValue(":topic_id", room_.getTopicId());
query.bindValue(":is_private", room_.isPrivate());
query.bindValue(":password", room_.getPassword());
query.bindValue(":is_deleted", false);
if (query.exec()) {
qint32 id = query.lastInsertId().toInt();
PLOG_INFO << "ID of a new Room db entity: " << id;
a_dbConnection.databaseConnectionClose();
return id;
}
else {
PLOG_ERROR << "Error adding a new Room db entity.";
a_dbConnection.databaseConnectionClose();
return -1;
}
}
else {
PLOG_ERROR << "Cannot connect to the data base.";
a_dbConnection.databaseConnectionClose();
return -1;
}
}
catch (const std::exception& exception)
{
PLOG_ERROR << "Exception in createRoom method: " << exception.what();
return -1;
}
});
}
QFuture<bool> RoomRepository::deleteRoom(const qint32& id_) {
return QtConcurrent::run([this, query_string_ = "UPDATE room SET is_deleted=:is_deleted WHERE id=:id", id_]() {
try
{
a_dbConnection.databaseConnectionOpen();
if (a_dbConnection.getDatabase().isOpen()) {
QSqlQuery query;
query.prepare(query_string_);
query.bindValue(":id", id_);
query.bindValue(":is_deleted", true);
if (query.exec()) {
PLOG_INFO << "Room with ID: " << id_ << " was deleted successfully.";
a_dbConnection.databaseConnectionClose();
return true;
}
else {
PLOG_ERROR << "Room with ID: " << id_ << " was not deleted.";
a_dbConnection.databaseConnectionClose();
return false;
}
}
else {
PLOG_ERROR << "Cannot connect to the data base.";
a_dbConnection.databaseConnectionClose();
return false;
}
}
catch (const std::exception& exception)
{
PLOG_ERROR << "Exception in deleteRoom method: " << exception.what();
return false;
}
});
}
}