Добавляем так же в вывод связанную информацию
This commit is contained in:
@@ -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);
|
||||
|
||||
81
src/repository/authorrepository.cpp
Normal file
81
src/repository/authorrepository.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "authorrepository.h"
|
||||
|
||||
#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
|
||||
{
|
||||
|
||||
AuthorRepository::AuthorRepository(odb::core::database& db) :
|
||||
m_db(db)
|
||||
{}
|
||||
|
||||
QVector<Author_S> AuthorRepository::findAll()
|
||||
{
|
||||
odb::transaction t(m_db.begin());
|
||||
odb::result<Author_S> res(m_db.query<Author_S>());
|
||||
|
||||
QVector<Author_S> 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<Author_S> res(m_db.query<Author_S>(odb::query<Author_S>::id == id));
|
||||
|
||||
Author_S author;
|
||||
res.value(author);
|
||||
|
||||
t.commit();
|
||||
|
||||
return author;
|
||||
}
|
||||
|
||||
QVector<Author_S> AuthorRepository::findByBook(const int& bookId)
|
||||
{
|
||||
odb::transaction t(m_db.begin());
|
||||
|
||||
QVector<Author_S> authors;
|
||||
|
||||
using ABQuery = odb::query<AuthorBook_S>;
|
||||
using ABResult = odb::result<AuthorBook_S>;
|
||||
ABResult abResult(m_db.query<AuthorBook_S>(ABQuery::book == bookId));
|
||||
|
||||
for (const AuthorBook_S& ab : abResult)
|
||||
{
|
||||
// Загружаем каждую книгу по ID
|
||||
SH<Author_S> author(m_db.load<Author_S>(ab.author()->id()));
|
||||
authors.push_back(*author.data());
|
||||
}
|
||||
t.commit();
|
||||
|
||||
return authors;
|
||||
}
|
||||
|
||||
QVector<Author_S> AuthorRepository::findByBook(const QString& book_name)
|
||||
{
|
||||
odb::transaction t(m_db.begin());
|
||||
auto book_s = m_db.query<Book_S>(odb::query<Book_S>::title == book_name).one();
|
||||
|
||||
return findByBook(book_s->id());
|
||||
}
|
||||
|
||||
} // namespace repository
|
||||
41
src/repository/authorrepository.h
Normal file
41
src/repository/authorrepository.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef AUTHORREPOSITORY_H
|
||||
#define AUTHORREPOSITORY_H
|
||||
|
||||
#include "repository_global.h"
|
||||
|
||||
#include <model/books/author_s.h>
|
||||
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
|
||||
namespace repository
|
||||
{
|
||||
|
||||
class REPOSITORY_EXPORT AuthorRepository
|
||||
{
|
||||
public:
|
||||
AuthorRepository(odb::core::database& db);
|
||||
|
||||
// Получить всех авторов
|
||||
QVector<Author_S> findAll();
|
||||
|
||||
// Найти автора по id
|
||||
Author_S findById(int id);
|
||||
|
||||
// Сохранить книгу (создать или обновить)
|
||||
// void save(const Book_S& book);
|
||||
|
||||
// Удалить автора
|
||||
// void remove(int id);
|
||||
|
||||
// Найти авторов по книге
|
||||
QVector<Author_S> findByBook(const int& book_id);
|
||||
QVector<Author_S> findByBook(const QString& bookName);
|
||||
|
||||
private:
|
||||
odb::core::database& m_db;
|
||||
};
|
||||
|
||||
} // namespace repository
|
||||
|
||||
#endif // AUTHORREPOSITORY_H
|
||||
@@ -63,16 +63,15 @@ void BookRepository::remove(int id)
|
||||
t.commit();
|
||||
}
|
||||
|
||||
QVector<Book_S> BookRepository::findByAuthor(const QString& author)
|
||||
QVector<Book_S> BookRepository::findByAuthor(const int& authorId)
|
||||
{
|
||||
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()));
|
||||
ABResult abResult(m_db.query<AuthorBook_S>(ABQuery::author == authorId));
|
||||
|
||||
for (const AuthorBook_S& ab : abResult)
|
||||
{
|
||||
@@ -86,4 +85,12 @@ QVector<Book_S> BookRepository::findByAuthor(const QString& author)
|
||||
return books;
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
return findByAuthor(author_s->id());
|
||||
}
|
||||
|
||||
} // namespace repository
|
||||
|
||||
@@ -29,7 +29,8 @@ public:
|
||||
void remove(int id);
|
||||
|
||||
// Найти книги по автору
|
||||
QVector<Book_S> findByAuthor(const QString& author);
|
||||
QVector<Book_S> findByAuthor(const QString& authorName);
|
||||
QVector<Book_S> findByAuthor(const int& authorId);
|
||||
|
||||
private:
|
||||
odb::core::database& m_db;
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <model/books/book_s-odb.hxx> // Должен быть здесь
|
||||
#include <model/books/book_s.h>
|
||||
|
||||
#include <repository/authorrepository.h>
|
||||
#include <repository/bookrepository.h>
|
||||
|
||||
#include <odb/core.hxx>
|
||||
@@ -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<Author_S> res(m_db.query<Author_S>());
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user