PNP_PIbd-31_Rodionov_I_A_Co.../apiclient.cpp
2025-01-14 21:33:47 +04:00

110 lines
4.0 KiB
C++

#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&currentEntity=%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<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)
{
qDebug() << "Ошибка во время запроса: " << error;
}
Device ApiClient::deserializeDevice(const QJsonObject &json)
{
Device device;
device.fromJson(json);
return device;
}