From: APTX Date: Sat, 16 Aug 2014 15:42:13 +0000 (+0200) Subject: Add nextEpisode and previousEpisode. X-Git-Url: https://gitweb.aptx.org/?a=commitdiff_plain;h=93f9d8486e5706b4916f2476b649ce61e7e638ee;p=localmylist.git Add nextEpisode and previousEpisode. The intended use is for players requesting a file for the next/previous episode in a series. --- diff --git a/localmylist/database.cpp b/localmylist/database.cpp index 83eb130..03f99dd 100644 --- a/localmylist/database.cpp +++ b/localmylist/database.cpp @@ -463,6 +463,54 @@ OpenFileData Database::openEpisode(int aid, int epno, const QString &type) return readOpenFileData(q); } +OpenFileData Database::nextEpisode(int fid) +{ + QSqlQuery &q = prepare(R"( + SELECT f.fid, a.title_romaji, e.title_english, e.epno, fl.path, fl.host_id, + CASE WHEN split_part(f.resolution, 'x', 1) = '' OR split_part(f.resolution, 'x', 2) = '' THEN 0 ELSE split_part(f.resolution, 'x', 1)::int * split_part(f.resolution, 'x', 2)::int END pixels + FROM file f + JOIN episode e ON f.eid = e.eid + JOIN anime a ON a.aid = f.aid + JOIN file_location fl ON fl.fid = f.fid + WHERE f.fid = (SELECT f2.fid + FROM file f + JOIN episode e ON f.eid = e.eid + JOIN episode e2 ON e2.aid = e.aid AND ((e2.epno > e.epno AND e2.type = e.type) OR e2.type > e.type) -- No type ordering here (but it works as the enum is ordered) + JOIN episode_type et ON et.type = e2.type + JOIN file f2 ON f2.eid = e2.eid AND f2.fid <> f.fid + WHERE f.fid = :fid + ORDER BY et.ordering DESC, e2.epno ASC -- et.ordering should be DESC: 99 -> S1 (not O1) + LIMIT 1) + )"); + q.bindValue(":fid", fid); + + return readOpenFileData(q); +} + +OpenFileData Database::previousEpisode(int fid) +{ + QSqlQuery &q = prepare(R"( + SELECT f.fid, a.title_romaji, e.title_english, e.epno, fl.path, fl.host_id, + CASE WHEN split_part(f.resolution, 'x', 1) = '' OR split_part(f.resolution, 'x', 2) = '' THEN 0 ELSE split_part(f.resolution, 'x', 1)::int * split_part(f.resolution, 'x', 2)::int END pixels + FROM file f + JOIN episode e ON f.eid = e.eid + JOIN anime a ON a.aid = f.aid + JOIN file_location fl ON fl.fid = f.fid + WHERE f.fid = (SELECT f2.fid + FROM file f + JOIN episode e ON f.eid = e.eid + JOIN episode e2 ON e2.aid = e.aid AND ((e2.epno < e.epno AND e2.type = e.type) OR e2.type < e.type) -- No type ordering here (but it works as the enum is ordered) + JOIN episode_type et ON et.type = e2.type + JOIN file f2 ON f2.eid = e2.eid AND f2.fid <> f.fid + WHERE f.fid = :fid + ORDER BY et.ordering ASC, e2.epno DESC -- et.ordering should be ASC: C1 -> S99 (not 99) + LIMIT 1) + )"); + q.bindValue(":fid", fid); + + return readOpenFileData(q); +} + HostInfo Database::getHostInfo(const QString &hostName) { QSqlQuery &q = prepare( diff --git a/localmylist/database.h b/localmylist/database.h index 902ee37..de7e97a 100644 --- a/localmylist/database.h +++ b/localmylist/database.h @@ -72,6 +72,20 @@ public slots: LocalMyList::OpenFileData openFile(int fid); LocalMyList::OpenFileData openEpisode(int aid, int epno, const QString &type = ""); + /** + * @brief nextEpisode return the next available episode + * @param fid the id for which the next episode is to be found + * @return OpenFileData with fid != 0 if a next episode is found + */ + LocalMyList::OpenFileData nextEpisode(int fid); + + /** + * @brief previousEpisode return the prefioud available episode + * @param fid the id for which the previous episode is to be found + * @return OpenFileData with fid != 0 if a previous episode is found + */ + LocalMyList::OpenFileData previousEpisode(int fid); + LocalMyList::HostInfo getHostInfo(const QString &hostName); QVariantMap getConfig();