From 8c79d35a7d91db707003b58816ed7a9d4290ec70 Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 3 Aug 2025 10:35:31 +0500 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=81=D0=B5=20=D0=B5=D1=89=D0=B5=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=BF=D1=80=D0=B0=D1=88=D0=B8=D0=B2=D0=B0=D0=B5?= =?UTF-8?q?=D0=BC=20=D0=B2=D1=81=D0=B5=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D0=B8=20=D0=B8=D0=B7=20=D0=91=D0=94...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/database/author_s.h | 23 +++++++------- src/database/book_s.h | 20 ++++++++++-- src/database/database.qbs | 8 +++-- src/restapi/restapiserver.cpp | 57 ++++++++++++++++++++++++++++++++--- 4 files changed, 88 insertions(+), 20 deletions(-) diff --git a/src/database/author_s.h b/src/database/author_s.h index 4c3fa5b..5c9b2da 100644 --- a/src/database/author_s.h +++ b/src/database/author_s.h @@ -18,33 +18,34 @@ class DATABASE_EXPORT Author_S public: Author_S() = default; - Author_S(const QString& first, - const QString& last, + Author_S(const QString& firstN, + const QString& lastN, const unsigned short age) : - m_first(first), - m_last(last), + m_firstName(firstN), + m_lastName(lastN), m_age(age) { } unsigned long long id() const { return m_id; } - QString first() const { return m_first; } - QString last() const { return m_last; } + QString firstName() const { return m_firstName; } + QString lastName() const { return m_lastName; } unsigned short age() const { return m_age; } - QString full_name() const { return m_first + " " + m_last; } + QString full_name() const { return m_firstName + " " + m_lastName; } void age(unsigned short age) { m_age = age; } private: friend class odb::access; +private: #pragma db id auto - unsigned long long m_id; + quint64 m_id; - QString m_first; - QString m_last; - unsigned short m_age; + QString m_firstName; + QString m_lastName; + qint8 m_age; }; #pragma db view object(Author_S) diff --git a/src/database/book_s.h b/src/database/book_s.h index ec9d066..adcf45c 100644 --- a/src/database/book_s.h +++ b/src/database/book_s.h @@ -38,11 +38,27 @@ private: private: #pragma db id auto - unsigned long long m_id; + quint64 m_id; SH m_author; QString m_name; - int m_year; + qint8 m_year; }; +// #pragma db view object(Book_S) object(Author_S = author:Book_S::m_author) +// struct BookByAuthorView +// { +// #pragma db column(Book_S::m_id) +// quint64 book_id; + +// #pragma db column(Book_S::m_name) +// QString book_name; + +// #pragma db column(Book_S::m_year) +// qint8 year; + +// #pragma db column(Author_S::m_first + " " + Author_S::m_last) +// QString author_full_name; +// }; + #endif // BOOK_S_H diff --git a/src/database/database.qbs b/src/database/database.qbs index f202fa9..349b198 100644 --- a/src/database/database.qbs +++ b/src/database/database.qbs @@ -15,17 +15,19 @@ PSLibrary { Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] } Depends { name: "cpp" } + Depends { name: "odb.gen" } - Depends { name: "redkit_gen" } Depends { name: "rdbase" } + Depends { name: "redkit_gen" } + redkit_gen.includeModules: ["JsonSerializer"] + odb.gen.databases: "sqlite" cpp.cxxLanguageVersion: "c++17" Group { name: "cpp" files: [ - "books.*", "databasemanager.*", "*.cpp", "*.h", @@ -41,7 +43,7 @@ PSLibrary { "author_s.h", "book_s.h", ] - fileTags: ["hpp", "odbxx", "odb"] + fileTags: ["hpp", "odbxx"] } cpp.dynamicLibraries: [ diff --git a/src/restapi/restapiserver.cpp b/src/restapi/restapiserver.cpp index 152a547..0094ab4 100644 --- a/src/restapi/restapiserver.cpp +++ b/src/restapi/restapiserver.cpp @@ -14,6 +14,8 @@ #include #include +#include + RestApiServer::RestApiServer(odb::database& db, QObject* parent) : QTcpServer(parent), m_db(db) {} @@ -61,9 +63,56 @@ QByteArray RestApiServer::processRequest(const QString& request) { QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " "); - qWarning() << "THIS"; + quint64 ageReq = author.toInt(); + + qWarning() << "THIS" << author << ageReq; + + // if (dbConn.query_one(odb::query::object == m_objectId, sett)) + // odb::result res(m_db.query(odb::query::author-> == author)); + + // { + + QStringList nameParts = author.split(' '); + QString firstName = nameParts.size() > 0 ? nameParts[0] : ""; + QString lastName = nameParts.size() > 1 ? nameParts[1] : ""; + + qWarning() + << "author:" << author << "\n" + << "firstName:" << firstName << "\n" + << "lastName:" << lastName << "\n"; + + // Result result(m_db.query( + // (Query::author->m_first + " " + Query::author->m_last == authorName) || + // (Query::author->m_first.like("%" + firstName + "%") && + // Query::author->m_last.like("%" + lastName + "%")) + // )); + odb::transaction t(m_db.begin()); - odb::result res(m_db.query()); + + auto books = m_db.query(); + + // using Query = odb::query; + // auto authors = m_db.query(Query::id == 2); + + auto authors = m_db.query(); + + for (auto book : books) + qWarning() << book.name() << book.author()->full_name(); + + for (auto author : authors) + qWarning() << author.full_name() << author.age(); + + // std::vector> books(result.begin(), result.end()); + + t.commit(); + // return books; + // } + + // for (auto book : books) + // qWarning() << book.author()->full_name(); + + // qWarning() << books; + // QList results = books.getBooksByAuthor(author); QByteArray jsonResponse = "{ \"books\": ["; @@ -121,8 +170,8 @@ QByteArray RestApiServer::processRequest(const QString& request) QJsonObject j; j["id"] = QString::number(author.id()); - j["first"] = author.first(); - j["last"] = author.last(); + j["first"] = author.firstName(); + j["last"] = author.lastName(); j["age"] = author.age(); jArray.push_back(j); }