#ifndef APICLIENT_H #define APICLIENT_H #include #include #include #include "models/types.h" // IWYU pragma: keep class ApiClient : public QObject { Q_OBJECT 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 &departments); void deviceModelsReceived(const QMap &deviceModels); void deviceTypesReceived(const QMap &deviceType); void locationsReceived(const QMap &locations); void manufacturersReceived(const QMap &manufacturers); void devicesReceived(const QList &devices); void deviceUpdated(bool success); private: template QMap deserializeEntities(const QByteArray &data, const QString &itemsKey); Device deserializeDevice(const QJsonObject &json); private slots: void parseData(QNetworkReply *reply); void handleDeviceUpdated(QNetworkReply *reply); void handleError(QNetworkReply::NetworkError error); private: const QString baseUrl = "http://localhost:8080"; QNetworkAccessManager *_networkManager; }; template QMap ApiClient::deserializeEntities(const QByteArray &data, const QString &itemsKey) { QMap 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