PNP_PIbd-31_Rodionov_I_A_Co.../apiserver.cpp

229 lines
8.2 KiB
C++
Raw Normal View History

2024-12-24 18:02:23 +04:00
#include "apiserver.h"
ApiServer::ApiServer() {
server.route("/api/devices", QHttpServerRequest::Method::Get,
[this](const QHttpServerRequest &request) {
return handleGetFilteredDevices(request);
});
server.route("/api/departments", QHttpServerRequest::Method::Get,
[this](const QHttpServerRequest &request) {
return handleGetAllDepartments(request);
});
server.route("/api/devicemodels", QHttpServerRequest::Method::Get,
[this](const QHttpServerRequest &request) {
return handleGetAllDeviceModels(request);
});
server.route("/api/devicetypes", QHttpServerRequest::Method::Get,
[this](const QHttpServerRequest &request) {
return handleGetAllDeviceTypes(request);
});
server.route("/api/locations", QHttpServerRequest::Method::Get,
[this](const QHttpServerRequest &request) {
return handleGetAllLocations(request);
});
server.route("/api/manufacturers", QHttpServerRequest::Method::Get,
[this](const QHttpServerRequest &request) {
return handleGetAllManufacturers(request);
});
server.route("/api/devices/<arg>", QHttpServerRequest::Method::Put,
[this](int id, const QHttpServerRequest &request) {
return handleUpdateDevice(id, request);
});
}
void ApiServer::start(ServiceLoadDB *serviceDB)
{
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);
}
QHttpServerResponse ApiServer::handleGetFilteredDevices(const QHttpServerRequest &request)
{
QUrlQuery query = request.query();
bool isWorking = query.queryItemValue("isWorking") == "true";
bool priceFromOk;
double priceFrom = query.queryItemValue("priceFrom").toDouble(&priceFromOk);
if (!priceFromOk) {
return QHttpServerResponse("Invalid priceFrom value", QHttpServerResponse::StatusCode::BadRequest);
}
bool priceToOk;
double priceTo = query.queryItemValue("priceTo").toDouble(&priceToOk);
if (!priceToOk) {
return QHttpServerResponse("Invalid priceTo value", QHttpServerResponse::StatusCode::BadRequest);
}
bool applyFilters = query.queryItemValue("applyFilters") == "true";
bool disregardState = query.queryItemValue("disregardState") == "true";
bool entityIdOk;
int entityId = query.queryItemValue("entityId").toInt(&entityIdOk);
if (!entityIdOk) {
return QHttpServerResponse("Invalid entityId value", QHttpServerResponse::StatusCode::BadRequest);
}
bool currentEntityOk;
int currentEntity = query.queryItemValue("currentEntity").toInt(&currentEntityOk);
if (!currentEntityOk) {
return QHttpServerResponse("Invalid currentEntity value", QHttpServerResponse::StatusCode::BadRequest);
}
QString searchText = query.queryItemValue("searchText");
QStringList validSortOrders = {"Сначала новые", "Сначала старые", "Сначала дешевые", "Сначала дорогие", "Сначала с лайком"};
QString sortOrder = query.queryItemValue("sortOrder");
if (!validSortOrders.contains(sortOrder)) {
return QHttpServerResponse("Invalid sortOrder value", QHttpServerResponse::StatusCode::BadRequest);
}
FilterParams filterParams;
filterParams.setIsWorking(isWorking);
filterParams.setPriceFrom(priceFrom);
filterParams.setPriceTo(priceTo);
filterParams.setApllyFilters(applyFilters);
filterParams.setDisregardState(disregardState);
QList<Device> filteredDevices = deviceLogic->getAllByParameters(
entityId,
currentEntity,
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)
{
QMap<int, Department> departments = departmentLogic->getAll();
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)
{
QMap<int, DeviceModel> deviceModels = deviceModelLogic->getAll();
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)
{
QMap<int, DeviceType> deviceTypes = deviceTypeLogic->getAll();
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)
{
QMap<int, Location> locations = locationLogic->getAll();
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)
{
QMap<int, Manufacturer> manufacturers = manufacturerLogic->getAll();
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);
if (!deviceLogic->updateDevice(device)) {
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;
}