diff --git a/src/main.cpp b/src/main.cpp index 3524cce..68626cb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -118,7 +118,7 @@ int main(int argc, char* argv[]) uDBase db(openDB(dbPath)); // TODO Как-то нужно выполнять лишь раз - fillBooksBD(db); + // fillBooksBD(db); RestApiServer server(*db); server.start(8080); diff --git a/src/repository/authorrepository.cpp b/src/repository/authorrepository.cpp new file mode 100644 index 0000000..f28bc09 --- /dev/null +++ b/src/repository/authorrepository.cpp @@ -0,0 +1,81 @@ +#include "authorrepository.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace repository +{ + +AuthorRepository::AuthorRepository(odb::core::database& db) : + m_db(db) +{} + +QVector AuthorRepository::findAll() +{ + odb::transaction t(m_db.begin()); + odb::result res(m_db.query()); + + QVector authors; + + for (auto it = res.begin(); it != res.end(); ++it) + { + const auto& author = *it; + authors.push_back(author); + } + + t.commit(); + + return authors; +} + +Author_S AuthorRepository::findById(int id) +{ + odb::transaction t(m_db.begin()); + odb::result res(m_db.query(odb::query::id == id)); + + Author_S author; + res.value(author); + + t.commit(); + + return author; +} + +QVector AuthorRepository::findByBook(const int& bookId) +{ + odb::transaction t(m_db.begin()); + + QVector authors; + + using ABQuery = odb::query; + using ABResult = odb::result; + ABResult abResult(m_db.query(ABQuery::book == bookId)); + + for (const AuthorBook_S& ab : abResult) + { + // Загружаем каждую книгу по ID + SH author(m_db.load(ab.author()->id())); + authors.push_back(*author.data()); + } + t.commit(); + + return authors; +} + +QVector AuthorRepository::findByBook(const QString& book_name) +{ + odb::transaction t(m_db.begin()); + auto book_s = m_db.query(odb::query::title == book_name).one(); + + return findByBook(book_s->id()); +} + +} // namespace repository diff --git a/src/repository/authorrepository.h b/src/repository/authorrepository.h new file mode 100644 index 0000000..76c8454 --- /dev/null +++ b/src/repository/authorrepository.h @@ -0,0 +1,41 @@ +#ifndef AUTHORREPOSITORY_H +#define AUTHORREPOSITORY_H + +#include "repository_global.h" + +#include + +#include +#include + +namespace repository +{ + +class REPOSITORY_EXPORT AuthorRepository +{ +public: + AuthorRepository(odb::core::database& db); + + // Получить всех авторов + QVector findAll(); + + // Найти автора по id + Author_S findById(int id); + + // Сохранить книгу (создать или обновить) + // void save(const Book_S& book); + + // Удалить автора + // void remove(int id); + + // Найти авторов по книге + QVector findByBook(const int& book_id); + QVector findByBook(const QString& bookName); + +private: + odb::core::database& m_db; +}; + +} // namespace repository + +#endif // AUTHORREPOSITORY_H diff --git a/src/repository/bookrepository.cpp b/src/repository/bookrepository.cpp index 1e8778e..af7ddee 100644 --- a/src/repository/bookrepository.cpp +++ b/src/repository/bookrepository.cpp @@ -63,16 +63,15 @@ void BookRepository::remove(int id) t.commit(); } -QVector BookRepository::findByAuthor(const QString& author) +QVector BookRepository::findByAuthor(const int& authorId) { 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())); + ABResult abResult(m_db.query(ABQuery::author == authorId)); for (const AuthorBook_S& ab : abResult) { @@ -86,4 +85,12 @@ QVector BookRepository::findByAuthor(const QString& author) return books; } +QVector BookRepository::findByAuthor(const QString& author) +{ + odb::transaction t(m_db.begin()); + auto author_s = m_db.query(odb::query::fullName == author).one(); + + return findByAuthor(author_s->id()); +} + } // namespace repository diff --git a/src/repository/bookrepository.h b/src/repository/bookrepository.h index 8b620c7..f1918b8 100644 --- a/src/repository/bookrepository.h +++ b/src/repository/bookrepository.h @@ -29,7 +29,8 @@ public: void remove(int id); // Найти книги по автору - QVector findByAuthor(const QString& author); + QVector findByAuthor(const QString& authorName); + QVector findByAuthor(const int& authorId); private: odb::core::database& m_db; diff --git a/src/restapi/restapiserver.cpp b/src/restapi/restapiserver.cpp index 24ebb60..b84c4a2 100644 --- a/src/restapi/restapiserver.cpp +++ b/src/restapi/restapiserver.cpp @@ -10,6 +10,7 @@ #include // Должен быть здесь #include +#include #include #include @@ -76,7 +77,6 @@ QByteArray RestApiServer::processRequest(const QString& request) { QJsonObject j; j["id"] = QString::number(book.id()); - j["title"] = book.title(); jArray.push_back(j); @@ -97,11 +97,19 @@ QByteArray RestApiServer::processRequest(const QString& request) 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; + QJsonArray authors; + + repository::AuthorRepository a(m_db); + + for (const auto& author : a.findByBook(book.id())) + { + QJsonObject jj; + jj["id"] = QString::number(author.id()); + jj["fullName"] = author.fullName(); + authors.push_back(jj); + } + + j["authors"] = authors; j["title"] = book.title(); auto jDoc = QJsonDocument(j); @@ -120,6 +128,17 @@ QByteArray RestApiServer::processRequest(const QString& request) j["id"] = QString::number(book.id()); j["title"] = book.title(); + QJsonArray authors; + repository::AuthorRepository a(m_db); + for (const auto& author : a.findByBook(book.id())) + { + QJsonObject jj; + jj["id"] = QString::number(author.id()); + jj["fullName"] = author.fullName(); + authors.push_back(jj); + } + j["authors"] = authors; + jArray.push_back(j); } auto jDoc = QJsonDocument(jArray); @@ -127,10 +146,10 @@ QByteArray RestApiServer::processRequest(const QString& request) return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented); } - if (request.startsWith("GET /author")) + if (request.startsWith("GET /authors")) { - odb::transaction t(m_db.begin()); - odb::result res(m_db.query()); + repository::AuthorRepository a(m_db); + auto res = a.findAll(); QJsonArray jArray; for (auto it = res.begin(); it != res.end(); ++it) @@ -140,11 +159,21 @@ QByteArray RestApiServer::processRequest(const QString& request) QJsonObject j; j["id"] = QString::number(author.id()); j["fullName"] = author.fullName(); - j["langCode"] = author.langCode(); + + QJsonArray books; + repository::BookRepository b(m_db); + for (const auto& book : b.findByAuthor(author.id())) + { + QJsonObject jj; + jj["id"] = QString::number(book.id()); + jj["title"] = book.title(); + books.push_back(jj); + } + j["books"] = books; + 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); }