From 46d692c53482b4da2aab35beff667432c608622d Mon Sep 17 00:00:00 2001 From: Kevin Whitaker Date: Wed, 17 Jun 2020 14:08:09 -0400 Subject: [PATCH] Add missing files. --- src/jsonio.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/jsonio.h | 41 ++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/jsonio.cpp create mode 100644 src/jsonio.h diff --git a/src/jsonio.cpp b/src/jsonio.cpp new file mode 100644 index 0000000..3512a26 --- /dev/null +++ b/src/jsonio.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2020 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "jsonio.h" +#include +#include +#include +#include +#include +#include +#include + +JsonIO::JsonIO(SqlVehicle* vehicles, SqlServiceRecord* records, QObject* parent) +{ + this->vehicles = vehicles; + this->records = records; +} + +bool JsonIO::importCarfaxJsonToDB(QString fileLocation) +{ + QFile file(QUrl(fileLocation).toLocalFile()); + file.open(QIODevice::ReadOnly); + QString jsonContent; + jsonContent = file.readAll(); + file.close(); + QJsonDocument doc = QJsonDocument::fromJson(jsonContent.toUtf8()); + QJsonObject root = doc.object(); + if(root.contains("vehicle")) { + QJsonObject vehicle = root["vehicle"].toObject(); + QString make = vehicle["make"].toString(); + QString model = vehicle["model"].toString(); + QString year = vehicle["year"].toString(); + QString name = vehicle["nickname"].toString(make+" "+model); + QString vin = vehicle["vin"].toString(); + this->vehicles->addNewVehicle(name, make, model, year.toInt(), nullptr, vin); //TODO: image source? + int vehicleId = vehicles->query().lastInsertId().toInt(); + if(vehicle.contains("serviceRecords")) { + QJsonArray records = vehicle["serviceRecords"].toArray(); + foreach(const QJsonValue & record,records) { + QJsonObject recordObj = record.toObject(); + QString date = recordObj["date"].toString(); + int miles = recordObj["odometer"].toString().replace(",","").toInt(); + QString serviceProvider = recordObj["source"].toArray()[0].toObject()["text"].toString(); + QString notes = recordObj["comments"].toString(""); + QJsonArray details = recordObj["details"].toArray(); + foreach(const QJsonValue & detail, details) { + QString service = detail.toObject()["text"].toString(); + int serviceType = -1; + //Detect some common types + if(service == "Tires rotated") serviceType = 3; + if(service == "Oil and filter changed") serviceType = 0; + this->records->addNewRecord(vehicleId, serviceProvider, serviceType, service, date, miles, notes); + } + } + return true; + } + } + return false; +} + diff --git a/src/jsonio.h b/src/jsonio.h new file mode 100644 index 0000000..432a9e6 --- /dev/null +++ b/src/jsonio.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2020 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef JSONIO_H +#define JSONIO_H + +#include +#include "db/sqlvehicle.h" +#include "db/sqlservicerecord.h" + +/** + * Utility class to handle reading/writing json to/from DB. + */ +class JsonIO : public QObject +{ + Q_OBJECT +public: + JsonIO(SqlVehicle *vehicles, SqlServiceRecord *records, QObject *parent = nullptr); + Q_INVOKABLE bool importCarfaxJsonToDB(QString fileLocation); + +private: + SqlVehicle* vehicles; + SqlServiceRecord* records; + +}; + +#endif // JSONIO_H -- GitLab