diff --git a/seasoned_api/src/tmdb/tmdb.js b/seasoned_api/src/tmdb/tmdb.js index a5b7b4d..5efe0ed 100644 --- a/seasoned_api/src/tmdb/tmdb.js +++ b/seasoned_api/src/tmdb/tmdb.js @@ -68,6 +68,17 @@ class TMDB { .then(response => this.mapResults(response)); } + multiSearch(search_query, page=1) { + const query = { query: search_query, page: page }; + const cacheKey = `${this.cacheTags.multiSeach}:${page}:${search_query}`; + return Promise.resolve() + .then(() => this.cache.get(cacheKey)) + .catch(() => this.tmdb('searchMulti', query)) + .catch(() => { throw new Error('Could not complete search to tmdb'); }) + .then(response => this.cache.set(cacheKey, response)) + .then(response => this.mapResults(response)); + } + /** * Fetches a given list from tmdb. * @param {String} listName Name of list @@ -122,7 +133,7 @@ class TMDB { if (error) { return reject(error); } - return resolve(reponse); + resolve(reponse); }; if (!argument) { diff --git a/seasoned_api/src/webserver/app.js b/seasoned_api/src/webserver/app.js index 469b366..2d5c01e 100644 --- a/seasoned_api/src/webserver/app.js +++ b/seasoned_api/src/webserver/app.js @@ -67,6 +67,14 @@ router.get('/v1/seasoned/all', require('./controllers/seasoned/readStrays.js')); router.get('/v1/seasoned/:strayId', require('./controllers/seasoned/strayById.js')); router.post('/v1/seasoned/verify/:strayId', require('./controllers/seasoned/verifyStray.js')); +router.get('/v2/search/', require('./controllers/search/multiSearch.js')); +router.get('/v2/search/movie', require('./controllers/search/movieSearch.js')); +router.get('/v2/search/show', require('./controllers/search/showSearch.js')); +router.get('/v2/search/person', require('./controllers/search/personSearch.js')); + +router.get('/v2/movie/:id', require('./controllers/info/movieInfo.js')); +router.get('/v2/show/:id', require('./controllers/info/showInfo.js')); +router.get('/v2/person/:id', require('./controllers/info/personInfo.js')); /** * Plex */ diff --git a/seasoned_api/src/webserver/controllers/info/movieInfo.js b/seasoned_api/src/webserver/controllers/info/movieInfo.js new file mode 100644 index 0000000..5afa547 --- /dev/null +++ b/seasoned_api/src/webserver/controllers/info/movieInfo.js @@ -0,0 +1,24 @@ +const configuration = require('src/config/configuration').getInstance(); +const Cache = require('src/tmdb/cache'); +const TMDB = require('src/tmdb/tmdb'); +const cache = new Cache(); +const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey')); + +/** + * Controller: Retrieve information for a movie + * @param {Request} req http request variable + * @param {Response} res + * @returns {Callback} + */ + +function movieInfoController(req, res) { + const movieId = req.params.id; + tmdb.movieInfo(movieId) + .then((movie) => { + res.send(movie); + }).catch((error) => { + res.status(404).send({ success: false, error: error.message }); + }); +} + +module.exports = movieInfoController; diff --git a/seasoned_api/src/webserver/controllers/info/personInfo.js b/seasoned_api/src/webserver/controllers/info/personInfo.js new file mode 100644 index 0000000..1d19c9d --- /dev/null +++ b/seasoned_api/src/webserver/controllers/info/personInfo.js @@ -0,0 +1,24 @@ +const configuration = require('src/config/configuration').getInstance(); +const Cache = require('src/tmdb/cache'); +const TMDB = require('src/tmdb/tmdb'); +const cache = new Cache(); +const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey')); + +/** + * Controller: Retrieve information for a person + * @param {Request} req http request variable + * @param {Response} res + * @returns {Callback} + */ + +function personInfoController(req, res) { + const personId = req.params.id; + tmdb.personInfo(personId) + .then((person) => { + res.send(person); + }).catch((error) => { + res.status(404).send({ success: false, error: error.message }); + }); +} + +module.exports = personInfoController; diff --git a/seasoned_api/src/webserver/controllers/info/showInfo.js b/seasoned_api/src/webserver/controllers/info/showInfo.js new file mode 100644 index 0000000..5e1e33d --- /dev/null +++ b/seasoned_api/src/webserver/controllers/info/showInfo.js @@ -0,0 +1,24 @@ +const configuration = require('src/config/configuration').getInstance(); +const Cache = require('src/tmdb/cache'); +const TMDB = require('src/tmdb/tmdb'); +const cache = new Cache(); +const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey')); + +/** + * Controller: Retrieve information for a show + * @param {Request} req http request variable + * @param {Response} res + * @returns {Callback} + */ + +function showInfoController(req, res) { + const showId = req.params.id; + tmdb.showInfo(showId) + .then((show) => { + res.send(show); + }).catch((error) => { + res.status(404).send({ success: false, error: error.message }); + }); +} + +module.exports = showInfoController; diff --git a/seasoned_api/src/webserver/controllers/search/movieSearch.js b/seasoned_api/src/webserver/controllers/search/movieSearch.js new file mode 100644 index 0000000..906a132 --- /dev/null +++ b/seasoned_api/src/webserver/controllers/search/movieSearch.js @@ -0,0 +1,35 @@ +const SearchHistory = require('src/searchHistory/searchHistory'); +const configuration = require('src/config/configuration').getInstance(); +const Cache = require('src/tmdb/cache'); +const TMDB = require('src/tmdb/tmdb'); +const cache = new Cache(); +const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey')); +const searchHistory = new SearchHistory(); + +/** + * Controller: Search for movies by query and pagey + * @param {Request} req http request variable + * @param {Response} res + * @returns {Callback} + */ +function movieSearchController(req, res) { + const user = req.loggedInUser; + const { query, page } = req.query; + + Promise.resolve() + .then(() => { + if (user) { + return searchHistory.create(user, query); + } + return null + }) + .then(() => tmdb.movieSearch(query, page)) + .then((movies) => { + res.send(movies); + }) + .catch((error) => { + res.status(500).send({ success: false, error: error.message }); + }); +} + +module.exports = movieSearchController; diff --git a/seasoned_api/src/webserver/controllers/search/multiSearch.js b/seasoned_api/src/webserver/controllers/search/multiSearch.js new file mode 100644 index 0000000..bf90753 --- /dev/null +++ b/seasoned_api/src/webserver/controllers/search/multiSearch.js @@ -0,0 +1,35 @@ +const SearchHistory = require('src/searchHistory/searchHistory'); +const configuration = require('src/config/configuration').getInstance(); +const Cache = require('src/tmdb/cache'); +const TMDB = require('src/tmdb/tmdb'); +const cache = new Cache(); +const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey')); +const searchHistory = new SearchHistory(); + +/** + * Controller: Search for multi (movies, shows and people by query and pagey + * @param {Request} req http request variable + * @param {Response} res + * @returns {Callback} + */ +function multiSearchController(req, res) { + const user = req.loggedInUser; + const { query, page } = req.query; + + Promise.resolve() + .then(() => { + if (user) { + return searchHistory.create(user, query); + } + return null + }) + .then(() => tmdb.multiSearch(query, page)) + .then((result) => { + res.send(result); + }) + .catch((error) => { + res.status(500).send({ success: false, error: error.message }); + }); +} + +module.exports = multiSearchController; diff --git a/seasoned_api/src/webserver/controllers/search/personSearch.js b/seasoned_api/src/webserver/controllers/search/personSearch.js new file mode 100644 index 0000000..34bab20 --- /dev/null +++ b/seasoned_api/src/webserver/controllers/search/personSearch.js @@ -0,0 +1,35 @@ +const SearchHistory = require('src/searchHistory/searchHistory'); +const configuration = require('src/config/configuration').getInstance(); +const Cache = require('src/tmdb/cache'); +const TMDB = require('src/tmdb/tmdb'); +const cache = new Cache(); +const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey')); +const searchHistory = new SearchHistory(); + +/** + * Controller: Search for person by query and pagey + * @param {Request} req http request variable + * @param {Response} res + * @returns {Callback} + */ +function personSearchController(req, res) { + const user = req.loggedInUser; + const { query, page } = req.query; + + Promise.resolve() + .then(() => { + if (user) { + return searchHistory.create(user, query); + } + return null + }) + .then(() => tmdb.personSearch(query, page)) + .then((person) => { + res.send(person); + }) + .catch((error) => { + res.status(500).send({ success: false, error: error.message }); + }); +} + +module.exports = personSearchController; diff --git a/seasoned_api/src/webserver/controllers/search/showSearch.js b/seasoned_api/src/webserver/controllers/search/showSearch.js new file mode 100644 index 0000000..f982116 --- /dev/null +++ b/seasoned_api/src/webserver/controllers/search/showSearch.js @@ -0,0 +1,35 @@ +const SearchHistory = require('src/searchHistory/searchHistory'); +const configuration = require('src/config/configuration').getInstance(); +const Cache = require('src/tmdb/cache'); +const TMDB = require('src/tmdb/tmdb'); +const cache = new Cache(); +const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey')); +const searchHistory = new SearchHistory(); + +/** + * Controller: Search for shows by query and pagey + * @param {Request} req http request variable + * @param {Response} res + * @returns {Callback} + */ +function showSearchController(req, res) { + const user = req.loggedInUser; + const { query, page } = req.query; + + Promise.resolve() + .then(() => { + if (user) { + return searchHistory.create(user, query); + } + return null + }) + .then(() => tmdb.showSearch(query, page)) + .then((shows) => { + res.send(shows); + }) + .catch((error) => { + res.status(500).send({ success: false, error: error.message }); + }); +} + +module.exports = showSearchController;