diff --git a/src/database/author_s.h b/src/database/author_s.h index 5c9b2da..5451fba 100644 --- a/src/database/author_s.h +++ b/src/database/author_s.h @@ -45,7 +45,7 @@ private: QString m_firstName; QString m_lastName; - qint8 m_age; + quint8 m_age; }; #pragma db view object(Author_S) diff --git a/src/database/book_s.h b/src/database/book_s.h index adcf45c..d516b51 100644 --- a/src/database/book_s.h +++ b/src/database/book_s.h @@ -42,7 +42,7 @@ private: SH 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) diff --git a/src/main.cpp b/src/main.cpp index 72d6b4a..64d7771 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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); diff --git a/src/restapi/restapi.qbs b/src/restapi/restapi.qbs index 37ccfa9..cdce2c0 100644 --- a/src/restapi/restapi.qbs +++ b/src/restapi/restapi.qbs @@ -27,4 +27,12 @@ WPSLibrary { "restapiserver.*", ] } + + cpp.dynamicLibraries: [ + "odb-sqlite", + "odb-qt", + "odb", + "sqlite3" + ] + } // Project diff --git a/src/restapi/restapiserver.cpp b/src/restapi/restapiserver.cpp index 0094ab4..22ee409 100644 --- a/src/restapi/restapiserver.cpp +++ b/src/restapi/restapiserver.cpp @@ -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(odb::query::object == m_objectId, sett)) - // odb::result res(m_db.query(odb::query::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( - // (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(); + auto books = m_db.query(odb::query::author->id == ageReq); - // using Query = odb::query; - // auto authors = m_db.query(Query::id == 2); - - auto authors = m_db.query(); - - for (auto book : books) - qWarning() << book.name() << book.author()->full_name(); - - for (auto author : authors) - qWarning() << author.full_name() << author.age(); - - // std::vector> 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 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"))