From 6574c087bc35263490ac3dd15ba8c5c3310b5648 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Sun, 22 Oct 2017 17:01:26 +0200 Subject: [PATCH] Added user history saving and that tmdb now gets to use cache. --- seasoned_api/src/plex/requestRepository.js | 56 +++++++++---------- .../controllers/plex/searchRequest.js | 19 +++++-- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/seasoned_api/src/plex/requestRepository.js b/seasoned_api/src/plex/requestRepository.js index ef8b020..5ec3379 100644 --- a/seasoned_api/src/plex/requestRepository.js +++ b/seasoned_api/src/plex/requestRepository.js @@ -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') { diff --git a/seasoned_api/src/webserver/controllers/plex/searchRequest.js b/seasoned_api/src/webserver/controllers/plex/searchRequest.js index 4bf8a3c..44bcd74 100644 --- a/seasoned_api/src/webserver/controllers/plex/searchRequest.js +++ b/seasoned_api/src/webserver/controllers/plex/searchRequest.js @@ -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.'}) }