Added user history saving and that tmdb now gets to use cache.

This commit is contained in:
2017-10-22 17:01:26 +02:00
parent d90197b8b4
commit 6574c087bc
2 changed files with 42 additions and 33 deletions

View File

@@ -19,7 +19,7 @@ const nodemailer = require('nodemailer');
class RequestRepository {
constructor(database) {
constructor(cache, database) {
this.database = database || establishedDatabase;
this.queries = {
'insertRequest': "INSERT INTO requests VALUES (?, ?, ?, ?, ?, ?, CURRENT_DATE, 'requested', ?, ?)",
@@ -28,13 +28,12 @@ class RequestRepository {
}
}
searchRequest(query, page, type) {
// TODO get from cache
searchRequest(text, page, type) {
// STRIP METADATA THAT IS NOT ALLOWED
// Do a search in the tmdb api and return the results of the object
let getTmdbResults = function() {
return tmdb.search(query, page, type)
return tmdb.search(text, page, type)
.then((tmdbSearch) => {
return tmdbSearch.results;
})
@@ -54,35 +53,34 @@ class RequestRepository {
}
return Promise.resolve()
.then(() => plexRepository.searchMedia(query)
// Get the list of plexItems matching the query passed.
.then((plexItem) => {
let tmdbSearchResult = getTmdbResults();
.catch(() => plexRepository.searchMedia(text))
// Get the list of plexItems matching the query passed.
.then((plexItem) => {
let tmdbSearchResult = getTmdbResults();
// When we get the result from tmdbSearchResult we pass it along and iterate over each
// element, and updates the matchedInPlex status of a item.
return tmdbSearchResult.then((tmdbResult) => {
for (var i = 0; i < tmdbResult.length; i++) {
let foundMatchInPlex = checkIfMatchesPlexObjects(tmdbResult[i].title, tmdbResult[i].year, plexItem);
tmdbResult[i].matchedInPlex = foundMatchInPlex;
}
return { 'results': tmdbResult, 'page': 1 };
})
// TODO log error
.catch((error) => {
console.log(error);
throw new Error('Search query did not give any results.');
})
// When we get the result from tmdbSearchResult we pass it along and iterate over each
// element, and updates the matchedInPlex status of a item.
return tmdbSearchResult.then((tmdbResult) => {
for (var i = 0; i < tmdbResult.length; i++) {
let foundMatchInPlex = checkIfMatchesPlexObjects(tmdbResult[i].title, tmdbResult[i].year, plexItem);
tmdbResult[i].matchedInPlex = foundMatchInPlex;
}
return { 'results': tmdbResult, 'page': 1 };
})
.catch(() => {
let tmdbSearchResult = getTmdbResults();
// TODO log error
.catch((error) => {
console.log(error);
throw new Error('Search query did not give any results.');
})
})
.catch(() => {
let tmdbSearchResult = getTmdbResults();
// Catch if empty, then 404
return tmdbSearchResult.then((tmdbResult) => {
return {'results': tmdbResult, 'page': 1 };
})
// Catch if empty, then 404
return tmdbSearchResult.then((tmdbResult) => {
return {'results': tmdbResult, 'page': 1 };
})
)
})
}
lookup(identifier, type = 'movie') {