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.
This commit is contained in:
@@ -18,8 +18,8 @@ class RequestRepository {
|
|||||||
this.queries = {
|
this.queries = {
|
||||||
insertRequest: `INSERT INTO requests(id,title,year,poster_path,background_path,requested_by,ip,user_agent,type)
|
insertRequest: `INSERT INTO requests(id,title,year,poster_path,background_path,requested_by,ip,user_agent,type)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||||
fetchRequestedItems: 'SELECT * FROM requests ORDER BY date DESC',
|
fetchRequestedItems: 'SELECT * FROM requests ORDER BY date DESC LIMIT 25 OFFSET ?*25-25',
|
||||||
fetchRequestedItemsByStatus: 'SELECT * FROM requests WHERE status IS ? AND type LIKE ?',
|
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 ?',
|
updateRequestedById: 'UPDATE requests SET status = ? WHERE id is ? AND type is ?',
|
||||||
checkIfIdRequested: 'SELECT * FROM requests WHERE id IS ? AND type IS ?',
|
checkIfIdRequested: 'SELECT * FROM requests WHERE id IS ? AND type IS ?',
|
||||||
userRequests: 'SELECT * FROM requests WHERE requested_by IS ?'
|
userRequests: 'SELECT * FROM requests WHERE requested_by IS ?'
|
||||||
@@ -68,19 +68,19 @@ class RequestRepository {
|
|||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => tmdb.lookup(identifier, type))
|
.then(() => tmdb.lookup(identifier, type))
|
||||||
.then((movie) => {
|
.then((movie) => {
|
||||||
const username = user == undefined ? undefined : user.username;
|
const username = user === undefined ? undefined : user.username;
|
||||||
// Add request to database
|
// 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]);
|
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()
|
return Promise.resolve()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
if (status === 'requested' || status === 'downloading' || status === 'downloaded')
|
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
|
else
|
||||||
return this.database.all(this.queries.fetchRequestedItems);
|
return this.database.all(this.queries.fetchRequestedItems, page);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,10 +66,6 @@ class TMDB {
|
|||||||
.catch(() => { throw new Error('Could not search for movies/shows at tmdb.'); })
|
.catch(() => { throw new Error('Could not search for movies/shows at tmdb.'); })
|
||||||
.then(response => this.cache.set(cacheKey, response))
|
.then(response => this.cache.set(cacheKey, response))
|
||||||
.then(response => this.mapResults(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
|
* @returns {Promise} dict with query results, current page and total_pages
|
||||||
*/
|
*/
|
||||||
listSearch(listName, type = 'movie', page = '1') {
|
listSearch(listName, type = 'movie', page = '1') {
|
||||||
|
const query = { page: page }
|
||||||
|
console.log(query)
|
||||||
const cacheKey = `${this.cacheTags[listName]}:${type}:${page}`;
|
const cacheKey = `${this.cacheTags[listName]}:${type}:${page}`;
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => this.cache.get(cacheKey))
|
.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.')})
|
.catch(() => { throw new Error('Error fetching list from tmdb.')})
|
||||||
.then(response => this.cache.set(cacheKey, response))
|
.then(response => this.cache.set(cacheKey, response))
|
||||||
.then(response => this.mapResults(response, type))
|
.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.
|
* @returns {Promise} dict with tmdb results, mapped as movie/show objects.
|
||||||
*/
|
*/
|
||||||
mapResults(response, type) {
|
mapResults(response, type) {
|
||||||
|
console.log(response.page)
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const mappedResults = response.results.filter((element) => {
|
const mappedResults = response.results.filter((element) => {
|
||||||
return (element.media_type === 'movie' || element.media_type === 'tv' || element.media_type === undefined);
|
return (element.media_type === 'movie' || element.media_type === 'tv' || element.media_type === undefined);
|
||||||
}).map((element) => convertTmdbToSeasoned(element, type));
|
}).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); });
|
.catch((error) => { throw new Error(error); });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ const requestRepository = new RequestRepository();
|
|||||||
*/
|
*/
|
||||||
function fetchRequestedController(req, res) {
|
function fetchRequestedController(req, res) {
|
||||||
// const user = req.loggedInUser;
|
// const user = req.loggedInUser;
|
||||||
const { status } = req.query;
|
const { status, page } = req.query;
|
||||||
|
|
||||||
requestRepository.fetchRequested(status)
|
requestRepository.fetchRequested(status, page)
|
||||||
.then((requestedItems) => {
|
.then((requestedItems) => {
|
||||||
res.send({ success: true, results: requestedItems, total_results: requestedItems.length });
|
res.send({ success: true, results: requestedItems, total_results: requestedItems.length });
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user