From b9b509aca1094b9e87bfeb08a4e14b399a3ff0ba Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 13 Jun 2020 09:49:24 +0200 Subject: [PATCH] Improve code structure --- ComunicWatcher.pro | 2 ++ accounthelper.cpp | 42 +++++++++++++++++++++++++++++++++++++ accounthelper.h | 35 +++++++++++++++++++++++++++++++ loginwindow.cpp | 52 +++++++++++++++++++++++----------------------- loginwindow.h | 3 ++- 5 files changed, 107 insertions(+), 27 deletions(-) create mode 100644 accounthelper.cpp create mode 100644 accounthelper.h diff --git a/ComunicWatcher.pro b/ComunicWatcher.pro index 9d7a548..27eaa7b 100644 --- a/ComunicWatcher.pro +++ b/ComunicWatcher.pro @@ -14,12 +14,14 @@ DEFINES += QT_DEPRECATED_WARNINGS #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ + accounthelper.cpp \ apirequest.cpp \ apiresponse.cpp \ main.cpp \ loginwindow.cpp HEADERS += \ + accounthelper.h \ apirequest.h \ apiresponse.h \ config.h \ diff --git a/accounthelper.cpp b/accounthelper.cpp new file mode 100644 index 0000000..55b9d88 --- /dev/null +++ b/accounthelper.cpp @@ -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; + } + + }); +} diff --git a/accounthelper.h b/accounthelper.h new file mode 100644 index 0000000..fc5f0f6 --- /dev/null +++ b/accounthelper.h @@ -0,0 +1,35 @@ +/** + * Login helper + * + * @author Pierre Hubert + */ + +#pragma once + +#include + +#include + +enum LoginResult { + SUCCESS, + BAD_PASSWORD, + TOO_MANY_REQUEST, + ERROR +}; + +typedef std::function 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); +}; + diff --git a/loginwindow.cpp b/loginwindow.cpp index b67e087..9abef70 100644 --- a/loginwindow.cpp +++ b/loginwindow.cpp @@ -1,3 +1,4 @@ +#include "accounthelper.h" #include "apirequest.h" #include "loginwindow.h" #include "ui_loginwindow.h" @@ -51,40 +52,39 @@ void LoginWindow::submitForm() setLoading(true); - auto req = new APIRequest("account/login"); - req->addString("userMail", ui->emailEdit->text()); - req->addString("userPassword", ui->passwordEdit->text()); - req->exec(); - connect(req, &APIRequest::done, this, &LoginWindow::onResponse); + AccountHelper::LoginUser( + ui->emailEdit->text(), + ui->passwordEdit->text(), + [&](LoginResult res) { + this->onResponse(res); + }); } -void LoginWindow::onResponse(APIResponse res) +void LoginWindow::onResponse(LoginResult res) { - if(res.isError()) { - QString msg; + QString msg; - switch(res.getCode()) { - case 401: - msg = tr("Invalid credentials!"); - break; - - case 429: - msg = tr("Too many login attempt, please try again later!"); - break; - - default: - msg = tr("An error occured while trying to sign you in!"); - break; - } - - setLoading(false); - QMessageBox::warning(this, tr("Login failed"), msg); + switch(res) { + case SUCCESS: + QMessageBox::information(this, "ok", "success"); return; + + case BAD_PASSWORD: + msg = tr("Invalid credentials!"); + break; + + case TOO_MANY_REQUEST: + msg = tr("Too many login attempt, please try again later!"); + break; + + default: + msg = tr("An error occured while trying to sign you in!"); + break; } - auto token = res.getObject().value("tokens").toObject().value("token1").toString(); - QMessageBox::information(this, "ok", token); + setLoading(false); + QMessageBox::warning(this, tr("Login failed"), msg); } void LoginWindow::on_submitButton_clicked() diff --git a/loginwindow.h b/loginwindow.h index b2294c2..55edf38 100644 --- a/loginwindow.h +++ b/loginwindow.h @@ -7,6 +7,7 @@ #pragma once #include "apiresponse.h" +#include "accounthelper.h" #include #include @@ -33,7 +34,7 @@ private slots: */ void submitForm(); - void onResponse(APIResponse res); + void onResponse(LoginResult res); void on_closeButton_clicked();