2024-12-24 18:02:23 +04:00
|
|
|
#include "apiserver.h"
|
|
|
|
|
|
|
|
ApiServer::ApiServer() {
|
2025-01-14 21:28:06 +04:00
|
|
|
_server.route("/api/devices", QHttpServerRequest::Method::Get,
|
2024-12-24 18:02:23 +04:00
|
|
|
[this](const QHttpServerRequest &request) {
|
|
|
|
return handleGetFilteredDevices(request);
|
|
|
|
});
|
|
|
|
|
2025-01-14 21:28:06 +04:00
|
|
|
_server.route("/api/departments", QHttpServerRequest::Method::Get,
|
2024-12-24 18:02:23 +04:00
|
|
|
[this](const QHttpServerRequest &request) {
|
|
|
|
return handleGetAllDepartments(request);
|
|
|
|
});
|
|
|
|
|
2025-01-14 21:28:06 +04:00
|
|
|
_server.route("/api/devicemodels", QHttpServerRequest::Method::Get,
|
2024-12-24 18:02:23 +04:00
|
|
|
[this](const QHttpServerRequest &request) {
|
|
|
|
return handleGetAllDeviceModels(request);
|
|
|
|
});
|
|
|
|
|
2025-01-14 21:28:06 +04:00
|
|
|
_server.route("/api/devicetypes", QHttpServerRequest::Method::Get,
|
2024-12-24 18:02:23 +04:00
|
|
|
[this](const QHttpServerRequest &request) {
|
|
|
|
return handleGetAllDeviceTypes(request);
|
|
|
|
});
|
|
|
|
|
2025-01-14 21:28:06 +04:00
|
|
|
_server.route("/api/locations", QHttpServerRequest::Method::Get,
|
2024-12-24 18:02:23 +04:00
|
|
|
[this](const QHttpServerRequest &request) {
|
|
|
|
return handleGetAllLocations(request);
|
|
|
|
});
|
|
|
|
|
2025-01-14 21:28:06 +04:00
|
|
|
_server.route("/api/manufacturers", QHttpServerRequest::Method::Get,
|
2024-12-24 18:02:23 +04:00
|
|
|
[this](const QHttpServerRequest &request) {
|
|
|
|
return handleGetAllManufacturers(request);
|
|
|
|
});
|
|
|
|
|
2025-01-14 21:28:06 +04:00
|
|
|
_server.route("/api/devices/<arg>", QHttpServerRequest::Method::Put,
|
2024-12-24 18:02:23 +04:00
|
|
|
[this](int id, const QHttpServerRequest &request) {
|
|
|
|
return handleUpdateDevice(id, request);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiServer::start(ServiceLoadDB *serviceDB)
|
|
|
|
{
|
2025-01-14 21:28:06 +04:00
|
|
|
_server.listen(QHostAddress::LocalHost, 8080);
|
|
|
|
_deviceLogic = new DeviceLogic(serviceDB);
|
|
|
|
_departmentLogic = new DepartmentLogic(serviceDB);
|
|
|
|
_deviceModelLogic = new DeviceModelLogic(serviceDB);
|
|
|
|
_locationLogic = new LocationLogic(serviceDB);
|
|
|
|
_manufacturerLogic = new ManufacturerLogic(serviceDB);
|
|
|
|
_deviceTypeLogic = new DeviceTypeLogic(serviceDB);
|
2024-12-24 18:02:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QHttpServerResponse ApiServer::handleGetFilteredDevices(const QHttpServerRequest &request)
|
|
|
|
{
|
|
|
|
QUrlQuery query = request.query();
|
|
|
|
|
2025-01-14 21:28:06 +04:00
|
|
|
bool isWorking = getQueryParamWithDefault(query, "isWorking", true);
|
|
|
|
double priceFrom = getQueryParamWithDefault(query, "priceFrom", 0.00);
|
|
|
|
double priceTo = getQueryParamWithDefault(query, "priceTo", std::numeric_limits<double>::max());
|
|
|
|
bool applyFilters = getQueryParamWithDefault(query, "applyFilters", false);
|
|
|
|
bool disregardState = getQueryParamWithDefault(query, "disregardState", true);
|
|
|
|
int entityId = getQueryParamWithDefault(query, "entityId", -1);
|
|
|
|
int currentEntity = getQueryParamWithDefault(query, "currentEntity", 0);
|
|
|
|
QString searchText = getQueryParamWithDefault(query, "searchText", QString());
|
|
|
|
QString sortOrder = getQueryParamWithDefault(query, "sortOrder", QString("Сначала новые"));
|
|
|
|
|
|
|
|
if (!validEntityTypeNumbers.contains(currentEntity)) {
|
|
|
|
currentEntity = 0;
|
2024-12-24 18:02:23 +04:00
|
|
|
}
|
|
|
|
if (!validSortOrders.contains(sortOrder)) {
|
2025-01-14 21:28:06 +04:00
|
|
|
sortOrder = "Сначала новые";
|
2024-12-24 18:02:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
FilterParams filterParams;
|
|
|
|
filterParams.setIsWorking(isWorking);
|
|
|
|
filterParams.setPriceFrom(priceFrom);
|
|
|
|
filterParams.setPriceTo(priceTo);
|
|
|
|
filterParams.setApllyFilters(applyFilters);
|
|
|
|
filterParams.setDisregardState(disregardState);
|
|
|
|
|
2025-01-14 21:28:06 +04:00
|
|
|
QList<Device> filteredDevices = _deviceLogic->getAllByParameters(
|
2024-12-24 18:02:23 +04:00
|
|
|
entityId,
|
2025-01-14 21:28:06 +04:00
|
|
|
static_cast<EntityType>(currentEntity),
|
2024-12-24 18:02:23 +04:00
|
|
|
searchText,
|
|
|
|
filterParams,
|
|
|
|
sortOrder
|
|
|
|
);
|
|
|
|
|
|
|
|
QJsonArray deviceArray;
|
|
|
|
for (const Device &device : filteredDevices) {
|
|
|
|
deviceArray.append(device.toJson());
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject responseObject;
|
|
|
|
responseObject["devices"] = deviceArray;
|
|
|
|
|
|
|
|
return QHttpServerResponse(QJsonDocument(responseObject).toJson(), QHttpServerResponse::StatusCode::Ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
QHttpServerResponse ApiServer::handleGetAllDepartments(const QHttpServerRequest &request)
|
|
|
|
{
|
2025-01-14 21:28:06 +04:00
|
|
|
QMap<int, Department> departments = _departmentLogic->getAll();
|
2024-12-24 18:02:23 +04:00
|
|
|
|
|
|
|
if (departments.isEmpty()) {
|
|
|
|
return QHttpServerResponse("Failed to retrieve departments", QHttpServerResponse::StatusCode::InternalServerError);
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonArray departmentArray = createJsonArray(departments);
|
|
|
|
|
|
|
|
QJsonObject responseObject;
|
|
|
|
responseObject["departments"] = departmentArray;
|
|
|
|
|
|
|
|
return QHttpServerResponse(QJsonDocument(responseObject).toJson(), QHttpServerResponse::StatusCode::Ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
QHttpServerResponse ApiServer::handleGetAllDeviceModels(const QHttpServerRequest &request)
|
|
|
|
{
|
2025-01-14 21:28:06 +04:00
|
|
|
QMap<int, DeviceModel> deviceModels = _deviceModelLogic->getAll();
|
2024-12-24 18:02:23 +04:00
|
|
|
|
|
|
|
if (deviceModels.isEmpty()) {
|
|
|
|
return QHttpServerResponse("Failed to retrieve device models", QHttpServerResponse::StatusCode::InternalServerError);
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonArray deviceModelArray = createJsonArray(deviceModels);
|
|
|
|
|
|
|
|
QJsonObject responseObject;
|
|
|
|
responseObject["deviceModels"] = deviceModelArray;
|
|
|
|
|
|
|
|
return QHttpServerResponse(QJsonDocument(responseObject).toJson(), QHttpServerResponse::StatusCode::Ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
QHttpServerResponse ApiServer::handleGetAllDeviceTypes(const QHttpServerRequest &request)
|
|
|
|
{
|
2025-01-14 21:28:06 +04:00
|
|
|
QMap<int, DeviceType> deviceTypes = _deviceTypeLogic->getAll();
|
2024-12-24 18:02:23 +04:00
|
|
|
|
|
|
|
if (deviceTypes.isEmpty()) {
|
|
|
|
return QHttpServerResponse("Failed to retrieve device types", QHttpServerResponse::StatusCode::InternalServerError);
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonArray deviceTypeArray = createJsonArray(deviceTypes);
|
|
|
|
|
|
|
|
QJsonObject responseObject;
|
|
|
|
responseObject["deviceTypes"] = deviceTypeArray;
|
|
|
|
|
|
|
|
return QHttpServerResponse(QJsonDocument(responseObject).toJson(), QHttpServerResponse::StatusCode::Ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
QHttpServerResponse ApiServer::handleGetAllLocations(const QHttpServerRequest &request)
|
|
|
|
{
|
2025-01-14 21:28:06 +04:00
|
|
|
QMap<int, Location> locations = _locationLogic->getAll();
|
2024-12-24 18:02:23 +04:00
|
|
|
|
|
|
|
if (locations.isEmpty()) {
|
|
|
|
return QHttpServerResponse("Failed to retrieve locations", QHttpServerResponse::StatusCode::InternalServerError);
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonArray locationArray = createJsonArray(locations);
|
|
|
|
|
|
|
|
QJsonObject responseObject;
|
|
|
|
responseObject["locations"] = locationArray;
|
|
|
|
|
|
|
|
return QHttpServerResponse(QJsonDocument(responseObject).toJson(), QHttpServerResponse::StatusCode::Ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
QHttpServerResponse ApiServer::handleGetAllManufacturers(const QHttpServerRequest &request)
|
|
|
|
{
|
2025-01-14 21:28:06 +04:00
|
|
|
QMap<int, Manufacturer> manufacturers = _manufacturerLogic->getAll();
|
2024-12-24 18:02:23 +04:00
|
|
|
|
|
|
|
if (manufacturers.isEmpty()) {
|
|
|
|
return QHttpServerResponse("Failed to retrieve manufacturers", QHttpServerResponse::StatusCode::InternalServerError);
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonArray manufacturerArray = createJsonArray(manufacturers);
|
|
|
|
|
|
|
|
QJsonObject responseObject;
|
|
|
|
responseObject["manufacturers"] = manufacturerArray;
|
|
|
|
|
|
|
|
return QHttpServerResponse(QJsonDocument(responseObject).toJson(), QHttpServerResponse::StatusCode::Ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
QHttpServerResponse ApiServer::handleUpdateDevice(int id, const QHttpServerRequest &request)
|
|
|
|
{
|
|
|
|
QByteArray body = request.body();
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(body);
|
|
|
|
|
|
|
|
if (!doc.isObject()) {
|
|
|
|
return QHttpServerResponse("Invalid JSON", QHttpServerResponse::StatusCode::BadRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject jsonObject = doc.object();
|
|
|
|
|
|
|
|
Device device;
|
|
|
|
device.fromJson(jsonObject);
|
|
|
|
device.setId(id);
|
|
|
|
|
2025-01-14 21:28:06 +04:00
|
|
|
if (!_deviceLogic->updateDevice(device)) {
|
2024-12-24 18:02:23 +04:00
|
|
|
return QHttpServerResponse("Failed to update device", QHttpServerResponse::StatusCode::InternalServerError);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QHttpServerResponse("Device updated successfully", QHttpServerResponse::StatusCode::Ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
QJsonArray ApiServer::createJsonArray(const QMap<int, T> &items)
|
|
|
|
{
|
|
|
|
QJsonArray itemArray;
|
|
|
|
for (const T &item : items) {
|
|
|
|
itemArray.append(item.toJson());
|
|
|
|
}
|
|
|
|
return itemArray;
|
|
|
|
}
|
2025-01-14 21:28:06 +04:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T ApiServer::getQueryParamWithDefault(const QUrlQuery &query, const QString ¶mName, const T &defaultValue)
|
|
|
|
{
|
|
|
|
QString value = query.queryItemValue(paramName);
|
|
|
|
if (value.isEmpty()) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if constexpr (std::is_same<T, bool>::value) {
|
|
|
|
return value == "true";
|
|
|
|
}
|
|
|
|
if constexpr (std::is_same<T, int>::value) {
|
|
|
|
bool ok;
|
|
|
|
int result = value.toInt(&ok);
|
|
|
|
return ok ? result : defaultValue;
|
|
|
|
}
|
|
|
|
if constexpr (std::is_same<T, double>::value) {
|
|
|
|
|
|
|
|
bool ok;
|
|
|
|
double result = value.toDouble(&ok);
|
|
|
|
return ok ? result : defaultValue;
|
|
|
|
}
|
|
|
|
if constexpr (std::is_same<T, QString>::value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
}
|