2 Commits

Author SHA1 Message Date
0581813ee3 Merge pull request #92 from KevinMidboe/api_filterBug
Fixes api not filtering requests/all query when filtering
2018-03-20 13:17:17 +01:00
edf1de223e When filtering the request on status the sql query for the db did not sort DESC. 2018-03-20 13:11:26 +01:00

View File

@@ -8,21 +8,17 @@ const plexRepository = new PlexRepository();
const cache = new Cache(); const cache = new Cache();
const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey')); const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey'));
const MailTemplate = require('src/plex/mailTemplate');
const nodemailer = require('nodemailer');
class RequestRepository { class RequestRepository {
constructor(cache, database) { constructor(database) {
this.database = database || establishedDatabase; this.database = database || establishedDatabase;
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',
fetchRequestedItemsByStatus: 'SELECT * FROM requests WHERE status IS ? AND type LIKE ?', fetchRequestedItemsByStatus: 'SELECT * FROM requests WHERE status IS ? AND type LIKE ? ORDER BY date DESC',
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 ?',
}; };
this.cacheTags = { this.cacheTags = {
search: 'se', search: 'se',
@@ -51,10 +47,7 @@ class RequestRepository {
.then(() => this.database.get(this.queries.checkIfIdRequested, [tmdbMovie.id, tmdbMovie.type])) .then(() => this.database.get(this.queries.checkIfIdRequested, [tmdbMovie.id, tmdbMovie.type]))
.then((result, error) => { .then((result, error) => {
if (error) { throw new Error(error); } if (error) { throw new Error(error); }
let already_requested = false; tmdbMovie.requested = result ? true : false;
if (result) { already_requested = true; }
tmdbMovie.requested = already_requested;
return tmdbMovie; return tmdbMovie;
}); });
} }
@@ -68,7 +61,7 @@ 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]);
}); });
@@ -77,21 +70,23 @@ class RequestRepository {
fetchRequested(status, type = '%') { fetchRequested(status, 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]);
else }
return this.database.all(this.queries.fetchRequestedItems); return this.database.all(this.queries.fetchRequestedItems);
}) });
} }
userRequests(user) { userRequests(user) {
return Promise.resolve() return Promise.resolve()
.then(() => this.database.all(this.queries.userRequests, user.username)) .then(() => this.database.all(this.queries.userRequests, user.username))
.catch((error) => { .catch((error) => {
if (String(error).includes('no such column')) { throw new Error('Username not found'); } if (String(error).includes('no such column')) {
else { throw new Error('Unable to fetch your requests')} throw new Error('Username not found');
}
throw new Error('Unable to fetch your requests');
}) })
.then((result) => { return result }) .then((result) => { return result; });
} }
updateRequestedById(id, type, status) { updateRequestedById(id, type, status) {