2024-12-24 18:13:22 +04:00
|
|
|
#include "apiclient.h"
|
|
|
|
|
|
|
|
ApiClient::ApiClient(QObject *parent)
|
2025-01-14 21:33:47 +04:00
|
|
|
: QObject{parent}, _networkManager(new QNetworkAccessManager(this))
|
2024-12-24 18:13:22 +04:00
|
|
|
{}
|
|
|
|
|
|
|
|
void ApiClient::getFilteredDevices(bool isWorking, double priceFrom, double priceTo, bool applyFilters, bool disregardState, int entityId, int currentEntity,
|
|
|
|
const QString &searchText, const QString &sortOrder)
|
|
|
|
{
|
|
|
|
QString url = QString("/api/devices?isWorking=%1&priceFrom=%2&priceTo=%3&applyFilters=%4&disregardState=%5&entityId=%6¤tEntity=%7&searchText=%8&sortOrder=%9")
|
|
|
|
.arg(isWorking ? "true" : "false")
|
|
|
|
.arg(priceFrom)
|
|
|
|
.arg(priceTo)
|
|
|
|
.arg(applyFilters ? "true" : "false")
|
|
|
|
.arg(disregardState ? "true" : "false")
|
|
|
|
.arg(entityId)
|
|
|
|
.arg(currentEntity)
|
|
|
|
.arg(searchText)
|
|
|
|
.arg(sortOrder);
|
|
|
|
|
|
|
|
QNetworkRequest request(baseUrl + url);
|
2025-01-14 21:33:47 +04:00
|
|
|
QNetworkReply *reply = _networkManager->get(request);
|
2024-12-24 18:13:22 +04:00
|
|
|
|
|
|
|
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
|
|
|
parseData(reply);
|
|
|
|
});
|
|
|
|
connect(reply, &QNetworkReply::errorOccurred, this, &ApiClient::handleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::getEntities(const QString &url)
|
|
|
|
{
|
|
|
|
QNetworkRequest request(baseUrl + url);
|
2025-01-14 21:33:47 +04:00
|
|
|
QNetworkReply *reply = _networkManager->get(request);
|
2024-12-24 18:13:22 +04:00
|
|
|
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
|
|
|
parseData(reply);
|
|
|
|
});
|
|
|
|
connect(reply, &QNetworkReply::errorOccurred, this, &ApiClient::handleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::updateDevice(int id, const Device &device)
|
|
|
|
{
|
|
|
|
QString url = QString("/api/devices/%1").arg(id);
|
|
|
|
QNetworkRequest request(baseUrl + url);
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
|
|
|
|
|
|
QJsonDocument doc(device.toJson());
|
|
|
|
QByteArray data = doc.toJson();
|
|
|
|
|
2025-01-14 21:33:47 +04:00
|
|
|
QNetworkReply *reply = _networkManager->put(request, data);
|
2024-12-24 18:13:22 +04:00
|
|
|
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
|
|
|
handleDeviceUpdated(reply);
|
|
|
|
});
|
|
|
|
connect(reply, &QNetworkReply::errorOccurred, this, &ApiClient::handleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::parseData(QNetworkReply *reply)
|
|
|
|
{
|
|
|
|
if (reply->error() == QNetworkReply::NoError) {
|
|
|
|
QByteArray data = reply->readAll();
|
|
|
|
|
|
|
|
QString urlPath = reply->url().path();
|
|
|
|
|
|
|
|
if (urlPath.contains("/api/devices")) {
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(data);
|
|
|
|
QJsonArray devicesArray = doc.object().value("devices").toArray();
|
|
|
|
QList<Device> devices;
|
|
|
|
for (const QJsonValue &value : devicesArray) {
|
|
|
|
devices.append(deserializeDevice(value.toObject()));
|
|
|
|
}
|
|
|
|
emit devicesReceived(devices);
|
|
|
|
}
|
|
|
|
if (urlPath.contains("/api/locations")) {
|
|
|
|
emit locationsReceived(deserializeEntities<Location>(data, "locations"));
|
|
|
|
}
|
|
|
|
if (urlPath.contains("/api/departments")) {
|
|
|
|
emit departmentsReceived(deserializeEntities<Department>(data, "departments"));
|
|
|
|
}
|
|
|
|
if (urlPath.contains("/api/manufacturers")) {
|
|
|
|
emit manufacturersReceived(deserializeEntities<Manufacturer>(data, "manufacturers"));
|
|
|
|
}
|
|
|
|
if (urlPath.contains("/api/devicetypes")) {
|
|
|
|
emit deviceTypesReceived(deserializeEntities<DeviceType>(data, "deviceTypes"));
|
|
|
|
}
|
|
|
|
if (urlPath.contains("/api/devicemodels")) {
|
|
|
|
emit deviceModelsReceived(deserializeEntities<DeviceModel>(data, "deviceModels"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::handleDeviceUpdated(QNetworkReply *reply)
|
|
|
|
{
|
|
|
|
if (reply->error() == QNetworkReply::NoError) {
|
|
|
|
emit deviceUpdated(true);
|
|
|
|
} else {
|
|
|
|
emit deviceUpdated(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::handleError(QNetworkReply::NetworkError error)
|
|
|
|
{
|
2025-01-14 21:33:47 +04:00
|
|
|
qDebug() << "Ошибка во время запроса: " << error;
|
2024-12-24 18:13:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Device ApiClient::deserializeDevice(const QJsonObject &json)
|
|
|
|
{
|
|
|
|
Device device;
|
|
|
|
device.fromJson(json);
|
|
|
|
return device;
|
|
|
|
}
|