From 5036f4ca363f1d1701be67c1115e914919ce8f63 Mon Sep 17 00:00:00 2001 From: Kevin Midboe Date: Sun, 6 Mar 2022 11:57:45 +0100 Subject: [PATCH] Read and pass adult search query param consistently for movie, show & person info --- seasoned_api/src/tmdb/tmdb.js | 31 +++++++++---------- .../controllers/search/movieSearch.js | 9 +++--- .../controllers/search/personSearch.js | 13 ++++---- .../controllers/search/showSearch.js | 17 +++++----- 4 files changed, 34 insertions(+), 36 deletions(-) diff --git a/seasoned_api/src/tmdb/tmdb.js b/seasoned_api/src/tmdb/tmdb.js index c6fe8f1..0b5293b 100644 --- a/seasoned_api/src/tmdb/tmdb.js +++ b/seasoned_api/src/tmdb/tmdb.js @@ -176,13 +176,13 @@ class TMDB { * @param {Number} page representing pagination of results * @returns {Promise} dict with query results, current page and total_pages */ - movieSearch(query, page=1, adult=true) { - const tmdbquery = { query: query, page: page, adult: adult }; - const cacheKey = `tmdb/${this.cacheTags.movieSearch}:${page}:${query}:${adult}`; + movieSearch(search_query, page = 1, include_adult = true) { + const tmdbquery = { query: search_query, page, include_adult }; + const cacheKey = `tmdb/${this.cacheTags.movieSearch}:${page}:${search_query}:${include_adult}`; - return this.getFromCacheOrFetchFromTmdb(cacheKey, 'searchMovie', query) + return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchMovie", tmdbquery) .then(response => this.cache.set(cacheKey, response, this.defaultTTL)) - .then(response => this.mapResults(response, 'movie')) + .then(response => this.mapResults(response, "movie")); } /** @@ -191,13 +191,13 @@ class TMDB { * @param {Number} page representing pagination of results * @returns {Promise} dict with query results, current page and total_pages */ - showSearch(query, page=1) { - const tmdbquery = { query: query, page: page }; - const cacheKey = `tmdb/${this.cacheTags.showSearch}:${page}:${query}`; + showSearch(search_query, page = 1, include_adult = true) { + const tmdbquery = { query: search_query, page, include_adult }; + const cacheKey = `tmdb/${this.cacheTags.showSearch}:${page}:${search_query}:${include_adult}`; - return this.getFromCacheOrFetchFromTmdb(cacheKey, 'searchTv', query) + return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchTv", tmdbquery) .then(response => this.cache.set(cacheKey, response, this.defaultTTL)) - .then(response => this.mapResults(response, 'show')) + .then(response => this.mapResults(response, "show")); } /** @@ -206,14 +206,13 @@ class TMDB { * @param {Number} page representing pagination of results * @returns {Promise} dict with query results, current page and total_pages */ - personSearch(query, page=1) { + personSearch(search_query, page = 1, include_adult = true) { + const tmdbquery = { query: search_query, page, include_adult }; + const cacheKey = `tmdb/${this.cacheTags.personSearch}:${page}:${search_query}:${include_adult}`; - const tmdbquery = { query: query, page: page, include_adult: true }; - const cacheKey = `tmdb/${this.cacheTags.personSearch}:${page}:${query}:${include_adult}`; - - return this.getFromCacheOrFetchFromTmdb(cacheKey, 'searchPerson', query) + return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchPerson", tmdbquery) .then(response => this.cache.set(cacheKey, response, this.defaultTTL)) - .then(response => this.mapResults(response, 'person')) + .then(response => this.mapResults(response, "person")); } movieList(listname, page = 1) { diff --git a/seasoned_api/src/webserver/controllers/search/movieSearch.js b/seasoned_api/src/webserver/controllers/search/movieSearch.js index 17910cd..8cad277 100644 --- a/seasoned_api/src/webserver/controllers/search/movieSearch.js +++ b/seasoned_api/src/webserver/controllers/search/movieSearch.js @@ -11,15 +11,16 @@ const searchHistory = new SearchHistory(); * @returns {Callback} */ function movieSearchController(req, res) { - const { query, page } = req.query; + const { query, page, adult } = req.query; const username = req.loggedInUser ? req.loggedInUser.username : null; + const includeAdult = adult == "true" ? true : false; if (username) { - return searchHistory.create(username, query); + searchHistory.create(username, query); } - tmdb - .movieSearch(query, page) + return tmdb + .movieSearch(query, page, includeAdult) .then(movieSearchResults => res.send(movieSearchResults)) .catch(error => { const { status, message } = error; diff --git a/seasoned_api/src/webserver/controllers/search/personSearch.js b/seasoned_api/src/webserver/controllers/search/personSearch.js index 6886498..45d5cfc 100644 --- a/seasoned_api/src/webserver/controllers/search/personSearch.js +++ b/seasoned_api/src/webserver/controllers/search/personSearch.js @@ -11,18 +11,17 @@ const searchHistory = new SearchHistory(); * @returns {Callback} */ function personSearchController(req, res) { - const { query, page } = req.query; + const { query, page, adult } = req.query; const username = req.loggedInUser ? req.loggedInUser.username : null; + const includeAdult = adult == "true" ? true : false; if (username) { - return searchHistory.create(username, query); + searchHistory.create(username, query); } - tmdb - .personSearch(query, page) - .then(person => { - res.send(person); - }) + return tmdb + .personSearch(query, page, includeAdult) + .then(persons => res.send(persons)) .catch(error => { const { status, message } = error; diff --git a/seasoned_api/src/webserver/controllers/search/showSearch.js b/seasoned_api/src/webserver/controllers/search/showSearch.js index 4c5fef5..20b3c05 100644 --- a/seasoned_api/src/webserver/controllers/search/showSearch.js +++ b/seasoned_api/src/webserver/controllers/search/showSearch.js @@ -11,17 +11,16 @@ const searchHistory = new SearchHistory(); * @returns {Callback} */ function showSearchController(req, res) { - const { query, page } = req.query; + const { query, page, adult } = req.query; const username = req.loggedInUser ? req.loggedInUser.username : null; + const includeAdult = adult == "true" ? true : false; - Promise.resolve() - .then(() => { - if (username) { - return searchHistory.create(username, query); - } - return null; - }) - .then(() => tmdb.showSearch(query, page)) + if (username) { + searchHistory.create(username, query); + } + + return tmdb + .showSearch(query, page, includeAdult) .then(shows => { res.send(shows); })