2025-03-09 10:26:52 +05:00
|
|
|
#include "restapiserver.h"
|
|
|
|
|
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
#include <QJsonValue>
|
|
|
|
|
|
2025-08-03 00:14:45 +05:00
|
|
|
#include <database/author_s-odb.hxx> // Должен быть здесь
|
|
|
|
|
#include <database/author_s.h>
|
|
|
|
|
#include <database/book_s-odb.hxx> // Должен быть здесь
|
2025-03-16 10:46:06 +05:00
|
|
|
#include <database/book_s.h>
|
|
|
|
|
|
2025-03-09 10:26:52 +05:00
|
|
|
#include <odb/core.hxx>
|
|
|
|
|
#include <odb/database.hxx>
|
|
|
|
|
#include <odb/query.hxx>
|
|
|
|
|
|
|
|
|
|
RestApiServer::RestApiServer(odb::database& db, QObject* parent) :
|
2025-03-16 10:46:06 +05:00
|
|
|
QTcpServer(parent), m_db(db) {}
|
2025-03-09 10:26:52 +05:00
|
|
|
|
|
|
|
|
void RestApiServer::start(quint16 port)
|
|
|
|
|
{
|
|
|
|
|
if (!this->listen(QHostAddress::Any, port))
|
|
|
|
|
{
|
|
|
|
|
qWarning() << "Ошибка запуска сервера!";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "REST API сервер запущен на порту:" << port;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RestApiServer::incomingConnection(qintptr socketDescriptor)
|
|
|
|
|
{
|
|
|
|
|
QTcpSocket* socket = new QTcpSocket(this);
|
|
|
|
|
socket->setSocketDescriptor(socketDescriptor);
|
|
|
|
|
|
|
|
|
|
connect(socket, &QTcpSocket::readyRead, this, &RestApiServer::handleRequest);
|
|
|
|
|
connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RestApiServer::handleRequest()
|
|
|
|
|
{
|
|
|
|
|
QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
|
|
|
|
|
if (!socket)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QString request = QString::fromUtf8(socket->readAll());
|
|
|
|
|
QByteArray response = processRequest(request);
|
|
|
|
|
|
|
|
|
|
socket->write(response);
|
|
|
|
|
socket->flush();
|
|
|
|
|
socket->disconnectFromHost();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray RestApiServer::processRequest(const QString& request)
|
|
|
|
|
{
|
|
|
|
|
if (request.startsWith("GET /books/author/"))
|
|
|
|
|
{
|
|
|
|
|
QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " ");
|
|
|
|
|
|
2025-08-03 00:14:45 +05:00
|
|
|
qWarning() << "THIS";
|
2025-03-09 10:26:52 +05:00
|
|
|
// Books books;
|
|
|
|
|
// QList<BookRecord> results = books.getBooksByAuthor(author);
|
|
|
|
|
|
|
|
|
|
QByteArray jsonResponse = "{ \"books\": [";
|
|
|
|
|
// for (const BookRecord& book : results)
|
|
|
|
|
// {
|
|
|
|
|
// jsonResponse += QString("{ \"id\": %1, \"title\": \"%2\", \"author\": \"%3\", \"year\": %4 },")
|
|
|
|
|
// .arg(book.id)
|
|
|
|
|
// .arg(book.title)
|
|
|
|
|
// .arg(book.author)
|
|
|
|
|
// .arg(book.year)
|
|
|
|
|
// .toUtf8();
|
|
|
|
|
// }
|
|
|
|
|
// if (results.size() > 0)
|
|
|
|
|
// jsonResponse.chop(1); // Убираем последнюю запятую
|
2025-08-03 00:14:45 +05:00
|
|
|
jsonResponse += "] }";
|
2025-03-09 10:26:52 +05:00
|
|
|
|
|
|
|
|
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jsonResponse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.startsWith("GET /books"))
|
|
|
|
|
{
|
|
|
|
|
odb::transaction t(m_db.begin());
|
|
|
|
|
odb::result<Book_S> res(m_db.query<Book_S>());
|
|
|
|
|
|
|
|
|
|
QJsonArray jArray;
|
|
|
|
|
|
|
|
|
|
for (auto it = res.begin(); it != res.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
const auto& book = *it;
|
|
|
|
|
|
|
|
|
|
QJsonObject j;
|
|
|
|
|
j["id"] = QString::number(book.id());
|
2025-08-03 00:14:45 +05:00
|
|
|
j["title"] = book.name();
|
|
|
|
|
j["author"] = book.author()->full_name();
|
2025-03-09 10:26:52 +05:00
|
|
|
j["year"] = book.year();
|
|
|
|
|
jArray.push_back(j);
|
|
|
|
|
}
|
|
|
|
|
auto jDoc = QJsonDocument(jArray);
|
|
|
|
|
|
|
|
|
|
t.commit();
|
|
|
|
|
|
|
|
|
|
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.startsWith("GET /author"))
|
|
|
|
|
{
|
|
|
|
|
odb::transaction t(m_db.begin());
|
|
|
|
|
odb::result<Author_S> res(m_db.query<Author_S>());
|
|
|
|
|
|
|
|
|
|
QJsonArray jArray;
|
|
|
|
|
|
|
|
|
|
for (auto it = res.begin(); it != res.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
const auto& author = *it;
|
|
|
|
|
|
|
|
|
|
QJsonObject j;
|
|
|
|
|
j["id"] = QString::number(author.id());
|
2025-08-03 00:14:45 +05:00
|
|
|
j["first"] = author.first();
|
|
|
|
|
j["last"] = author.last();
|
2025-03-09 10:26:52 +05:00
|
|
|
j["age"] = author.age();
|
|
|
|
|
jArray.push_back(j);
|
|
|
|
|
}
|
|
|
|
|
auto jDoc = QJsonDocument(jArray);
|
|
|
|
|
|
|
|
|
|
t.commit();
|
|
|
|
|
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\nNot Found";
|
|
|
|
|
}
|