82 lines
1.8 KiB
C++
82 lines
1.8 KiB
C++
#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
|