Improved testing, more linting and added paging to request/all endpoint #95
@@ -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);
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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); });
|
||||
}
|
||||
|
||||
@@ -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 });
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user