#include "apiclient.h" ApiClient::ApiClient(QObject *parent) : QObject{parent}, _networkManager(new QNetworkAccessManager(this)) {} 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); QNetworkReply *reply = _networkManager->get(request); 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); QNetworkReply *reply = _networkManager->get(request); 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(); QNetworkReply *reply = _networkManager->put(request, data); 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 devices; for (const QJsonValue &value : devicesArray) { devices.append(deserializeDevice(value.toObject())); } emit devicesReceived(devices); } if (urlPath.contains("/api/locations")) { emit locationsReceived(deserializeEntities(data, "locations")); } if (urlPath.contains("/api/departments")) { emit departmentsReceived(deserializeEntities(data, "departments")); } if (urlPath.contains("/api/manufacturers")) { emit manufacturersReceived(deserializeEntities(data, "manufacturers")); } if (urlPath.contains("/api/devicetypes")) { emit deviceTypesReceived(deserializeEntities(data, "deviceTypes")); } if (urlPath.contains("/api/devicemodels")) { emit deviceModelsReceived(deserializeEntities(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) { qDebug() << "Ошибка во время запроса: " << error; } Device ApiClient::deserializeDevice(const QJsonObject &json) { Device device; device.fromJson(json); return device; }