Improve code structure

This commit is contained in:
Pierre HUBERT 2020-06-13 09:49:24 +02:00
parent dde3ad35df
commit b9b509aca1
5 changed files with 107 additions and 27 deletions

View File

@ -14,12 +14,14 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \ SOURCES += \
accounthelper.cpp \
apirequest.cpp \ apirequest.cpp \
apiresponse.cpp \ apiresponse.cpp \
main.cpp \ main.cpp \
loginwindow.cpp loginwindow.cpp
HEADERS += \ HEADERS += \
accounthelper.h \
apirequest.h \ apirequest.h \
apiresponse.h \ apiresponse.h \
config.h \ config.h \

42
accounthelper.cpp Normal file
View File

@ -0,0 +1,42 @@
#include "accounthelper.h"
#include "apirequest.h"
AccountHelper::AccountHelper()
{
}
void AccountHelper::LoginUser(const QString &email, const QString &password, const vLoginCallback cb)
{
auto req = new APIRequest("account/login");
req->addString("userMail", email);
req->addString("userPassword", password);
req->exec();
QObject::connect(req, &APIRequest::done, [=] (APIResponse res) {
QString token;
switch(res.getCode()) {
case 200:
// Save login token
token = res.getObject().value("tokens").toObject().value("token1").toString();
cb(LoginResult::SUCCESS);
break;
case 401:
cb(LoginResult::BAD_PASSWORD);
break;
case 429:
cb(LoginResult::TOO_MANY_REQUEST);
break;
default:
cb(LoginResult::ERROR);
break;
}
});
}

35
accounthelper.h Normal file
View File

@ -0,0 +1,35 @@
/**
* Login helper
*
* @author Pierre Hubert
*/
#pragma once
#include <functional>
#include <QObject>
enum LoginResult {
SUCCESS,
BAD_PASSWORD,
TOO_MANY_REQUEST,
ERROR
};
typedef std::function<void (LoginResult)> vLoginCallback;
class AccountHelper
{
public:
AccountHelper();
/**
* Attempt to login user
*
* @param email User email address
* @param password User password
*/
static void LoginUser(const QString &email, const QString &password, const vLoginCallback cb);
};

View File

@ -1,3 +1,4 @@
#include "accounthelper.h"
#include "apirequest.h" #include "apirequest.h"
#include "loginwindow.h" #include "loginwindow.h"
#include "ui_loginwindow.h" #include "ui_loginwindow.h"
@ -51,24 +52,29 @@ void LoginWindow::submitForm()
setLoading(true); setLoading(true);
auto req = new APIRequest("account/login"); AccountHelper::LoginUser(
req->addString("userMail", ui->emailEdit->text()); ui->emailEdit->text(),
req->addString("userPassword", ui->passwordEdit->text()); ui->passwordEdit->text(),
req->exec(); [&](LoginResult res) {
connect(req, &APIRequest::done, this, &LoginWindow::onResponse); this->onResponse(res);
});
} }
void LoginWindow::onResponse(APIResponse res) void LoginWindow::onResponse(LoginResult res)
{ {
if(res.isError()) {
QString msg; QString msg;
switch(res.getCode()) { switch(res) {
case 401:
case SUCCESS:
QMessageBox::information(this, "ok", "success");
return;
case BAD_PASSWORD:
msg = tr("Invalid credentials!"); msg = tr("Invalid credentials!");
break; break;
case 429: case TOO_MANY_REQUEST:
msg = tr("Too many login attempt, please try again later!"); msg = tr("Too many login attempt, please try again later!");
break; break;
@ -79,12 +85,6 @@ void LoginWindow::onResponse(APIResponse res)
setLoading(false); setLoading(false);
QMessageBox::warning(this, tr("Login failed"), msg); QMessageBox::warning(this, tr("Login failed"), msg);
return;
}
auto token = res.getObject().value("tokens").toObject().value("token1").toString();
QMessageBox::information(this, "ok", token);
} }
void LoginWindow::on_submitButton_clicked() void LoginWindow::on_submitButton_clicked()

View File

@ -7,6 +7,7 @@
#pragma once #pragma once
#include "apiresponse.h" #include "apiresponse.h"
#include "accounthelper.h"
#include <QDialog> #include <QDialog>
#include <QMouseEvent> #include <QMouseEvent>
@ -33,7 +34,7 @@ private slots:
*/ */
void submitForm(); void submitForm();
void onResponse(APIResponse res); void onResponse(LoginResult res);
void on_closeButton_clicked(); void on_closeButton_clicked();