нормальный SQL поиск
This commit is contained in:
@@ -45,7 +45,7 @@ private:
|
||||
|
||||
QString m_firstName;
|
||||
QString m_lastName;
|
||||
qint8 m_age;
|
||||
quint8 m_age;
|
||||
};
|
||||
|
||||
#pragma db view object(Author_S)
|
||||
|
||||
@@ -42,7 +42,7 @@ private:
|
||||
|
||||
SH<Author_S> m_author;
|
||||
QString m_name;
|
||||
qint8 m_year;
|
||||
quint8 m_year;
|
||||
};
|
||||
|
||||
// #pragma db view object(Book_S) object(Author_S = author:Book_S::m_author)
|
||||
|
||||
@@ -128,7 +128,8 @@ int main(int argc, char* argv[])
|
||||
const std::string dbPath = "test_db.sqlite";
|
||||
uDBase db(openDB(dbPath));
|
||||
|
||||
fillBooksBD(db);
|
||||
// TODO Как-то нужно выполнять лишь раз
|
||||
// fillBooksBD(db);
|
||||
|
||||
RestApiServer server(*db);
|
||||
server.start(8080);
|
||||
|
||||
@@ -27,4 +27,12 @@ WPSLibrary {
|
||||
"restapiserver.*",
|
||||
]
|
||||
}
|
||||
|
||||
cpp.dynamicLibraries: [
|
||||
"odb-sqlite",
|
||||
"odb-qt",
|
||||
"odb",
|
||||
"sqlite3"
|
||||
]
|
||||
|
||||
} // Project
|
||||
|
||||
@@ -59,19 +59,13 @@ void RestApiServer::handleRequest()
|
||||
|
||||
QByteArray RestApiServer::processRequest(const QString& request)
|
||||
{
|
||||
qWarning() << request << "\n\n";
|
||||
|
||||
if (request.startsWith("GET /books/author/"))
|
||||
{
|
||||
QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " ");
|
||||
|
||||
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] : "";
|
||||
@@ -81,55 +75,25 @@ QByteArray RestApiServer::processRequest(const QString& request)
|
||||
<< "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());
|
||||
|
||||
auto books = m_db.query<Book_S>();
|
||||
auto books = m_db.query<Book_S>(odb::query<Book_S>::author->id == ageReq);
|
||||
|
||||
// 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());
|
||||
QJsonArray jArray;
|
||||
for (const auto& book : books)
|
||||
{
|
||||
QJsonObject j;
|
||||
j["id"] = QString::number(book.id());
|
||||
j["name"] = book.name();
|
||||
j["author"] = book.author()->full_name();
|
||||
j["year"] = book.year();
|
||||
jArray.push_back(j);
|
||||
}
|
||||
auto jDoc = QJsonDocument(jArray);
|
||||
|
||||
t.commit();
|
||||
// return books;
|
||||
// }
|
||||
|
||||
// for (auto book : books)
|
||||
// qWarning() << book.author()->full_name();
|
||||
|
||||
// qWarning() << books;
|
||||
|
||||
// QList<BookRecord> results = books.getBooksByAuthor(author);
|
||||
|
||||
QByteArray jsonResponse = "{ \"books\": [";
|
||||
// for (const BookRecord& book : results)
|
||||
// {
|
||||
// jsonResponse += QString("{ \"id\": %1, \"title\": \"%2\", \"author\": \"%3\", \"year\": %4 },")
|
||||
// .arg(book.id)
|
||||
// .arg(book.title)
|
||||
// .arg(book.author)
|
||||
// .arg(book.year)
|
||||
// .toUtf8();
|
||||
// }
|
||||
// if (results.size() > 0)
|
||||
// jsonResponse.chop(1); // Убираем последнюю запятую
|
||||
jsonResponse += "] }";
|
||||
|
||||
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jsonResponse;
|
||||
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jDoc.toJson(QJsonDocument::Indented);
|
||||
}
|
||||
|
||||
if (request.startsWith("GET /books"))
|
||||
|
||||
Reference in New Issue
Block a user