Files
seasonedShows/seasoned_api/src/webserver/controllers/search/personSearch.js

41 lines
1.3 KiB
JavaScript

const configuration = require("../../../config/configuration").getInstance();
const TMDB = require("../../../tmdb/tmdb");
const SearchHistory = require("../../../searchHistory/searchHistory");
const tmdb = new TMDB(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 { query, page, adult } = req.query;
const username = req.loggedInUser ? req.loggedInUser.username : null;
const includeAdult = adult == "true" ? true : false;
if (username) {
searchHistory.create(username, query);
}
return tmdb
.personSearch(query, page, includeAdult)
.then(persons => res.send(persons))
.catch(error => {
const { status, message } = error;
if (status && message) {
res.status(status).send({ success: false, message });
} else {
// TODO log unhandled errors
console.log("caugth person search controller error", error);
res.status(500).send({
message: `An unexpected error occured while searching people with query: ${query}`
});
}
});
}
module.exports = personSearchController;