forked from ondra/colnod-connector
Working version for sync data from colnod to backend DB
This commit is contained in:
parent
a510938999
commit
ce7317c510
@ -1,6 +1,5 @@
|
||||
#include <QCoreApplication>
|
||||
|
||||
#include "data.h"
|
||||
#include "datamanager.h"
|
||||
#include "colnodapi.h"
|
||||
|
||||
|
||||
@ -3,7 +3,6 @@ INCLUDEPATH += $$PWD
|
||||
message($$PWD)
|
||||
SOURCES += \
|
||||
$$PWD/colnodapi.cpp \
|
||||
$$PWD/data.cpp \
|
||||
$$PWD/databasemanager.cpp \
|
||||
$$PWD/datamanager.cpp \
|
||||
$$PWD/entity.cpp \
|
||||
@ -14,7 +13,6 @@ SOURCES += \
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/colnodapi.h \
|
||||
$$PWD/data.h \
|
||||
$$PWD/databasemanager.h \
|
||||
$$PWD/datamanager.h \
|
||||
$$PWD/entity.h \
|
||||
|
||||
@ -2,7 +2,6 @@
|
||||
#define COLNODAPI_H
|
||||
|
||||
#include "requestmanager.h"
|
||||
#include "data.h"
|
||||
#include "datamanager.h"
|
||||
|
||||
|
||||
|
||||
@ -1,7 +0,0 @@
|
||||
#include "data.h"
|
||||
|
||||
Data::Data()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@ -1,24 +0,0 @@
|
||||
#ifndef DATA_H
|
||||
#define DATA_H
|
||||
#include <QObject>
|
||||
|
||||
#include "subject.h"
|
||||
#include "token.h"
|
||||
#include <qjsondocument.h>
|
||||
#include <qjsonarray.h>
|
||||
|
||||
class Data : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Data();
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // DATA_H
|
||||
@ -17,24 +17,49 @@ DatabaseManager::DatabaseManager(QString host, QString dbName, QString user, QSt
|
||||
query = QSqlQuery("query", db);
|
||||
}
|
||||
|
||||
void DatabaseManager::saveSubject(Subject* subject, QString table)
|
||||
bool DatabaseManager::saveSubject(Subject* subject, QString table)
|
||||
{
|
||||
query.prepare("INSERT INTO subject (id, name) VALUES (:id, :name)");
|
||||
query.bindValue(":table", table);
|
||||
query.bindValue(":id",subject->getId().toString());
|
||||
qDebug() << subject->getId().toString() << subject->getName();
|
||||
QString id = subject->idToStringWithoutBraces();
|
||||
if (checkIfExists(id, table))
|
||||
return false;
|
||||
query.prepare(QString("INSERT INTO %1 (id, name, last_update) "
|
||||
"VALUES (:id, :name, :last_update)").arg(table));
|
||||
query.bindValue(":id", id);
|
||||
query.bindValue(":name", subject->getName());
|
||||
query.bindValue(":last_update", QDateTime::fromMSecsSinceEpoch(subject->getLastUpdate()).toString("yyyy-MM-dd hh:mm:ss.zzz"));
|
||||
query.exec();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DatabaseManager::saveToken(Token* token, QString table)
|
||||
{
|
||||
QString id = token->idToStringWithoutBraces();
|
||||
if (checkIfExists(id, table))
|
||||
return false;
|
||||
query.prepare(QString("INSERT INTO %1 (id, last_update, token_data, subject_id) "
|
||||
"VALUES (:id, :last_update, :data, :subject_id)").arg(table));
|
||||
query.bindValue(":id", id);
|
||||
query.bindValue(":last_update", QDateTime::fromMSecsSinceEpoch(token->getLastUpdate()).toString("yyyy-MM-dd hh:mm:ss.zzz"));
|
||||
query.bindValue(":data", token->getTokenData());
|
||||
query.bindValue(":subject_id", token->subjectIdToStringWithoutBraces());
|
||||
query.exec();
|
||||
return true;
|
||||
}
|
||||
|
||||
void DatabaseManager::saveSubjectAttribute(Subject *subject, QString table)
|
||||
{
|
||||
auto attributes = subject->getAtr();
|
||||
for (auto attribute : attributes){
|
||||
query.prepare(QString("INSERT INTO %1 (id, attribute_key, attribute_value, subject_id)"
|
||||
"VALUES (:id, :key, :value, :subject_id)").arg(table));
|
||||
query.bindValue(":id", subject->idToStringWithoutBraces() + " " + attribute.first);
|
||||
query.bindValue(":key", attribute.first);
|
||||
query.bindValue(":value", attribute.second);
|
||||
query.bindValue(":subject_id", subject->idToStringWithoutBraces());
|
||||
query.exec();
|
||||
}
|
||||
|
||||
void DatabaseManager::saveToken(Token* token, QString table)
|
||||
{
|
||||
query.prepare("INSERT INTO :table (id, name) VALUES (:id, :data, :subject_id)");
|
||||
query.bindValue(":table", table);
|
||||
query.bindValue(":id",token->getId());
|
||||
query.bindValue(":data", token->getTokenData());
|
||||
query.bindValue(":subject_id", token->getSubjectId());
|
||||
query.exec();
|
||||
|
||||
}
|
||||
|
||||
QString DatabaseManager::getTestMessage(int id)
|
||||
@ -43,7 +68,6 @@ QString DatabaseManager::getTestMessage(int id)
|
||||
query.bindValue(":id", id);
|
||||
query.exec();
|
||||
query.next();
|
||||
//qDebug() << query.value(0);
|
||||
return query.value(0).toString();
|
||||
}
|
||||
|
||||
@ -52,9 +76,6 @@ void DatabaseManager::createTestRecord(QString msg)
|
||||
query.exec("SELECT MAX(id) FROM test");
|
||||
query.next();
|
||||
int id = query.value(0).toInt() + 1;
|
||||
//QString statement = QString("INSERT INTO test (id, message) VALUES (%1, '%2')").arg(id).arg(msg);
|
||||
//query.exec(statement);
|
||||
|
||||
query.prepare("INSERT INTO test (id, message) VALUES (:id, :msg)");
|
||||
query.bindValue(":id", id);
|
||||
query.bindValue(":msg", msg);
|
||||
@ -63,3 +84,13 @@ void DatabaseManager::createTestRecord(QString msg)
|
||||
qDebug() << "U inserted new row, id: " << id << "msg: " << getTestMessage(id);
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool DatabaseManager::checkIfExists(QString id, QString table)
|
||||
{
|
||||
query.prepare(QString("SELECT exists(SELECT 1 FROM %1 WHERE id = :id)").arg(table));
|
||||
query.bindValue(":id", id);
|
||||
query.exec();
|
||||
query.next();
|
||||
return query.value(0).toBool();
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
#include <QDebug>
|
||||
#include "subject.h"
|
||||
#include "token.h"
|
||||
#include <QDateTime>
|
||||
|
||||
|
||||
class DatabaseManager : public QObject
|
||||
@ -17,8 +18,9 @@ class DatabaseManager : public QObject
|
||||
public:
|
||||
DatabaseManager(QString host, QString dbName, QString user, QString password);
|
||||
|
||||
void saveSubject(Subject* subject, QString table);
|
||||
void saveToken(Token* token, QString table);
|
||||
bool saveSubject(Subject* subject, QString table);
|
||||
bool saveToken(Token* token, QString table);
|
||||
void saveSubjectAttribute(Subject* subject, QString table);
|
||||
|
||||
QString getTestMessage(int id);
|
||||
void createTestRecord(QString msg);
|
||||
@ -26,6 +28,11 @@ public:
|
||||
|
||||
bool getOpened() {return opened;}
|
||||
|
||||
|
||||
private:
|
||||
bool checkIfExists(QString id, QString table);
|
||||
|
||||
|
||||
private:
|
||||
QSqlDatabase db;
|
||||
QSqlQuery query;
|
||||
|
||||
@ -32,13 +32,24 @@ void DataManager::localSaveTokens(QString response)
|
||||
|
||||
void DataManager::pushDataToDB()
|
||||
{
|
||||
QSqlDatabase::database().transaction();
|
||||
qDebug() << "In progress...";
|
||||
int counter = 0;
|
||||
for (auto subject : subjects)
|
||||
{
|
||||
db->saveSubject(subject, "subject");
|
||||
if (db->saveSubject(subject, "subject"))
|
||||
counter++;
|
||||
db->saveSubjectAttribute(subject, "subject_attribute");
|
||||
}
|
||||
|
||||
qDebug() << counter << " subjects has been updated";
|
||||
counter = 0;
|
||||
for (auto token : tokens)
|
||||
{
|
||||
db->saveToken(token, "Subjects");
|
||||
if (db->saveToken(token, "token"))
|
||||
counter++;
|
||||
}
|
||||
QSqlDatabase::database().commit();
|
||||
qDebug() << counter << " tokens has been updated";
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -7,7 +7,6 @@
|
||||
#include "databasemanager.h"
|
||||
#include <QJsonDocument>
|
||||
#include <qjsonarray.h>
|
||||
#include "data.h"
|
||||
|
||||
|
||||
class DataManager : public QObject
|
||||
@ -23,6 +22,8 @@ public:
|
||||
void pushDataToDB();
|
||||
|
||||
|
||||
|
||||
|
||||
private:
|
||||
DatabaseManager *db = nullptr;
|
||||
QList<QPointer<Subject>> subjects;
|
||||
|
||||
@ -33,6 +33,11 @@ void Entity::setLastUpdate(uint64_t lastUpdate) {
|
||||
this->lastUpdate = lastUpdate;
|
||||
}
|
||||
|
||||
QString Entity::idToStringWithoutBraces()
|
||||
{
|
||||
return id.toString().mid(1,36);
|
||||
}
|
||||
|
||||
void Entity::fromJSON(const QJsonObject &json) {
|
||||
id = json.value("id").toString();
|
||||
name = json.value("name").toString();
|
||||
|
||||
@ -23,6 +23,8 @@ public:
|
||||
uint64_t getLastUpdate() const;
|
||||
void setLastUpdate(uint64_t lastUpdate);
|
||||
|
||||
QString idToStringWithoutBraces();
|
||||
|
||||
virtual void fromJSON(const QJsonObject &json);
|
||||
virtual QJsonObject toJSON() const;
|
||||
protected:
|
||||
|
||||
@ -11,10 +11,13 @@ QString Subject::getEmail() const {
|
||||
void Subject::fromJSON(const QJsonObject &json) {
|
||||
Entity::fromJSON(json);
|
||||
|
||||
//attributes = json["attributes"]["items"].toObject();
|
||||
|
||||
attributes = json.value("attributes").toObject().value("items").toObject();
|
||||
|
||||
for (int i = 0; i < attributeTypes->length(); i++)
|
||||
{
|
||||
if (!attributes[attributeTypes[i]].toString().isEmpty()){
|
||||
atr.append(qMakePair(attributeTypes[i], attributes[attributeTypes[i]].toString()));
|
||||
}
|
||||
}
|
||||
const auto &tokenFlagsArray = json.value("tokenFlags").toArray();
|
||||
for(const auto &flag : tokenFlagsArray){
|
||||
tokenFlags.insert(flag.toString());
|
||||
@ -72,3 +75,11 @@ const QSet<QString> &Subject::getPinFlags() const {
|
||||
void Subject::setPinFlags(const QSet<QString> &pinFlags) {
|
||||
Subject::pinFlags = pinFlags;
|
||||
}
|
||||
|
||||
const QList<QPair<QString, QString>> &Subject::getAtr() const {
|
||||
return atr;
|
||||
}
|
||||
|
||||
void Subject::setAtr(const QList<QPair<QString, QString>> &atr) {
|
||||
this->atr = atr;
|
||||
}
|
||||
|
||||
@ -4,7 +4,9 @@
|
||||
#include <QSet>
|
||||
|
||||
#include "entity.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QPointer>
|
||||
#include <QPair>
|
||||
|
||||
class Subject : public Entity {
|
||||
|
||||
@ -21,6 +23,11 @@ public:
|
||||
void setPin(const QString &pin);
|
||||
const QSet<QString> &getPinFlags() const;
|
||||
void setPinFlags(const QSet<QString> &pinFlags);
|
||||
// const QList<QPointer<Attribute>> &getAtr() const;
|
||||
// void setAtr(const QList<QPointer<Attribute>> &atr);
|
||||
|
||||
const QList<QPair<QString, QString>> &getAtr() const;
|
||||
void setAtr(const QList<QPair<QString, QString>> &atr);
|
||||
|
||||
void fromJSON(const QJsonObject &json) override;
|
||||
QJsonObject toJSON() const override;
|
||||
@ -29,6 +36,10 @@ protected:
|
||||
QSet<QString> tokenFlags;
|
||||
QString pin;
|
||||
QSet<QString> pinFlags;
|
||||
//QList<QPointer<Attribute>> atr;
|
||||
QList<QPair<QString, QString>> atr;
|
||||
const QString attributeTypes[6] = {"Jmeno", "Prijmeni", "ProfilParking", "Spolecnost", "Profil", "spz"};
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -106,3 +106,7 @@ const QUuid &Token::getSubjectId() const {
|
||||
void Token::setSubjectId(const QUuid &subjectId) {
|
||||
Token::subjectId = subjectId;
|
||||
}
|
||||
|
||||
QString Token::subjectIdToStringWithoutBraces(){
|
||||
return subjectId.toString().mid(1,36);
|
||||
}
|
||||
|
||||
@ -29,6 +29,8 @@ public:
|
||||
const QUuid &getSubjectId() const;
|
||||
void setSubjectId(const QUuid &subjectId);
|
||||
|
||||
QString subjectIdToStringWithoutBraces();
|
||||
|
||||
|
||||
void fromJSON(const QJsonObject &json) override;
|
||||
QJsonObject toJSON() const override;
|
||||
|
||||
@ -18,13 +18,14 @@ void UnitTest::loginSuccessfully()
|
||||
//POST request w/o datas
|
||||
void UnitTest::getSubjects()
|
||||
{
|
||||
Response* response = new Response;
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getSubjects();
|
||||
response = testAccess->getSubjects();
|
||||
spy.wait();
|
||||
QVERIFY(spy.isValid());
|
||||
QCOMPARE(spy.count(), 1);
|
||||
QString json = testAccess->getResponse();
|
||||
QString json = response->reply->readAll();
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
|
||||
QVERIFY(doc.isObject());
|
||||
QVERIFY2(doc.object().value("items").isArray(), "No parameter 'id' in response");
|
||||
@ -35,14 +36,15 @@ void UnitTest::getSubjects()
|
||||
|
||||
void UnitTest::getTokens()
|
||||
{
|
||||
Response* response = new Response;
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getTokens();
|
||||
response = testAccess->getTokens();
|
||||
QCOMPARE(spy.count(), 0);
|
||||
spy.wait();
|
||||
QVERIFY(spy.isValid());
|
||||
QCOMPARE(spy.count(), 1);
|
||||
QString json = testAccess->getResponse();
|
||||
QString json = response->reply->readAll();
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
|
||||
QVERIFY(doc.isObject());
|
||||
QVERIFY2(doc.object().value("items").isArray(), "No parameter 'id' in response");
|
||||
@ -52,16 +54,17 @@ void UnitTest::getTokens()
|
||||
|
||||
void UnitTest::getTokenSubject()
|
||||
{
|
||||
Response* response = new Response;
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getTokenSubject();
|
||||
response = testAccess->getTokenSubject();
|
||||
QCOMPARE(spy.count(), 0);
|
||||
spy.wait();
|
||||
QVERIFY(spy.isValid());
|
||||
QCOMPARE(spy.count(), 1);
|
||||
QString response = testAccess->getResponse();
|
||||
qDebug() << response;
|
||||
QVERIFY(!response.isEmpty());
|
||||
QString json = response->reply->readAll();
|
||||
qDebug() << json;
|
||||
QVERIFY(!json.isEmpty());
|
||||
}
|
||||
|
||||
void UnitTest::subjectJSONParser()
|
||||
@ -79,13 +82,13 @@ void UnitTest::subjectJSONParser()
|
||||
QJsonObject obj = doc.object();
|
||||
QJsonArray arr = obj["items"].toArray();
|
||||
*/
|
||||
|
||||
Response* response = new Response;
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getSubjects();
|
||||
response = testAccess->getSubjects();
|
||||
spy.wait();
|
||||
QString response = testAccess->getResponse();
|
||||
QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
|
||||
QString json = response->reply->readAll();
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
|
||||
QJsonArray arr = doc.object()["items"].toArray();
|
||||
auto testSubject = new Subject();
|
||||
testSubject->fromJSON(arr[1].toObject());
|
||||
@ -95,12 +98,13 @@ void UnitTest::subjectJSONParser()
|
||||
|
||||
void UnitTest::tokenJSONParser()
|
||||
{
|
||||
Response* response = new Response;
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getTokens();
|
||||
response = testAccess->getTokens();
|
||||
spy.wait();
|
||||
QString response = testAccess->getResponse();
|
||||
QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
|
||||
QString json = response->reply->readAll();
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
|
||||
QJsonArray arr = doc.object()["items"].toArray();
|
||||
auto testToken = new Token();
|
||||
testToken->fromJSON(arr[5].toObject());
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
#include "databasemanager.h"
|
||||
#include <QSslSocket>
|
||||
#include "datamanager.h"
|
||||
#include "response.h"
|
||||
|
||||
|
||||
class UnitTest : public QObject
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user