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

View File

@@ -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 = 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) { function searchRequestController(req, res) {
const user = req.headers.loggedinuser;
const { query, page, type } = req.query; const { query, page, type } = req.query;
console.log('searchReq: ' + query, page, type); 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) => { .then((searchResult) => {
// Verify that respond has content, if so send the content back
if (searchResult.results.length > 0) { if (searchResult.results.length > 0) {
res.send(searchResult); res.send(searchResult);
} }
// If no content was found, send 404 status and error message
else { else {
res.status(404).send({success: false, error: 'Search query did not return any results.'}) res.status(404).send({success: false, error: 'Search query did not return any results.'})
} }