Added user history saving and that tmdb now gets to use cache.
This commit is contained in:
@@ -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') {
|
||||
|
||||
@@ -1,17 +1,28 @@
|
||||
const SearchHistory = require('src/searchHistory/searchHistory');
|
||||
const Cache = require('src/tmdb/cache');
|
||||
const RequestRepository = require('src/plex/requestRepository.js');
|
||||
const requestRepository = new RequestRepository();
|
||||
const cache = new Cache();
|
||||
const requestRepository = new RequestRepository(cache);
|
||||
const searchHistory = new SearchHistory();
|
||||
|
||||
|
||||
function searchRequestController(req, res) {
|
||||
const user = req.headers.loggedinuser;
|
||||
const { query, page, type } = req.query;
|
||||
console.log('searchReq: ' + query, page, type);
|
||||
|
||||
requestRepository.searchRequest(query, page, type)
|
||||
Promise.resolve()
|
||||
.then(() => {
|
||||
if (user) {
|
||||
return searchHistory.create(user, query);
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.then(() => requestRepository.searchRequest(query, page, type))
|
||||
.then((searchResult) => {
|
||||
// Verify that respond has content, if so send the content back
|
||||
if (searchResult.results.length > 0) {
|
||||
res.send(searchResult);
|
||||
}
|
||||
// If no content was found, send 404 status and error message
|
||||
else {
|
||||
res.status(404).send({success: false, error: 'Search query did not return any results.'})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user