Use json in foldercontentcontoller_v2

This commit is contained in:
Luis Ángel San Martín 2018-03-25 13:15:49 +02:00
parent c36d4501cc
commit c1ba2fe77e
3 changed files with 24 additions and 8 deletions

View File

@ -25,7 +25,7 @@ FolderContentControllerV2::FolderContentControllerV2() {}
void FolderContentControllerV2::service(HttpRequest& request, HttpResponse& response)
{
response.setHeader("Content-Type", "text/plain; charset=utf-8");
response.setHeader("Content-Type", "application/json");
QString path = QUrl::fromPercentEncoding(request.getPath()).toUtf8();
QStringList pathElements = path.split('/');
@ -34,13 +34,12 @@ void FolderContentControllerV2::service(HttpRequest& request, HttpResponse& resp
serviceContent(libraryId, parentId, response);
response.setStatus(200,"OK");
response.write("",true);
}
void FolderContentControllerV2::serviceContent(const int &library, const qulonglong &folderId, HttpResponse &response)
{
//clock_t begin = clock();
QList<LibraryItem *> folderContent = DBHelper::getFolderSubfoldersFromLibrary(library,folderId);
QList<LibraryItem *> folderComics = DBHelper::getFolderComicsFromLibrary(library,folderId);
@ -49,6 +48,8 @@ void FolderContentControllerV2::serviceContent(const int &library, const qulongl
folderComics.clear();
QJsonArray items;
ComicDB * currentComic;
Folder * currentFolder;
for(QList<LibraryItem *>::const_iterator itr = folderContent.constBegin();itr!=folderContent.constEnd();itr++)
@ -56,17 +57,16 @@ void FolderContentControllerV2::serviceContent(const int &library, const qulongl
if((*itr)->isDir())
{
currentFolder = (Folder *)(*itr);
response.write(YACReaderServerDataHelper::folderToYSFormat(library, *currentFolder).toUtf8());
items.append(YACReaderServerDataHelper::folderToJSON(library, *currentFolder));
}
else
{
currentComic = (ComicDB *)(*itr);
response.write(YACReaderServerDataHelper::comicToYSFormat(library, *currentComic).toUtf8());
items.append(YACReaderServerDataHelper::comicToJSON(library, *currentComic));
}
}
/*clock_t end = clock();
double msecs = double(end - begin);
QJsonDocument output(items);
response.write(QString("%1ms").arg(msecs).toUtf8());*/
response.write(output.toJson());
}

View File

@ -23,12 +23,27 @@ QString YACReaderServerDataHelper::comicToYSFormat(const qulonglong libraryId,co
.arg(comic.info.read?1:0);
}
QJsonObject YACReaderServerDataHelper::folderToJSON(const qulonglong libraryId, const Folder & folder)
{
QJsonObject json;
json["type"] = "folder";
json["id"] = QString::number(folder.id);
json["library_id"] = QString::number(libraryId);
json["folder_name"] = folder.name;
json["num_children"] = QString::number(folder.getNumChildren());
json["first_comic_hash"] = folder.getFirstChildHash();
return json;
}
QJsonObject YACReaderServerDataHelper::comicToJSON(const qulonglong libraryId, const ComicDB & comic)
{
QJsonObject json;
json["type"] = "comic";
json["id"] = QString::number(comic.id);
json["library_id"] = QString::number(libraryId);
json["file_name"] = comic.name;
json["file_size"] = QString::number(comic.getFileSize());
json["hash"] = comic.info.hash;

View File

@ -11,6 +11,7 @@ public:
static QString folderToYSFormat(const qulonglong libraryId, const Folder & folder);
static QString comicToYSFormat(const qulonglong libraryId, const ComicDB & comic);
static QJsonObject folderToJSON(const qulonglong libraryId, const Folder & folder);
static QJsonObject comicToJSON(const qulonglong libraryId, const ComicDB & comic);
private: