diff --git a/YACReaderLibrary/db_helper.cpp b/YACReaderLibrary/db_helper.cpp index 9d53105b..074f2194 100644 --- a/YACReaderLibrary/db_helper.cpp +++ b/YACReaderLibrary/db_helper.cpp @@ -773,6 +773,10 @@ void DBHelper::updateFromRemoteClientWithHash(const ComicInfo &comicInfo) ComicInfo info = loadComicInfo(comicInfo.hash, db); + if (!info.existOnDb) { + continue; + } + if (comicInfo.currentPage > 0) { info.currentPage = comicInfo.currentPage; @@ -795,6 +799,121 @@ void DBHelper::updateFromRemoteClientWithHash(const ComicInfo &comicInfo) } } +void DBHelper::updateFromRemoteClient(const QMap> &comics) +{ + foreach (qulonglong libraryId, comics.keys()) { + QString libraryPath = DBHelper::getLibraries().getPath(libraryId); + QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary"); + + db.transaction(); + + QSqlQuery updateComicInfo(db); + updateComicInfo.prepare("UPDATE comic_info SET " + "read = :read, " + "currentPage = :currentPage, " + "hasBeenOpened = :hasBeenOpened, " + "lastTimeOpened = :lastTimeOpened, " + "rating = :rating" + " WHERE id = :id "); + + foreach (ComicInfo comicInfo, comics[libraryId]) { + ComicDB comic = DBHelper::loadComic(comicInfo.id, db); + + if (comic.info.hash == comicInfo.hash) { + if (comicInfo.currentPage > 0) { + comic.info.currentPage = comicInfo.currentPage; + + if (comic.info.currentPage == comic.info.numPages) + comic.info.read = true; + + comic.info.hasBeenOpened = true; + + if (comic.info.lastTimeOpened.toULongLong() < comicInfo.lastTimeOpened.toULongLong()) + comic.info.lastTimeOpened = comicInfo.lastTimeOpened; + } + + if (comicInfo.rating > 0) + comic.info.rating = comicInfo.rating; + + updateComicInfo.bindValue(":read", comic.info.read ? 1 : 0); + updateComicInfo.bindValue(":currentPage", comic.info.currentPage); + updateComicInfo.bindValue(":hasBeenOpened", comic.info.hasBeenOpened ? 1 : 0); + updateComicInfo.bindValue(":lastTimeOpened", QDateTime::currentMSecsSinceEpoch() / 1000); + updateComicInfo.bindValue(":id", comic.info.id); + updateComicInfo.bindValue(":rating", comic.info.rating); + updateComicInfo.exec(); + + updateComicInfo.clear(); + } + } + + db.commit(); + + db.close(); + QSqlDatabase::removeDatabase(db.connectionName()); + } +} + +void DBHelper::updateFromRemoteClientWithHash(const QList &comics) +{ + YACReaderLibraries libraries = DBHelper::getLibraries(); + + QStringList names = libraries.getNames(); + + foreach (QString name, names) { + QString libraryPath = DBHelper::getLibraries().getPath(libraries.getId(name)); + + QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary"); + + db.transaction(); + + QSqlQuery updateComicInfo(db); + updateComicInfo.prepare("UPDATE comic_info SET " + "read = :read, " + "currentPage = :currentPage, " + "hasBeenOpened = :hasBeenOpened, " + "lastTimeOpened = :lastTimeOpened, " + "rating = :rating" + " WHERE id = :id "); + + foreach (ComicInfo comicInfo, comics) { + ComicInfo info = loadComicInfo(comicInfo.hash, db); + + if (!info.existOnDb) { + continue; + } + + if (comicInfo.currentPage > 0) { + info.currentPage = comicInfo.currentPage; + + if (info.currentPage == info.numPages) + info.read = true; + + info.hasBeenOpened = true; + + if (info.lastTimeOpened.toULongLong() < comicInfo.lastTimeOpened.toULongLong()) + info.lastTimeOpened = comicInfo.lastTimeOpened; + } + + if (comicInfo.rating > 0) { + info.rating = comicInfo.rating; + } + + updateComicInfo.bindValue(":read", info.read ? 1 : 0); + updateComicInfo.bindValue(":currentPage", info.currentPage); + updateComicInfo.bindValue(":hasBeenOpened", info.hasBeenOpened ? 1 : 0); + updateComicInfo.bindValue(":lastTimeOpened", QDateTime::currentMSecsSinceEpoch() / 1000); + updateComicInfo.bindValue(":id", info.id); + updateComicInfo.bindValue(":rating", info.rating); + updateComicInfo.exec(); + ; + } + + db.close(); + QSqlDatabase::removeDatabase(db.connectionName()); + } +} + void DBHelper::renameLabel(qulonglong id, const QString &name, QSqlDatabase &db) { QSqlQuery renameLabelQuery(db); diff --git a/YACReaderLibrary/db_helper.h b/YACReaderLibrary/db_helper.h index 9b4150ee..fb61b854 100644 --- a/YACReaderLibrary/db_helper.h +++ b/YACReaderLibrary/db_helper.h @@ -69,9 +69,11 @@ public: static void updateChildrenInfo(QSqlDatabase &db); static void updateProgress(qulonglong libraryId, const ComicInfo &comicInfo); static void setComicAsReading(qulonglong libraryId, const ComicInfo &comicInfo); - static void updateReadingRemoteProgress(const ComicInfo &comicInfo, QSqlDatabase &db); static void updateFromRemoteClient(qulonglong libraryId, const ComicInfo &comicInfo); static void updateFromRemoteClientWithHash(const ComicInfo &comicInfo); + static void updateReadingRemoteProgress(const ComicInfo &comicInfo, QSqlDatabase &db); + static void updateFromRemoteClient(const QMap> &comics); + static void updateFromRemoteClientWithHash(const QList &comics); static void renameLabel(qulonglong id, const QString &name, QSqlDatabase &db); static void renameList(qulonglong id, const QString &name, QSqlDatabase &db); static void reasignOrderToSublists(QList ids, QSqlDatabase &db); diff --git a/YACReaderLibrary/server/controllers/v2/synccontroller_v2.cpp b/YACReaderLibrary/server/controllers/v2/synccontroller_v2.cpp index 99bfc972..1b8ced6e 100644 --- a/YACReaderLibrary/server/controllers/v2/synccontroller_v2.cpp +++ b/YACReaderLibrary/server/controllers/v2/synccontroller_v2.cpp @@ -27,6 +27,8 @@ void SyncControllerV2::service(HttpRequest &request, HttpResponse &response) int currentRating; unsigned long long lastTimeOpened; QString hash; + QMap> comics; + QList comicsWithNoLibrary; foreach (QString comicInfo, data) { QList comicInfoProgress = comicInfo.split("\t"); @@ -47,8 +49,10 @@ void SyncControllerV2::service(HttpRequest &request, HttpResponse &response) lastTimeOpened = comicInfoProgress.at(5).toULong(); info.lastTimeOpened = lastTimeOpened; - - DBHelper::updateFromRemoteClient(libraryId, info); + if (!comics.contains(libraryId)) { + comics[libraryId] = QList(); + } + comics[libraryId].push_back(info); } else { hash = comicInfoProgress.at(2); currentPage = comicInfoProgress.at(3).toInt(); @@ -63,10 +67,13 @@ void SyncControllerV2::service(HttpRequest &request, HttpResponse &response) lastTimeOpened = comicInfoProgress.at(5).toULong(); info.lastTimeOpened = lastTimeOpened; - DBHelper::updateFromRemoteClientWithHash(info); + comicsWithNoLibrary.push_back(info); } } } + + DBHelper::updateFromRemoteClient(comics); + DBHelper::updateFromRemoteClientWithHash(comicsWithNoLibrary); } else { response.setStatus(412, "No comic info received"); response.write("", true);