Все еще запрашиваем все записи из БД...
This commit is contained in:
@@ -18,33 +18,34 @@ class DATABASE_EXPORT Author_S
|
|||||||
public:
|
public:
|
||||||
Author_S() = default;
|
Author_S() = default;
|
||||||
|
|
||||||
Author_S(const QString& first,
|
Author_S(const QString& firstN,
|
||||||
const QString& last,
|
const QString& lastN,
|
||||||
const unsigned short age) :
|
const unsigned short age) :
|
||||||
m_first(first),
|
m_firstName(firstN),
|
||||||
m_last(last),
|
m_lastName(lastN),
|
||||||
m_age(age)
|
m_age(age)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long long id() const { return m_id; }
|
unsigned long long id() const { return m_id; }
|
||||||
QString first() const { return m_first; }
|
QString firstName() const { return m_firstName; }
|
||||||
QString last() const { return m_last; }
|
QString lastName() const { return m_lastName; }
|
||||||
unsigned short age() const { return m_age; }
|
unsigned short age() const { return m_age; }
|
||||||
|
|
||||||
QString full_name() const { return m_first + " " + m_last; }
|
QString full_name() const { return m_firstName + " " + m_lastName; }
|
||||||
|
|
||||||
void age(unsigned short age) { m_age = age; }
|
void age(unsigned short age) { m_age = age; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class odb::access;
|
friend class odb::access;
|
||||||
|
|
||||||
|
private:
|
||||||
#pragma db id auto
|
#pragma db id auto
|
||||||
unsigned long long m_id;
|
quint64 m_id;
|
||||||
|
|
||||||
QString m_first;
|
QString m_firstName;
|
||||||
QString m_last;
|
QString m_lastName;
|
||||||
unsigned short m_age;
|
qint8 m_age;
|
||||||
};
|
};
|
||||||
|
|
||||||
#pragma db view object(Author_S)
|
#pragma db view object(Author_S)
|
||||||
|
|||||||
@@ -38,11 +38,27 @@ private:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
#pragma db id auto
|
#pragma db id auto
|
||||||
unsigned long long m_id;
|
quint64 m_id;
|
||||||
|
|
||||||
SH<Author_S> m_author;
|
SH<Author_S> m_author;
|
||||||
QString m_name;
|
QString m_name;
|
||||||
int m_year;
|
qint8 m_year;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// #pragma db view object(Book_S) object(Author_S = author:Book_S::m_author)
|
||||||
|
// struct BookByAuthorView
|
||||||
|
// {
|
||||||
|
// #pragma db column(Book_S::m_id)
|
||||||
|
// quint64 book_id;
|
||||||
|
|
||||||
|
// #pragma db column(Book_S::m_name)
|
||||||
|
// QString book_name;
|
||||||
|
|
||||||
|
// #pragma db column(Book_S::m_year)
|
||||||
|
// qint8 year;
|
||||||
|
|
||||||
|
// #pragma db column(Author_S::m_first + " " + Author_S::m_last)
|
||||||
|
// QString author_full_name;
|
||||||
|
// };
|
||||||
|
|
||||||
#endif // BOOK_S_H
|
#endif // BOOK_S_H
|
||||||
|
|||||||
@@ -15,17 +15,19 @@ PSLibrary {
|
|||||||
|
|
||||||
Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] }
|
Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] }
|
||||||
Depends { name: "cpp" }
|
Depends { name: "cpp" }
|
||||||
|
|
||||||
Depends { name: "odb.gen" }
|
Depends { name: "odb.gen" }
|
||||||
Depends { name: "redkit_gen" }
|
|
||||||
Depends { name: "rdbase" }
|
Depends { name: "rdbase" }
|
||||||
|
|
||||||
|
Depends { name: "redkit_gen" }
|
||||||
|
redkit_gen.includeModules: ["JsonSerializer"]
|
||||||
|
|
||||||
odb.gen.databases: "sqlite"
|
odb.gen.databases: "sqlite"
|
||||||
cpp.cxxLanguageVersion: "c++17"
|
cpp.cxxLanguageVersion: "c++17"
|
||||||
|
|
||||||
Group {
|
Group {
|
||||||
name: "cpp"
|
name: "cpp"
|
||||||
files: [
|
files: [
|
||||||
"books.*",
|
|
||||||
"databasemanager.*",
|
"databasemanager.*",
|
||||||
"*.cpp",
|
"*.cpp",
|
||||||
"*.h",
|
"*.h",
|
||||||
@@ -41,7 +43,7 @@ PSLibrary {
|
|||||||
"author_s.h",
|
"author_s.h",
|
||||||
"book_s.h",
|
"book_s.h",
|
||||||
]
|
]
|
||||||
fileTags: ["hpp", "odbxx", "odb"]
|
fileTags: ["hpp", "odbxx"]
|
||||||
}
|
}
|
||||||
|
|
||||||
cpp.dynamicLibraries: [
|
cpp.dynamicLibraries: [
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
#include <odb/database.hxx>
|
#include <odb/database.hxx>
|
||||||
#include <odb/query.hxx>
|
#include <odb/query.hxx>
|
||||||
|
|
||||||
|
#include <redkit-gen-modules/json_serializer.h>
|
||||||
|
|
||||||
RestApiServer::RestApiServer(odb::database& db, QObject* parent) :
|
RestApiServer::RestApiServer(odb::database& db, QObject* parent) :
|
||||||
QTcpServer(parent), m_db(db) {}
|
QTcpServer(parent), m_db(db) {}
|
||||||
|
|
||||||
@@ -61,9 +63,56 @@ QByteArray RestApiServer::processRequest(const QString& request)
|
|||||||
{
|
{
|
||||||
QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " ");
|
QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " ");
|
||||||
|
|
||||||
qWarning() << "THIS";
|
quint64 ageReq = author.toInt();
|
||||||
|
|
||||||
|
qWarning() << "THIS" << author << ageReq;
|
||||||
|
|
||||||
|
// if (dbConn.query_one<SDB_T>(odb::query<SDB_T>::object == m_objectId, sett))
|
||||||
|
// odb::result<Book_S> res(m_db.query<Book_S>(odb::query<Book_S>::author-> == author));
|
||||||
|
|
||||||
|
// {
|
||||||
|
|
||||||
|
QStringList nameParts = author.split(' ');
|
||||||
|
QString firstName = nameParts.size() > 0 ? nameParts[0] : "";
|
||||||
|
QString lastName = nameParts.size() > 1 ? nameParts[1] : "";
|
||||||
|
|
||||||
|
qWarning()
|
||||||
|
<< "author:" << author << "\n"
|
||||||
|
<< "firstName:" << firstName << "\n"
|
||||||
|
<< "lastName:" << lastName << "\n";
|
||||||
|
|
||||||
|
// Result result(m_db.query<Book_S>(
|
||||||
|
// (Query::author->m_first + " " + Query::author->m_last == authorName) ||
|
||||||
|
// (Query::author->m_first.like("%" + firstName + "%") &&
|
||||||
|
// Query::author->m_last.like("%" + lastName + "%"))
|
||||||
|
// ));
|
||||||
|
|
||||||
odb::transaction t(m_db.begin());
|
odb::transaction t(m_db.begin());
|
||||||
odb::result<Book_S> res(m_db.query<Book_S>());
|
|
||||||
|
auto books = m_db.query<Book_S>();
|
||||||
|
|
||||||
|
// using Query = odb::query<Author_S>;
|
||||||
|
// auto authors = m_db.query<Author_S>(Query::id == 2);
|
||||||
|
|
||||||
|
auto authors = m_db.query<Author_S>();
|
||||||
|
|
||||||
|
for (auto book : books)
|
||||||
|
qWarning() << book.name() << book.author()->full_name();
|
||||||
|
|
||||||
|
for (auto author : authors)
|
||||||
|
qWarning() << author.full_name() << author.age();
|
||||||
|
|
||||||
|
// std::vector<SH<Book_S>> books(result.begin(), result.end());
|
||||||
|
|
||||||
|
t.commit();
|
||||||
|
// return books;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (auto book : books)
|
||||||
|
// qWarning() << book.author()->full_name();
|
||||||
|
|
||||||
|
// qWarning() << books;
|
||||||
|
|
||||||
// QList<BookRecord> results = books.getBooksByAuthor(author);
|
// QList<BookRecord> results = books.getBooksByAuthor(author);
|
||||||
|
|
||||||
QByteArray jsonResponse = "{ \"books\": [";
|
QByteArray jsonResponse = "{ \"books\": [";
|
||||||
@@ -121,8 +170,8 @@ QByteArray RestApiServer::processRequest(const QString& request)
|
|||||||
|
|
||||||
QJsonObject j;
|
QJsonObject j;
|
||||||
j["id"] = QString::number(author.id());
|
j["id"] = QString::number(author.id());
|
||||||
j["first"] = author.first();
|
j["first"] = author.firstName();
|
||||||
j["last"] = author.last();
|
j["last"] = author.lastName();
|
||||||
j["age"] = author.age();
|
j["age"] = author.age();
|
||||||
jArray.push_back(j);
|
jArray.push_back(j);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user