diff --git a/project.qbs b/project.qbs index 90f6a06..c36f82f 100644 --- a/project.qbs +++ b/project.qbs @@ -18,6 +18,8 @@ Project { "src/model/model.qbs", "src/restapi/restapi.qbs", "src/utils/utils.qbs", + "src/repository/repository.qbs", + "external_libs/quazip.qbs", ] } diff --git a/src/model/books/book_s.h b/src/model/books/book_s.h index ee83309..9622e5e 100644 --- a/src/model/books/book_s.h +++ b/src/model/books/book_s.h @@ -58,7 +58,7 @@ public: private: friend class odb::access; -// public: // for Redkit-gen + // public: // for Redkit-gen private: #pragma db id auto quint64 m_id; diff --git a/src/repository/bookrepository.cpp b/src/repository/bookrepository.cpp new file mode 100644 index 0000000..1e8778e --- /dev/null +++ b/src/repository/bookrepository.cpp @@ -0,0 +1,89 @@ +#include "bookrepository.h" + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace repository +{ + +BookRepository::BookRepository(odb::core::database& db) : + m_db(db) +{} + +QVector BookRepository::findAll() +{ + odb::transaction t(m_db.begin()); + odb::result res(m_db.query()); + + QVector books; + + for (auto it = res.begin(); it != res.end(); ++it) + { + const auto& book = *it; + books.push_back(book); + } + + t.commit(); + + return books; +} + +Book_S BookRepository::findById(int id) +{ + odb::transaction t(m_db.begin()); + odb::result res(m_db.query(odb::query::id == id)); + + Book_S book; + res.value(book); + + t.commit(); + + return book; +} + +// void BookRepository::save(const Book_S& book) +// { +// } + +void BookRepository::remove(int id) +{ + odb::transaction t(m_db.begin()); + odb::result res(m_db.query(odb::query::id == id)); + res.one().reset(); + t.commit(); +} + +QVector BookRepository::findByAuthor(const QString& author) +{ + odb::transaction t(m_db.begin()); + auto author_s = m_db.query(odb::query::fullName == author).one(); + + QVector books; + + using ABQuery = odb::query; + using ABResult = odb::result; + ABResult abResult(m_db.query(ABQuery::author == author_s->id())); + + for (const AuthorBook_S& ab : abResult) + { + // Загружаем каждую книгу по ID + SH book(m_db.load(ab.book()->id())); + books.push_back(*book.data()); + } + + t.commit(); + + return books; +} + +} // namespace repository diff --git a/src/repository/bookrepository.h b/src/repository/bookrepository.h new file mode 100644 index 0000000..8b620c7 --- /dev/null +++ b/src/repository/bookrepository.h @@ -0,0 +1,40 @@ +#ifndef BOOKREPOSITORY_H +#define BOOKREPOSITORY_H + +#include "repository_global.h" + +#include + +#include +#include + +namespace repository +{ + +class REPOSITORY_EXPORT BookRepository +{ +public: + BookRepository(odb::core::database& db); + + // Получить все книги + QVector findAll(); + + // Найти книгу по ID + Book_S findById(int id); + + // Сохранить книгу (создать или обновить) + // void save(const Book_S& book); + + // Удалить книгу + void remove(int id); + + // Найти книги по автору + QVector findByAuthor(const QString& author); + +private: + odb::core::database& m_db; +}; + +} // namespace repository + +#endif // BOOKREPOSITORY_H diff --git a/src/repository/repository.qbs b/src/repository/repository.qbs new file mode 100644 index 0000000..f7649c8 --- /dev/null +++ b/src/repository/repository.qbs @@ -0,0 +1,33 @@ +/*! + \qmltype cpp-opds + \inherits Project + \brief Описание +*/ +PSLibrary { + name: "repository" + cpp.defines: [ + "REPOSITORY_LIBRARY" + ] + + Depends { name: "Qt"; submodules: [ "core" ] } + Depends { name: "model" } + + Depends { name: "rdbase" } + Depends { name: "odb.gen" } + Depends { name: "database" } + + Group { + name: "cpp" + files: [ + "**/*.h", + "**/*.cpp", + ] + } + + cpp.dynamicLibraries: [ + "odb-sqlite", + "odb-qt", + "odb", + "sqlite3" + ] +} diff --git a/src/repository/repository_global.h b/src/repository/repository_global.h new file mode 100644 index 0000000..b2eeb4b --- /dev/null +++ b/src/repository/repository_global.h @@ -0,0 +1,12 @@ +#ifndef REPOSITORY_GLOBAL_H +#define REPOSITORY_GLOBAL_H + +#include + +#if defined(REPOSITORY_LIBRARY) +#define REPOSITORY_EXPORT Q_DECL_EXPORT +#else +#define REPOSITORY_EXPORT Q_DECL_IMPORT +#endif + +#endif // REPOSITORY_GLOBAL_H diff --git a/src/restapi/restapi.qbs b/src/restapi/restapi.qbs index 046ae13..8bb41ac 100644 --- a/src/restapi/restapi.qbs +++ b/src/restapi/restapi.qbs @@ -19,6 +19,7 @@ WPSLibrary { Depends { name: "redkit_gen" } Depends { name: "rdbase" } Depends { name: "model" } + Depends { name: "repository" } cpp.cxxLanguageVersion: "c++20" diff --git a/src/restapi/restapiserver.cpp b/src/restapi/restapiserver.cpp index 88b4662..24ebb60 100644 --- a/src/restapi/restapiserver.cpp +++ b/src/restapi/restapiserver.cpp @@ -10,6 +10,8 @@ #include // Должен быть здесь #include +#include + #include #include #include @@ -63,70 +65,64 @@ QByteArray RestApiServer::processRequest(const QString& request) if (request.startsWith("GET /books/author/")) { + repository::BookRepository b(m_db); + QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " "); - // quint64 ageReq = author.toInt(); - - QStringList nameParts = author.split(' '); - QString firstName = nameParts.size() > 0 ? nameParts[0] : ""; - QString lastName = nameParts.size() > 1 ? nameParts[1] : ""; - - odb::transaction t(m_db.begin()); - - // auto books = m_db.query(odb::query::author->id == ageReq); + qWarning() << author; + auto books = b.findByAuthor(author); QJsonArray jArray; - // for (const auto& book : books) - // { - // QJsonObject j; - // j["id"] = QString::number(book.id()); + for (const auto& book : books) + { + QJsonObject j; + j["id"] = QString::number(book.id()); - // QJsonObject author; - // author["id"] = QString::number(book.author()->id()); - // author["fullName"] = book.author()->fullName(); - // author["langCode"] = book.author()->langCode(); - // j["author"] = author; - // // j["series"] = book.series()->serName(); - // // j["genre"] = book.genre()->name(); - // j["title"] = book.title(); - // j["year"] = book.year(); - // j["lastModified"] = book.lastModified().toString(); - // j["lang"] = book.lang(); - // jArray.push_back(j); - // } + j["title"] = book.title(); + + 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 /books/id")) + { + repository::BookRepository b(m_db); + + QString book_id = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " "); + qWarning() << book_id; + + auto book = b.findById(book_id.toInt()); + + QJsonObject j; + j["id"] = QString::number(book.id()); + QJsonObject author; + // // // author["id"] = QString::number(book.author()->id()); + // // // author["fullName"] = book.author()->fullName(); + // // // author["langCode"] = book.author()->langCode(); + j["author"] = author; + j["title"] = book.title(); + + auto jDoc = QJsonDocument(j); return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented); } if (request.startsWith("GET /books")) { - odb::transaction t(m_db.begin()); - odb::result res(m_db.query()); + repository::BookRepository b(m_db); QJsonArray jArray; - for (auto it = res.begin(); it != res.end(); ++it) + for (const auto& book : b.findAll()) { - const auto& book = *it; - QJsonObject j; j["id"] = QString::number(book.id()); - // QJsonObject author; - // author["id"] = QString::number(book.author()->id()); - // author["fullName"] = book.author()->fullName(); - // author["langCode"] = book.author()->langCode(); - // j["author"] = author; - // j["series"] = book.series()->serName(); - // j["genre"] = book.genre()->name(); j["title"] = book.title(); - // j["year"] = book.year(); - // j["lastModified"] = book.lastModified().toString(); - // j["lang"] = book.lang(); + 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); } diff --git a/src/utils/zipwrapper.h b/src/utils/zipwrapper.h index 9052812..fdfcf09 100644 --- a/src/utils/zipwrapper.h +++ b/src/utils/zipwrapper.h @@ -7,8 +7,6 @@ #include #include - - #include #include // TODO для совместимости. Временно