Skip to content

Commit

Permalink
Mapped results in tmdb now returns the complete json object so not ne…
Browse files Browse the repository at this point in the history
…eded to be created before sent. When getting all requested movies and shows it is now possible to only get one page at a time.
  • Loading branch information
KevinMidboe committed Mar 20, 2018
1 parent 42b8b5e commit 18359f4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
12 changes: 6 additions & 6 deletions seasoned_api/src/plex/requestRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?'
Expand Down Expand Up @@ -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);
})
}

Expand Down
15 changes: 5 additions & 10 deletions seasoned_api/src/tmdb/tmdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}));
}

/**
Expand All @@ -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,
}));
}

/**
Expand All @@ -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); });
}
Expand Down
4 changes: 2 additions & 2 deletions seasoned_api/src/webserver/controllers/plex/fetchRequested.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
})
Expand Down

0 comments on commit 18359f4

Please sign in to comment.