mirror of
https://github.com/YACReader/yacreader
synced 2025-06-04 01:28:55 -04:00
Implement a server controller to provide a search API
This commit is contained in:
parent
5c0b5c7430
commit
4198b5ca3a
103
YACReaderLibrary/server/controllers/v2/searchcontroller_v2.cpp
Normal file
103
YACReaderLibrary/server/controllers/v2/searchcontroller_v2.cpp
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
|
||||||
|
#include "searchcontroller_v2.h"
|
||||||
|
|
||||||
|
#include "data_base_management.h"
|
||||||
|
#include "db_helper.h"
|
||||||
|
#include "yacreader_libraries.h"
|
||||||
|
#include "search_query.h"
|
||||||
|
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <QSqlDatabase>
|
||||||
|
#include <QUrl>
|
||||||
|
|
||||||
|
SearchController::SearchController() { }
|
||||||
|
|
||||||
|
void SearchController::service(stefanfrings::HttpRequest &request, stefanfrings::HttpResponse &response)
|
||||||
|
{
|
||||||
|
response.setHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
|
QString path = QUrl::fromPercentEncoding(request.getPath()).toUtf8();
|
||||||
|
QStringList pathElements = path.split('/');
|
||||||
|
int libraryId = pathElements.at(3).toInt();
|
||||||
|
|
||||||
|
auto body = request.getBody();
|
||||||
|
QJsonDocument json = QJsonDocument::fromJson(body);
|
||||||
|
auto query = json["query"].toString();
|
||||||
|
|
||||||
|
response.setStatus(200, "OK");
|
||||||
|
serviceSearch(libraryId, query, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SearchController::serviceSearch(int libraryId, const QString &query, stefanfrings::HttpResponse &response)
|
||||||
|
{
|
||||||
|
QJsonArray results;
|
||||||
|
|
||||||
|
// TODO replace + "/yacreaderlibrary" concatenations with getDBPath
|
||||||
|
QString libraryDBPath = DBHelper::getLibraries().getDBPath(libraryId);
|
||||||
|
QString connectionName = "";
|
||||||
|
{
|
||||||
|
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryDBPath);
|
||||||
|
|
||||||
|
// folders
|
||||||
|
try {
|
||||||
|
auto sqlQuery = foldersSearchQuery(db, query);
|
||||||
|
getFolders(libraryId, sqlQuery, results);
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// comics
|
||||||
|
try {
|
||||||
|
auto sqlQuery = foldersSearchQuery(db, query);
|
||||||
|
getComics(libraryId, sqlQuery, results);
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
connectionName = db.connectionName();
|
||||||
|
}
|
||||||
|
QSqlDatabase::removeDatabase(connectionName);
|
||||||
|
|
||||||
|
QJsonDocument output(results);
|
||||||
|
|
||||||
|
response.write(output.toJson(QJsonDocument::Compact));
|
||||||
|
}
|
||||||
|
|
||||||
|
void SearchController::getFolders(int libraryId, QSqlQuery &sqlQuery, QJsonArray &items)
|
||||||
|
{
|
||||||
|
while (sqlQuery.next()) {
|
||||||
|
QJsonObject folder;
|
||||||
|
|
||||||
|
folder["type"] = "folder";
|
||||||
|
folder["id"] = sqlQuery.value("id").toString();
|
||||||
|
folder["library_id"] = QString::number(libraryId);
|
||||||
|
folder["folder_name"] = sqlQuery.value("name").toString();
|
||||||
|
folder["num_children"] = sqlQuery.value("numChildren").toInt();
|
||||||
|
folder["first_comic_hash"] = sqlQuery.value("firstChildHash").toInt();
|
||||||
|
|
||||||
|
items.append(folder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SearchController::getComics(int libraryId, QSqlQuery &sqlQuery, QJsonArray &items)
|
||||||
|
{
|
||||||
|
while (sqlQuery.next()) {
|
||||||
|
QJsonObject json;
|
||||||
|
|
||||||
|
json["type"] = "comic";
|
||||||
|
json["id"] = sqlQuery.value("id").toString();
|
||||||
|
json["library_id"] = QString::number(libraryId);
|
||||||
|
json["file_name"] = sqlQuery.value("fileName").toString();
|
||||||
|
auto hash = sqlQuery.value("hash").toString();
|
||||||
|
json["file_size"] = hash.right(hash.length() - 40).toLongLong();
|
||||||
|
json["hash"] = hash;
|
||||||
|
json["current_page"] = sqlQuery.value("currentPage").toInt();
|
||||||
|
json["num_pages"] = sqlQuery.value("numPages").toInt();
|
||||||
|
json["read"] = sqlQuery.value("read").toBool();
|
||||||
|
json["cover_size_ratio"] = sqlQuery.value("coverSizeRatio").toFloat();
|
||||||
|
json["title"] = sqlQuery.value("title").toString();
|
||||||
|
json["number"] = sqlQuery.value("number").toInt();
|
||||||
|
json["last_time_opened"] = sqlQuery.value("lastTimeOpened").toLongLong();
|
||||||
|
json["manga"] = sqlQuery.value("manga").toBool();
|
||||||
|
|
||||||
|
items.append(json);
|
||||||
|
}
|
||||||
|
}
|
26
YACReaderLibrary/server/controllers/v2/searchcontroller_v2.h
Normal file
26
YACReaderLibrary/server/controllers/v2/searchcontroller_v2.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
#ifndef SEARCHCONTROLLER_H
|
||||||
|
#define SEARCHCONTROLLER_H
|
||||||
|
|
||||||
|
#include "httprequest.h"
|
||||||
|
#include "httpresponse.h"
|
||||||
|
#include "httprequesthandler.h"
|
||||||
|
|
||||||
|
#include <QSqlQuery>
|
||||||
|
|
||||||
|
class SearchController : public stefanfrings::HttpRequestHandler
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
Q_DISABLE_COPY(SearchController)
|
||||||
|
public:
|
||||||
|
SearchController();
|
||||||
|
|
||||||
|
void service(stefanfrings::HttpRequest &request, stefanfrings::HttpResponse &response) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void serviceSearch(int libraryId, const QString &query, stefanfrings::HttpResponse &response);
|
||||||
|
void getFolders(int libraryId, QSqlQuery &sqlQuery, QJsonArray &items);
|
||||||
|
void getComics(int libraryId, QSqlQuery &sqlQuery, QJsonArray &items);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SEARCHCONTROLLER_H
|
@ -34,6 +34,7 @@
|
|||||||
#include "controllers/v2/readinglistinfocontroller_v2.h"
|
#include "controllers/v2/readinglistinfocontroller_v2.h"
|
||||||
#include "controllers/v2/comicfullinfocontroller_v2.h"
|
#include "controllers/v2/comicfullinfocontroller_v2.h"
|
||||||
#include "controllers/v2/comiccontrollerinreadinglist_v2.h"
|
#include "controllers/v2/comiccontrollerinreadinglist_v2.h"
|
||||||
|
#include "controllers/v2/searchcontroller_v2.h"
|
||||||
|
|
||||||
#include "controllers/webui/statuspagecontroller.h"
|
#include "controllers/webui/statuspagecontroller.h"
|
||||||
|
|
||||||
@ -257,6 +258,7 @@ void RequestMapper::serviceV2(HttpRequest &request, HttpResponse &response)
|
|||||||
QRegExp readingLists("/v2/library/.+/reading_lists/?");
|
QRegExp readingLists("/v2/library/.+/reading_lists/?");
|
||||||
QRegExp readingListContent("/v2/library/.+/reading_list/[0-9]+/content/?");
|
QRegExp readingListContent("/v2/library/.+/reading_list/[0-9]+/content/?");
|
||||||
QRegExp readingListInfo("/v2/library/.+/reading_list/[0-9]+/info/?");
|
QRegExp readingListInfo("/v2/library/.+/reading_list/[0-9]+/info/?");
|
||||||
|
QRegExp search("/v2/library/.+/search/?");
|
||||||
|
|
||||||
QRegExp sync("/v2/sync");
|
QRegExp sync("/v2/sync");
|
||||||
|
|
||||||
@ -318,6 +320,8 @@ void RequestMapper::serviceV2(HttpRequest &request, HttpResponse &response)
|
|||||||
ReadingListInfoControllerV2().service(request, response);
|
ReadingListInfoControllerV2().service(request, response);
|
||||||
} else if (tagInfo.exactMatch(path)) {
|
} else if (tagInfo.exactMatch(path)) {
|
||||||
TagInfoControllerV2().service(request, response);
|
TagInfoControllerV2().service(request, response);
|
||||||
|
} else if (search.exactMatch(path)) {
|
||||||
|
SearchController().service(request, response);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// response.writeText(library.cap(1));
|
// response.writeText(library.cap(1));
|
||||||
|
@ -9,6 +9,7 @@ DEPENDPATH += $$PWD/controllers/v2
|
|||||||
|
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
|
$$PWD/controllers/v2/searchcontroller_v2.h \
|
||||||
$$PWD/static.h \
|
$$PWD/static.h \
|
||||||
$$PWD/requestmapper.h \
|
$$PWD/requestmapper.h \
|
||||||
$$PWD/yacreader_http_server.h \
|
$$PWD/yacreader_http_server.h \
|
||||||
@ -53,6 +54,7 @@ HEADERS += \
|
|||||||
|
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
|
$$PWD/controllers/v2/searchcontroller_v2.cpp \
|
||||||
$$PWD/static.cpp \
|
$$PWD/static.cpp \
|
||||||
$$PWD/requestmapper.cpp \
|
$$PWD/requestmapper.cpp \
|
||||||
$$PWD/yacreader_http_server.cpp \
|
$$PWD/yacreader_http_server.cpp \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user