Учимся пользоваться odb в нашем меленьком проекте
This commit was merged in pull request #3.
This commit is contained in:
129
src/restapiserver.cpp
Normal file
129
src/restapiserver.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
#include "restapiserver.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
|
||||
#include "book_s-odb.hxx"
|
||||
#include "book_s.h"
|
||||
|
||||
#include <odb/core.hxx>
|
||||
#include <odb/database.hxx>
|
||||
#include <odb/query.hxx>
|
||||
|
||||
RestApiServer::RestApiServer(odb::database& db, QObject* parent) :
|
||||
m_db(db), QTcpServer(parent) {}
|
||||
|
||||
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", " ");
|
||||
|
||||
// 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); // Убираем последнюю запятую
|
||||
// jsonResponse += "] }";
|
||||
|
||||
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());
|
||||
j["title"] = QString::fromStdString(book.name());
|
||||
j["author"] = QString::fromStdString(book.author()->full_name());
|
||||
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());
|
||||
j["first"] = QString::fromStdString(author.first());
|
||||
j["last"] = QString::fromStdString(author.last());
|
||||
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";
|
||||
}
|
||||
Reference in New Issue
Block a user