From 18359f442c7dd350c8c3280d866581ef92f4cd09 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 20 Mar 2018 21:17:41 +0100 Subject: [PATCH] Mapped results in tmdb now returns the complete json object so not needed to be created before sent. When getting all requested movies and shows it is now possible to only get one page at a time. --- seasoned_api/src/plex/requestRepository.js | 12 ++++++------ seasoned_api/src/tmdb/tmdb.js | 15 +++++---------- .../webserver/controllers/plex/fetchRequested.js | 4 ++-- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/seasoned_api/src/plex/requestRepository.js b/seasoned_api/src/plex/requestRepository.js index fefa027..db37207 100644 --- a/seasoned_api/src/plex/requestRepository.js +++ b/seasoned_api/src/plex/requestRepository.js @@ -18,8 +18,8 @@ class RequestRepository { this.queries = { insertRequest: `INSERT INTO requests(id,title,year,poster_path,background_path,requested_by,ip,user_agent,type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, - fetchRequestedItems: 'SELECT * FROM requests ORDER BY date DESC', - fetchRequestedItemsByStatus: 'SELECT * FROM requests WHERE status IS ? AND type LIKE ?', + fetchRequestedItems: 'SELECT * FROM requests ORDER BY date DESC LIMIT 25 OFFSET ?*25-25', + fetchRequestedItemsByStatus: 'SELECT * FROM requests WHERE status IS ? AND type LIKE ? DESC LIMIT 25 OFFSET ?*25-25', updateRequestedById: 'UPDATE requests SET status = ? WHERE id is ? AND type is ?', checkIfIdRequested: 'SELECT * FROM requests WHERE id IS ? AND type IS ?', userRequests: 'SELECT * FROM requests WHERE requested_by IS ?' @@ -68,19 +68,19 @@ class RequestRepository { return Promise.resolve() .then(() => tmdb.lookup(identifier, type)) .then((movie) => { - const username = user == undefined ? undefined : user.username; + const username = user === undefined ? undefined : user.username; // Add request to database return this.database.run(this.queries.insertRequest, [movie.id, movie.title, movie.year, movie.poster_path, movie.background_path, username, ip, user_agent, movie.type]); }); } - fetchRequested(status, type = '%') { + fetchRequested(status, page = '1', type = '%') { return Promise.resolve() .then(() => { if (status === 'requested' || status === 'downloading' || status === 'downloaded') - return this.database.all(this.queries.fetchRequestedItemsByStatus, [status, type]); + return this.database.all(this.queries.fetchRequestedItemsByStatus, [status, type, page]); else - return this.database.all(this.queries.fetchRequestedItems); + return this.database.all(this.queries.fetchRequestedItems, page); }) } diff --git a/seasoned_api/src/tmdb/tmdb.js b/seasoned_api/src/tmdb/tmdb.js index 4401b49..154f3f7 100644 --- a/seasoned_api/src/tmdb/tmdb.js +++ b/seasoned_api/src/tmdb/tmdb.js @@ -66,10 +66,6 @@ class TMDB { .catch(() => { throw new Error('Could not search for movies/shows at tmdb.'); }) .then(response => this.cache.set(cacheKey, response)) .then(response => this.mapResults(response)) - .catch((error) => { throw new Error(error); }) - .then(([mappedResults, pagenumber, totalpages, total_results]) => ({ - results: mappedResults, page: pagenumber, total_results, total_pages: totalpages, - })); } /** @@ -80,17 +76,15 @@ class TMDB { * @returns {Promise} dict with query results, current page and total_pages */ listSearch(listName, type = 'movie', page = '1') { + const query = { page: page } + console.log(query) const cacheKey = `${this.cacheTags[listName]}:${type}:${page}`; return Promise.resolve() .then(() => this.cache.get(cacheKey)) - .catch(() => this.tmdb(TMDB_METHODS[listName][type], page)) + .catch(() => this.tmdb(TMDB_METHODS[listName][type], query)) .catch(() => { throw new Error('Error fetching list from tmdb.')}) .then(response => this.cache.set(cacheKey, response)) .then(response => this.mapResults(response, type)) - .catch((error) => { throw new Error(error); }) - .then(([mappedResults, pagenumber, totalpages, total_results]) => ({ - results: mappedResults, page: pagenumber, total_pages: totalpages, total_results, - })); } /** @@ -100,12 +94,13 @@ class TMDB { * @returns {Promise} dict with tmdb results, mapped as movie/show objects. */ mapResults(response, type) { + console.log(response.page) return Promise.resolve() .then(() => { const mappedResults = response.results.filter((element) => { return (element.media_type === 'movie' || element.media_type === 'tv' || element.media_type === undefined); }).map((element) => convertTmdbToSeasoned(element, type)); - return [mappedResults, response.page, response.total_pages, response.total_results]; + return {results: mappedResults, page: response.page, total_pages: response.total_pages, total_results: response.total_results} }) .catch((error) => { throw new Error(error); }); } diff --git a/seasoned_api/src/webserver/controllers/plex/fetchRequested.js b/seasoned_api/src/webserver/controllers/plex/fetchRequested.js index f216396..cd0f842 100644 --- a/seasoned_api/src/webserver/controllers/plex/fetchRequested.js +++ b/seasoned_api/src/webserver/controllers/plex/fetchRequested.js @@ -10,9 +10,9 @@ const requestRepository = new RequestRepository(); */ function fetchRequestedController(req, res) { // const user = req.loggedInUser; - const { status } = req.query; + const { status, page } = req.query; - requestRepository.fetchRequested(status) + requestRepository.fetchRequested(status, page) .then((requestedItems) => { res.send({ success: true, results: requestedItems, total_results: requestedItems.length }); })