2024-12-24 18:13:22 +04:00
|
|
|
#ifndef APICLIENT_H
|
|
|
|
#define APICLIENT_H
|
|
|
|
|
|
|
|
#include <QtCore>
|
|
|
|
#include <QtNetwork>
|
|
|
|
#include <QObject>
|
|
|
|
|
|
|
|
#include "models/types.h" // IWYU pragma: keep
|
|
|
|
|
|
|
|
class ApiClient : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2025-01-14 21:33:47 +04:00
|
|
|
|
2024-12-24 18:13:22 +04:00
|
|
|
public:
|
|
|
|
explicit ApiClient(QObject *parent = nullptr);
|
|
|
|
|
|
|
|
void getEntities(const QString &url);
|
|
|
|
void getFilteredDevices(bool isWorking, double priceFrom, double priceTo, bool applyFilters,
|
|
|
|
bool disregardState, int entityId, int currentEntity, const QString &searchText,
|
|
|
|
const QString &sortOrder);
|
|
|
|
void updateDevice(int id, const Device &device);
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void departmentsReceived(const QMap<int, Department> &departments);
|
|
|
|
void deviceModelsReceived(const QMap<int, DeviceModel> &deviceModels);
|
|
|
|
void deviceTypesReceived(const QMap<int, DeviceType> &deviceType);
|
|
|
|
void locationsReceived(const QMap<int, Location> &locations);
|
|
|
|
void manufacturersReceived(const QMap<int, Manufacturer> &manufacturers);
|
|
|
|
void devicesReceived(const QList<Device> &devices);
|
|
|
|
void deviceUpdated(bool success);
|
|
|
|
|
2025-01-14 21:33:47 +04:00
|
|
|
private:
|
|
|
|
template <typename T>
|
|
|
|
QMap<int, T> deserializeEntities(const QByteArray &data, const QString &itemsKey);
|
|
|
|
Device deserializeDevice(const QJsonObject &json);
|
|
|
|
|
2024-12-24 18:13:22 +04:00
|
|
|
private slots:
|
|
|
|
void parseData(QNetworkReply *reply);
|
|
|
|
void handleDeviceUpdated(QNetworkReply *reply);
|
|
|
|
void handleError(QNetworkReply::NetworkError error);
|
|
|
|
|
|
|
|
private:
|
|
|
|
const QString baseUrl = "http://localhost:8080";
|
|
|
|
|
2025-01-14 21:33:47 +04:00
|
|
|
QNetworkAccessManager *_networkManager;
|
2024-12-24 18:13:22 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
QMap<int, T> ApiClient::deserializeEntities(const QByteArray &data, const QString &itemsKey)
|
|
|
|
{
|
|
|
|
QMap<int, T> entities;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(data);
|
|
|
|
QJsonArray itemsArray = doc.object().value(itemsKey).toArray();
|
|
|
|
|
|
|
|
for (const QJsonValue &value : itemsArray) {
|
|
|
|
T entity;
|
|
|
|
entity.fromJson(value.toObject());
|
|
|
|
entities.insert(entity.id(), entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
return entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // APICLIENT_H
|