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