WIP: Новые таблицы и изменение старых структур #6

Draft
alex wants to merge 12 commits from Новые-таблицы into master
9 changed files with 217 additions and 46 deletions
Showing only changes of commit b993116e09 - Show all commits

View File

@@ -18,6 +18,8 @@ Project {
"src/model/model.qbs", "src/model/model.qbs",
"src/restapi/restapi.qbs", "src/restapi/restapi.qbs",
"src/utils/utils.qbs", "src/utils/utils.qbs",
"src/repository/repository.qbs",
"external_libs/quazip.qbs", "external_libs/quazip.qbs",
] ]
} }

View File

@@ -58,7 +58,7 @@ public:
private: private:
friend class odb::access; friend class odb::access;
// public: // for Redkit-gen // public: // for Redkit-gen
private: private:
#pragma db id auto #pragma db id auto
quint64 m_id; quint64 m_id;

View File

@@ -0,0 +1,89 @@
#include "bookrepository.h"
#include <QDebug>
#include <model/books/author_book_s-odb.hxx>
#include <model/books/author_book_s.h>
#include <model/books/author_s-odb.hxx>
#include <model/books/author_s.h>
#include <model/books/book_s-odb.hxx>
#include <model/books/book_s.h>
#include <odb/core.hxx>
#include <odb/database.hxx>
#include <odb/query.hxx>
namespace repository
{
BookRepository::BookRepository(odb::core::database& db) :
m_db(db)
{}
QVector<Book_S> BookRepository::findAll()
{
odb::transaction t(m_db.begin());
odb::result<Book_S> res(m_db.query<Book_S>());
QVector<Book_S> 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<Book_S> res(m_db.query<Book_S>(odb::query<Book_S>::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<Book_S> res(m_db.query<Book_S>(odb::query<Book_S>::id == id));
res.one().reset();
t.commit();
}
QVector<Book_S> BookRepository::findByAuthor(const QString& author)
{
odb::transaction t(m_db.begin());
auto author_s = m_db.query<Author_S>(odb::query<Author_S>::fullName == author).one();
QVector<Book_S> books;
using ABQuery = odb::query<AuthorBook_S>;
using ABResult = odb::result<AuthorBook_S>;
ABResult abResult(m_db.query<AuthorBook_S>(ABQuery::author == author_s->id()));
for (const AuthorBook_S& ab : abResult)
{
// Загружаем каждую книгу по ID
SH<Book_S> book(m_db.load<Book_S>(ab.book()->id()));
books.push_back(*book.data());
}
t.commit();
return books;
}
} // namespace repository

View File

@@ -0,0 +1,40 @@
#ifndef BOOKREPOSITORY_H
#define BOOKREPOSITORY_H
#include "repository_global.h"
#include <model/books/book_s.h>
#include <QString>
#include <QVector>
namespace repository
{
class REPOSITORY_EXPORT BookRepository
{
public:
BookRepository(odb::core::database& db);
// Получить все книги
QVector<Book_S> findAll();
// Найти книгу по ID
Book_S findById(int id);
// Сохранить книгу (создать или обновить)
// void save(const Book_S& book);
// Удалить книгу
void remove(int id);
// Найти книги по автору
QVector<Book_S> findByAuthor(const QString& author);
private:
odb::core::database& m_db;
};
} // namespace repository
#endif // BOOKREPOSITORY_H

View File

@@ -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"
]
}

View File

@@ -0,0 +1,12 @@
#ifndef REPOSITORY_GLOBAL_H
#define REPOSITORY_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(REPOSITORY_LIBRARY)
#define REPOSITORY_EXPORT Q_DECL_EXPORT
#else
#define REPOSITORY_EXPORT Q_DECL_IMPORT
#endif
#endif // REPOSITORY_GLOBAL_H

View File

@@ -19,6 +19,7 @@ WPSLibrary {
Depends { name: "redkit_gen" } Depends { name: "redkit_gen" }
Depends { name: "rdbase" } Depends { name: "rdbase" }
Depends { name: "model" } Depends { name: "model" }
Depends { name: "repository" }
cpp.cxxLanguageVersion: "c++20" cpp.cxxLanguageVersion: "c++20"

View File

@@ -10,6 +10,8 @@
#include <model/books/book_s-odb.hxx> // Должен быть здесь #include <model/books/book_s-odb.hxx> // Должен быть здесь
#include <model/books/book_s.h> #include <model/books/book_s.h>
#include <repository/bookrepository.h>
#include <odb/core.hxx> #include <odb/core.hxx>
#include <odb/database.hxx> #include <odb/database.hxx>
#include <odb/query.hxx> #include <odb/query.hxx>
@@ -63,70 +65,64 @@ QByteArray RestApiServer::processRequest(const QString& request)
if (request.startsWith("GET /books/author/")) if (request.startsWith("GET /books/author/"))
{ {
repository::BookRepository b(m_db);
QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " "); QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " ");
// quint64 ageReq = author.toInt(); qWarning() << author;
auto books = b.findByAuthor(author);
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<Book_S>(odb::query<Book_S>::author->id == ageReq);
QJsonArray jArray; QJsonArray jArray;
// for (const auto& book : books) for (const auto& book : books)
// { {
// QJsonObject j; QJsonObject j;
// j["id"] = QString::number(book.id()); j["id"] = QString::number(book.id());
// QJsonObject author; j["title"] = book.title();
// author["id"] = QString::number(book.author()->id());
// author["fullName"] = book.author()->fullName(); jArray.push_back(j);
// 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); 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); return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented);
} }
if (request.startsWith("GET /books")) if (request.startsWith("GET /books"))
{ {
odb::transaction t(m_db.begin()); repository::BookRepository b(m_db);
odb::result<Book_S> res(m_db.query<Book_S>());
QJsonArray jArray; QJsonArray jArray;
for (auto it = res.begin(); it != res.end(); ++it) for (const auto& book : b.findAll())
{ {
const auto& book = *it;
QJsonObject j; QJsonObject j;
j["id"] = QString::number(book.id()); 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["title"] = book.title();
// j["year"] = book.year();
// j["lastModified"] = book.lastModified().toString();
// j["lang"] = book.lang();
jArray.push_back(j); jArray.push_back(j);
} }
auto jDoc = QJsonDocument(jArray); 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 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented);
} }

View File

@@ -7,8 +7,6 @@
#include <QString> #include <QString>
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <external_libs/quazip/quazip/quazip.h> #include <external_libs/quazip/quazip/quazip.h>
#include <external_libs/quazip/quazip/quazipfile.h> // TODO для совместимости. Временно #include <external_libs/quazip/quazip/quazipfile.h> // TODO для совместимости. Временно