Skip to content

Commit 7269934

Browse files
committed
add upload all test to server from client
1 parent b53614c commit 7269934

File tree

6 files changed

+153
-3
lines changed

6 files changed

+153
-3
lines changed

client/mainwindow.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,23 @@ void MainWindow::slotReadyRead()
120120
}
121121
break;
122122
}
123+
124+
case (type_forward::upload_result):
125+
{
126+
auto type = doc.FirstChildElement("body")->FirstChildElement("answer");
127+
if (type->GetText() == QString("correct"))
128+
{
129+
qDebug() << "Загружено на сервер!";
130+
}
131+
else
132+
{
133+
throw "Неверный логин или пароль!";
134+
}
135+
break;
136+
}
123137
}
124138

139+
125140
this->socket->close();
126141
}
127142
catch (const char* e)
@@ -167,6 +182,11 @@ void MainWindow::slotConnected()
167182
info = this->get_user_info();
168183
this->send_auth(info.login, info.password);
169184
break;
185+
186+
case (type_forward::upload_result):
187+
info = this->get_user_info();
188+
this->send_test_result(info.login, info.password);
189+
break;
170190
}
171191
}
172192

@@ -253,6 +273,67 @@ void MainWindow::send_auth(const QString &login, const QString &password)
253273
this->send(printer.CStr());
254274
}
255275

276+
void MainWindow::send_test_result(const QString &login, const QString &password)
277+
{
278+
tinyxml2::XMLDocument doc;
279+
tinyxml2::XMLDeclaration* decl = doc.NewDeclaration("xml version=\"1.1\" encoding=\"UTF-8\"");
280+
doc.InsertEndChild(decl);
281+
282+
tinyxml2::XMLElement* type = doc.NewElement("type");
283+
type->SetText("set_all_result");
284+
doc.InsertEndChild(type);
285+
286+
tinyxml2::XMLElement* body = doc.NewElement("body");
287+
doc.InsertEndChild(body);
288+
289+
tinyxml2::XMLElement* login_xml = doc.NewElement("login");
290+
login_xml->SetText(login.toLocal8Bit().data());
291+
body->InsertEndChild(login_xml);
292+
293+
tinyxml2::XMLElement* password_xml = doc.NewElement("password");
294+
password_xml->SetText(password.toLocal8Bit().data());
295+
body->InsertEndChild(password_xml);
296+
297+
int counter{ 0 };
298+
QSqlQuery query("SELECT COUNT(*) FROM User_test");
299+
if (query.next())
300+
counter = query.value(0).toInt();
301+
tinyxml2::XMLElement* count_test = doc.NewElement("count_test");
302+
count_test->SetText(counter);
303+
body->InsertEndChild(count_test);
304+
305+
if (counter)
306+
{
307+
tinyxml2::XMLElement* tests = doc.NewElement("tests");
308+
309+
QSqlQuery res_tests("SELECT * FROM User_test");
310+
311+
while (res_tests.next())
312+
{
313+
tinyxml2::XMLElement* test = doc.NewElement("test");
314+
315+
tinyxml2::XMLElement* xml_name = doc.NewElement("name");
316+
xml_name->SetText(res_tests.value(0).toString().toUtf8().data());
317+
test->InsertEndChild(xml_name);
318+
319+
tinyxml2::XMLElement* result = doc.NewElement("result");
320+
result->SetText(res_tests.value(1).toString().toUtf8().data());
321+
test->InsertEndChild(result);
322+
323+
tests->InsertEndChild(test);
324+
}
325+
326+
body->InsertEndChild(tests);
327+
}
328+
329+
tinyxml2::XMLPrinter printer;
330+
doc.Print(&printer);
331+
332+
qDebug() << printer.CStr();
333+
334+
this->send(printer.CStr());
335+
}
336+
256337
void MainWindow::fill_db_login(tinyxml2::XMLElement *body)
257338
{
258339
QString login {this->ui->login_lineEdit->text()};
@@ -499,3 +580,14 @@ void MainWindow::on_load_result_clicked()
499580
connect(socket.get(), SIGNAL(readyRead()), SLOT(slotReadyRead()));
500581
connect(socket.get(), SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(slotError(QAbstractSocket::SocketError)));
501582
}
583+
584+
void MainWindow::on_upload_result_clicked()
585+
{
586+
this->forward = type_forward::upload_result;
587+
588+
this->socket.reset(new QTcpSocket());
589+
socket->connectToHost(this->host, this->port);
590+
connect(socket.get(), SIGNAL(connected()), SLOT(slotConnected()));
591+
connect(socket.get(), SIGNAL(readyRead()), SLOT(slotReadyRead()));
592+
connect(socket.get(), SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(slotError(QAbstractSocket::SocketError)));
593+
}

client/mainwindow.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ private slots:
7070

7171
void on_load_result_clicked();
7272

73+
void on_upload_result_clicked();
74+
7375
private:
7476
enum class screen
7577
{
@@ -107,6 +109,7 @@ private slots:
107109
void create_db();
108110

109111
void send_auth(const QString& login, const QString& password);
112+
void send_test_result(const QString& login, const QString& password);
110113

111114
void fill_db_login(tinyxml2::XMLElement* body);
112115
void fill_db_register();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!-- сначала отправляем количество байт, занимаемых xml -->
2+
3+
<!-- Клиент отсылает результаты своих тестов -->
4+
<?xml version="1.1" encoding="UTF-8"?>
5+
<type>set_all_result</type>
6+
<body>
7+
<login>login</login>
8+
<password>password</password>
9+
<count_test>1</count_test>
10+
<tests>
11+
<test>
12+
<name>Адаптер</name>
13+
<result>10</result>
14+
</test>
15+
</tests>
16+
</body>

server/handler_server.cpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ std::string Handler_server::processing(const std::string& data)
3232
return this->result(body->FirstChildElement("login")->GetText(), body->FirstChildElement("password")->GetText(),
3333
body->FirstChildElement("pattern")->GetText(), body->FirstChildElement("result")->GetText());
3434
}
35+
else if (type->GetText() == std::string("set_all_result"))
36+
{
37+
return this->set_all_result(body->FirstChildElement("login")->GetText(), body->FirstChildElement("password")->GetText(), body);
38+
}
3539
else
3640
{
3741
std::cout << "ERROR parse xml: " << " type " << type->GetText() << " not found" << std::endl;
@@ -292,6 +296,40 @@ std::string Handler_server::result(const std::string& login, const std::string&
292296
}
293297
}
294298

299+
std::string Handler_server::set_all_result(const std::string& login, const std::string& password, tinyxml2::XMLElement* body)
300+
{
301+
if (this->is_correct_auth(login, password))
302+
{
303+
int user_id{ db.execAndGet("SELECT id FROM User WHERE login = '" + login + "'") };
304+
305+
db.exec("DELETE FROM User_test WHERE id_user = " + std::to_string(user_id));
306+
307+
int length{ body->FirstChildElement("count_test")->IntText() };
308+
if (length > 0)
309+
{
310+
tinyxml2::XMLElement* tests = body->FirstChildElement("tests");
311+
tinyxml2::XMLElement* test = tests->FirstChildElement("test");
312+
while (test)
313+
{
314+
std::string name{ test->FirstChildElement("name")->GetText() };
315+
std::string result{ test->FirstChildElement("result")->GetText() };
316+
317+
int pattern_id{ db.execAndGet("SELECT id FROM Pattern WHERE name = '" + name + "'") };
318+
319+
db.exec("INSERT INTO User_test (id_user, id_pattern, count_correct) VALUES (" + std::to_string(user_id) + ", " + std::to_string(pattern_id) + ", " + result + ")");
320+
321+
test = test->NextSiblingElement("test");
322+
}
323+
}
324+
325+
return this->correct();
326+
}
327+
else
328+
{
329+
return this->uncorrect();
330+
}
331+
}
332+
295333
bool Handler_server::is_correct_auth(const std::string& login, const std::string& password) const
296334
{
297335
SQLite::Statement query(db, "SELECT COUNT(*) FROM User WHERE login = ? and password = ?");
@@ -373,10 +411,10 @@ void Handler_server::decode_file(const std::string& path, const std::string& dat
373411
tmp_file.close();
374412

375413
std::ofstream file(path, std::ios::out | std::ios::binary);
376-
auto data{ base64_decode(data) };
377-
for (size_t i = 0; i < data.size(); i++)
414+
auto data_file{ base64_decode(data) };
415+
for (size_t i = 0; i < data_file.size(); i++)
378416
{
379-
file << data[i];
417+
file << data_file[i];
380418
}
381419
file.close();
382420
}

server/handler_server.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Handler_server
2323
std::string reg(const std::string& login, const std::string& password, const std::string& img_type, const std::string& img);
2424
std::string patterns() const;
2525
std::string result(const std::string& login, const std::string& password, const std::string& pattern, const std::string& result);
26+
std::string set_all_result(const std::string& login, const std::string& password, tinyxml2::XMLElement* body);
2627

2728
bool is_correct_auth(const std::string& login, const std::string& password) const;
2829
std::string uncorrect() const;

server/main.db

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)