Correctly use username from req.loggedInUser

This commit is contained in:
2022-01-03 19:17:27 +01:00
parent be889b8100
commit d8985aaff7
12 changed files with 335 additions and 272 deletions

View File

@@ -1,99 +1,130 @@
const PlexRepository = require('src/plex/plexRepository'); const PlexRepository = require("src/plex/plexRepository");
const configuration = require('src/config/configuration').getInstance(); const configuration = require("src/config/configuration").getInstance();
const TMDB = require('src/tmdb/tmdb'); const TMDB = require("src/tmdb/tmdb");
const establishedDatabase = require('src/database/database'); const establishedDatabase = require("src/database/database");
const plexRepository = new PlexRepository(configuration.get('plex', 'ip')); const plexRepository = new PlexRepository(configuration.get("plex", "ip"));
const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')); const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
class RequestRepository { class RequestRepository {
constructor(database) { constructor(database) {
this.database = database || establishedDatabase; this.database = database || establishedDatabase;
this.queries = { this.queries = {
insertRequest: `INSERT INTO requests(id,title,year,poster_path,background_path,requested_by,ip,user_agent,type) insertRequest: `INSERT INTO requests(id,title,year,poster_path,background_path,requested_by,ip,user_agent,type)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
fetchRequestedItems: 'SELECT * FROM requests ORDER BY date DESC LIMIT 25 OFFSET ?*25-25', fetchRequestedItems:
fetchRequestedItemsByStatus: 'SELECT * FROM requests WHERE status IS ? AND type LIKE ? ORDER BY date DESC LIMIT 25 OFFSET ?*25-25', "SELECT * FROM requests ORDER BY date DESC LIMIT 25 OFFSET ?*25-25",
updateRequestedById: 'UPDATE requests SET status = ? WHERE id is ? AND type is ?', fetchRequestedItemsByStatus:
checkIfIdRequested: 'SELECT * FROM requests WHERE id IS ? AND type IS ?', "SELECT * FROM requests WHERE status IS ? AND type LIKE ? ORDER BY date DESC LIMIT 25 OFFSET ?*25-25",
userRequests: 'SELECT * FROM requests WHERE requested_by IS ? ORDER BY date DESC', updateRequestedById:
}; "UPDATE requests SET status = ? WHERE id is ? AND type is ?",
this.cacheTags = { checkIfIdRequested: "SELECT * FROM requests WHERE id IS ? AND type IS ?",
search: 'se', userRequests:
lookup: 'i', "SELECT * FROM requests WHERE requested_by IS ? ORDER BY date DESC"
}; };
} this.cacheTags = {
search: "se",
lookup: "i"
};
}
search(query, type, page) { search(query, type, page) {
return Promise.resolve() return Promise.resolve()
.then(() => tmdb.search(query, type, page)) .then(() => tmdb.search(query, type, page))
.catch(error => Error(`error in the house${error}`)); .catch(error => Error(`error in the house${error}`));
} }
lookup(identifier, type = 'movie') { lookup(identifier, type = "movie") {
return Promise.resolve() return Promise.resolve()
.then(() => tmdb.lookup(identifier, type)) .then(() => tmdb.lookup(identifier, type))
.then(tmdbMovie => this.checkID(tmdbMovie)) .then(tmdbMovie => this.checkID(tmdbMovie))
.then(tmdbMovie => plexRepository.inPlex(tmdbMovie)) .then(tmdbMovie => plexRepository.inPlex(tmdbMovie))
.catch((error) => { .catch(error => {
throw new Error(error); throw new Error(error);
}); });
} }
checkID(tmdbMovie) { checkID(tmdbMovie) {
return Promise.resolve() return Promise.resolve()
.then(() => this.database.get(this.queries.checkIfIdRequested, [tmdbMovie.id, tmdbMovie.type])) .then(() =>
.then((result, error) => { this.database.get(this.queries.checkIfIdRequested, [
if (error) { throw new Error(error); } tmdbMovie.id,
tmdbMovie.requested = result ? true : false; tmdbMovie.type
return tmdbMovie; ])
}); )
} .then((result, error) => {
if (error) {
throw new Error(error);
}
tmdbMovie.requested = result ? true : false;
return tmdbMovie;
});
}
/** /**
* Send request for given media id. * Send request for given media id.
* @param {identifier, type} the id of the media object and type of media must be defined * @param {identifier, type} the id of the media object and type of media must be defined
* @returns {Promise} If nothing has gone wrong. * @returns {Promise} If nothing has gone wrong.
*/ */
sendRequest(identifier, type, ip, user_agent, user) { sendRequest(identifier, type, ip, user_agent, user) {
return Promise.resolve() return Promise.resolve()
.then(() => tmdb.lookup(identifier, type)) .then(() => tmdb.lookup(identifier, type))
.then((movie) => { .then(movie => {
const username = user === undefined ? undefined : user.username; const username = user === undefined ? undefined : user.username;
// Add request to database // Add request to database
return this.database.run(this.queries.insertRequest, [movie.id, movie.title, movie.year, movie.poster_path, movie.background_path, username, ip, user_agent, movie.type]); return this.database.run(this.queries.insertRequest, [
movie.id,
movie.title,
movie.year,
movie.poster_path,
movie.background_path,
username,
ip,
user_agent,
movie.type
]);
}); });
} }
fetchRequested(status, page = '1', type = '%') { fetchRequested(status, page = "1", type = "%") {
return Promise.resolve() return Promise.resolve().then(() => {
.then(() => { if (
if (status === 'requested' || status === 'downloading' || status === 'downloaded') status === "requested" ||
return this.database.all(this.queries.fetchRequestedItemsByStatus, [status, type, page]); status === "downloading" ||
else status === "downloaded"
return this.database.all(this.queries.fetchRequestedItems, page); )
}) return this.database.all(this.queries.fetchRequestedItemsByStatus, [
} status,
type,
page
]);
else return this.database.all(this.queries.fetchRequestedItems, page);
});
}
userRequests(user) { userRequests(username) {
return Promise.resolve() return Promise.resolve()
.then(() => this.database.all(this.queries.userRequests, user.username)) .then(() => this.database.all(this.queries.userRequests, username))
.catch((error) => { .catch(error => {
if (String(error).includes('no such column')) { if (String(error).includes("no such column")) {
throw new Error('Username not found'); throw new Error("Username not found");
} }
throw new Error('Unable to fetch your requests'); throw new Error("Unable to fetch your requests");
}) })
.then((result) => { .then(result => {
// TODO do a correct mapping before sending, not just a dump of the database // TODO do a correct mapping before sending, not just a dump of the database
result.map(item => item.poster = item.poster_path) result.map(item => (item.poster = item.poster_path));
return result return result;
}); });
} }
updateRequestedById(id, type, status) { updateRequestedById(id, type, status) {
return this.database.run(this.queries.updateRequestedById, [status, id, type]); return this.database.run(this.queries.updateRequestedById, [
} status,
id,
type
]);
}
} }
module.exports = RequestRepository; module.exports = RequestRepository;

View File

@@ -1,26 +1,24 @@
const SearchHistory = require('src/searchHistory/searchHistory'); const SearchHistory = require("src/searchHistory/searchHistory");
const Cache = require('src/tmdb/cache'); const Cache = require("src/tmdb/cache");
const RequestRepository = require('src/plex/requestRepository.js'); const RequestRepository = require("src/plex/requestRepository.js");
const cache = new Cache(); const cache = new Cache();
const requestRepository = new RequestRepository(cache); const requestRepository = new RequestRepository(cache);
const searchHistory = new SearchHistory(); const searchHistory = new SearchHistory();
function searchRequestController(req, res) { function searchRequestController(req, res) {
const user = req.loggedInUser; const { query, page, type } = req.query;
const { query, page, type } = req.query; const username = req.loggedInUser ? req.loggedInUser.username : null;
const username = user === undefined ? undefined : user.username;
Promise.resolve() Promise.resolve()
.then(() => searchHistory.create(username, query)) .then(() => searchHistory.create(username, query))
.then(() => requestRepository.search(query, page, type)) .then(() => requestRepository.search(query, page, type))
.then((searchResult) => { .then(searchResult => {
res.send(searchResult); res.send(searchResult);
}) })
.catch(error => { .catch(error => {
res.status(500).send({ success: false, message: error.message }); res.status(500).send({ success: false, message: error.message });
}); });
} }
module.exports = searchRequestController; module.exports = searchRequestController;

View File

@@ -1,16 +1,16 @@
const configuration = require('src/config/configuration').getInstance() const configuration = require("src/config/configuration").getInstance();
const RequestRepository = require('src/request/request'); const RequestRepository = require("src/request/request");
const TMDB = require('src/tmdb/tmdb') const TMDB = require("src/tmdb/tmdb");
const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')) const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
const request = new RequestRepository() const request = new RequestRepository();
const tmdbMovieInfo = (id) => { const tmdbMovieInfo = id => {
return tmdb.movieInfo(id) return tmdb.movieInfo(id);
} };
const tmdbShowInfo = (id) => { const tmdbShowInfo = id => {
return tmdb.showInfo(id) return tmdb.showInfo(id);
} };
/** /**
* Controller: POST a media id to be donwloaded * Controller: POST a media id to be donwloaded
@@ -21,28 +21,43 @@ const tmdbShowInfo = (id) => {
function submitRequestController(req, res) { function submitRequestController(req, res) {
// This is the id that is the param of the url // This is the id that is the param of the url
const id = req.params.mediaId; const id = req.params.mediaId;
const type = req.query.type ? req.query.type.toLowerCase() : undefined const type = req.query.type ? req.query.type.toLowerCase() : undefined;
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; const ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress;
const user_agent = req.headers['user-agent']; const user_agent = req.headers["user-agent"];
const user = req.loggedInUser; const username = req.loggedInUser ? req.loggedInUser.username : null;
let mediaFunction = undefined
if (type === 'movie') { let mediaFunction = undefined;
console.log('movie')
mediaFunction = tmdbMovieInfo if (type === "movie") {
} else if (type === 'show') { console.log("movie");
console.log('show') mediaFunction = tmdbMovieInfo;
mediaFunction = tmdbShowInfo } else if (type === "show") {
console.log("show");
mediaFunction = tmdbShowInfo;
} else { } else {
res.status(422).send({ success: false, message: 'Incorrect type. Allowed types: "movie" or "show"'}) res
.status(422)
.send({
success: false,
message: 'Incorrect type. Allowed types: "movie" or "show"'
});
} }
if (mediaFunction === undefined) { res.status(200); return } if (mediaFunction === undefined) {
res.status(200);
return;
}
mediaFunction(id) mediaFunction(id)
.then(tmdbMedia => request.requestFromTmdb(tmdbMedia, ip, user_agent, user)) .then(tmdbMedia =>
.then(() => res.send({ success: true, message: 'Media item successfully requested' })) request.requestFromTmdb(tmdbMedia, ip, user_agent, username)
.catch(err => res.status(500).send({ success: false, message: err.message })) )
.then(() =>
res.send({ success: true, message: "Media item successfully requested" })
)
.catch(err =>
res.status(500).send({ success: false, message: err.message })
);
} }
module.exports = submitRequestController; module.exports = submitRequestController;

View File

@@ -23,7 +23,7 @@ function requestTmdbIdController(req, res) {
const ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress; const ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress;
const user_agent = req.headers["user-agent"]; const user_agent = req.headers["user-agent"];
const user = req.loggedInUser; const username = req.loggedInUser ? req.loggedInUser.username : null;
let mediaFunction = undefined; let mediaFunction = undefined;
@@ -47,7 +47,9 @@ function requestTmdbIdController(req, res) {
mediaFunction(id) mediaFunction(id)
// .catch((error) => { console.error(error); res.status(404).send({ success: false, error: 'Id not found' }) }) // .catch((error) => { console.error(error); res.status(404).send({ success: false, error: 'Id not found' }) })
.then(tmdbMedia => request.requestFromTmdb(tmdbMedia, ip, user_agent, user)) .then(tmdbMedia =>
request.requestFromTmdb(tmdbMedia, ip, user_agent, username)
)
.then(() => .then(() =>
res.send({ success: true, message: "Request has been submitted." }) res.send({ success: true, message: "Request has been submitted." })
) )

View File

@@ -1,7 +1,7 @@
const configuration = require('src/config/configuration').getInstance(); const configuration = require("src/config/configuration").getInstance();
const TMDB = require('src/tmdb/tmdb'); const TMDB = require("src/tmdb/tmdb");
const SearchHistory = require('src/searchHistory/searchHistory'); const SearchHistory = require("src/searchHistory/searchHistory");
const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')); const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
const searchHistory = new SearchHistory(); const searchHistory = new SearchHistory();
/** /**
@@ -11,28 +11,29 @@ const searchHistory = new SearchHistory();
* @returns {Callback} * @returns {Callback}
*/ */
function movieSearchController(req, res) { function movieSearchController(req, res) {
const user = req.loggedInUser;
const { query, page } = req.query; const { query, page } = req.query;
const username = req.loggedInUser ? req.loggedInUser.username : null;
if (user) { if (username) {
return searchHistory.create(user, query); return searchHistory.create(username, query);
} }
tmdb.movieSearch(query, page) tmdb
.movieSearch(query, page)
.then(movieSearchResults => res.send(movieSearchResults)) .then(movieSearchResults => res.send(movieSearchResults))
.catch(error => { .catch(error => {
const { status, message } = error; const { status, message } = error;
if (status && message) { if (status && message) {
res.status(status).send({ success: false, message }) res.status(status).send({ success: false, message });
} else { } else {
// TODO log unhandled errors // TODO log unhandled errors
console.log('caugth movie search controller error', error) console.log("caugth movie search controller error", error);
res.status(500).send({ res.status(500).send({
message: `An unexpected error occured while searching movies with query: ${query}` message: `An unexpected error occured while searching movies with query: ${query}`
}) });
} }
}) });
} }
module.exports = movieSearchController; module.exports = movieSearchController;

View File

@@ -1,14 +1,14 @@
const configuration = require('src/config/configuration').getInstance(); const configuration = require("src/config/configuration").getInstance();
const TMDB = require('src/tmdb/tmdb'); const TMDB = require("src/tmdb/tmdb");
const SearchHistory = require('src/searchHistory/searchHistory'); const SearchHistory = require("src/searchHistory/searchHistory");
const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')); const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
const searchHistory = new SearchHistory(); const searchHistory = new SearchHistory();
function checkAndCreateJsonResponse(result) { function checkAndCreateJsonResponse(result) {
if (typeof result['createJsonResponse'] === 'function') { if (typeof result["createJsonResponse"] === "function") {
return result.createJsonResponse() return result.createJsonResponse();
} }
return result return result;
} }
/** /**
@@ -18,26 +18,31 @@ function checkAndCreateJsonResponse(result) {
* @returns {Callback} * @returns {Callback}
*/ */
function multiSearchController(req, res) { function multiSearchController(req, res) {
const user = req.loggedInUser;
const { query, page, adult } = req.query; const { query, page, adult } = req.query;
const username = req.loggedInUser ? req.loggedInUser.username : null;
if (user) { if (username) {
searchHistory.create(user.username, query) searchHistory.create(username, query);
} }
return tmdb.multiSearch(query, page, adult) return tmdb
.then(multiSearchResults => res.send(multiSearchResults)) .multiSearch(query, page, adult)
.catch(error => { .then(multiSearchResults => res.send(multiSearchResults))
const { status, message } = error; .catch(error => {
const { status, message } = error;
if (status && message) { if (status && message) {
res.status(status).send({ success: false, message }) res.status(status).send({ success: false, message });
} else { } else {
// TODO log unhandled errors // TODO log unhandled errors
console.log('caugth multi search controller error', error) console.log("caugth multi search controller error", error);
res.status(500).send({ message: `An unexpected error occured while searching with query: ${query}` }) res
} .status(500)
}) .send({
message: `An unexpected error occured while searching with query: ${query}`
});
}
});
} }
module.exports = multiSearchController; module.exports = multiSearchController;

View File

@@ -1,7 +1,7 @@
const configuration = require('src/config/configuration').getInstance(); const configuration = require("src/config/configuration").getInstance();
const TMDB = require('src/tmdb/tmdb'); const TMDB = require("src/tmdb/tmdb");
const SearchHistory = require('src/searchHistory/searchHistory'); const SearchHistory = require("src/searchHistory/searchHistory");
const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')); const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
const searchHistory = new SearchHistory(); const searchHistory = new SearchHistory();
/** /**
@@ -11,30 +11,31 @@ const searchHistory = new SearchHistory();
* @returns {Callback} * @returns {Callback}
*/ */
function personSearchController(req, res) { function personSearchController(req, res) {
const user = req.loggedInUser;
const { query, page } = req.query; const { query, page } = req.query;
const username = req.loggedInUser ? req.loggedInUser.username : null;
if (user) { if (username) {
return searchHistory.create(user, query); return searchHistory.create(username, query);
} }
tmdb.personSearch(query, page) tmdb
.then((person) => { .personSearch(query, page)
.then(person => {
res.send(person); res.send(person);
}) })
.catch(error => { .catch(error => {
const { status, message } = error; const { status, message } = error;
if (status && message) { if (status && message) {
res.status(status).send({ success: false, message }) res.status(status).send({ success: false, message });
} else { } else {
// TODO log unhandled errors // TODO log unhandled errors
console.log('caugth person search controller error', error) console.log("caugth person search controller error", error);
res.status(500).send({ res.status(500).send({
message: `An unexpected error occured while searching people with query: ${query}` message: `An unexpected error occured while searching people with query: ${query}`
}) });
} }
}) });
} }
module.exports = personSearchController; module.exports = personSearchController;

View File

@@ -1,7 +1,7 @@
const SearchHistory = require('src/searchHistory/searchHistory'); const SearchHistory = require("src/searchHistory/searchHistory");
const configuration = require('src/config/configuration').getInstance(); const configuration = require("src/config/configuration").getInstance();
const TMDB = require('src/tmdb/tmdb'); const TMDB = require("src/tmdb/tmdb");
const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')); const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
const searchHistory = new SearchHistory(); const searchHistory = new SearchHistory();
/** /**
@@ -11,23 +11,23 @@ const searchHistory = new SearchHistory();
* @returns {Callback} * @returns {Callback}
*/ */
function showSearchController(req, res) { function showSearchController(req, res) {
const user = req.loggedInUser;
const { query, page } = req.query; const { query, page } = req.query;
const username = req.loggedInUser ? req.loggedInUser.username : null;
Promise.resolve() Promise.resolve()
.then(() => { .then(() => {
if (user) { if (username) {
return searchHistory.create(user, query); return searchHistory.create(username, query);
} }
return null return null;
}) })
.then(() => tmdb.showSearch(query, page)) .then(() => tmdb.showSearch(query, page))
.then((shows) => { .then(shows => {
res.send(shows); res.send(shows);
}) })
.catch(error => { .catch(error => {
res.status(500).send({ success: false, message: error.message }); res.status(500).send({ success: false, message: error.message });
}); });
} }
module.exports = showSearchController; module.exports = showSearchController;

View File

@@ -1,24 +1,25 @@
const UserRepository = require('src/user/userRepository'); const UserRepository = require("src/user/userRepository");
const userRepository = new UserRepository(); const userRepository = new UserRepository();
const fetch = require('node-fetch'); const fetch = require("node-fetch");
const FormData = require('form-data'); const FormData = require("form-data");
function handleError(error, res) { function handleError(error, res) {
let { status, message, source } = error; let { status, message, source } = error;
if (status && message) { if (status && message) {
if (status === 401) { if (status === 401) {
message = 'Unauthorized. Please check plex credentials.', (message = "Unauthorized. Please check plex credentials."),
source = 'plex' (source = "plex");
} }
res.status(status).send({ success: false, message, source }) res.status(status).send({ success: false, message, source });
} else { } else {
console.log('caught authenticate plex account controller error', error) console.log("caught authenticate plex account controller error", error);
res.status(500).send({ res.status(500).send({
message: 'An unexpected error occured while authenticating your account with plex', message:
"An unexpected error occured while authenticating your account with plex",
source source
}) });
} }
} }
@@ -28,33 +29,32 @@ function handleResponse(response) {
success: false, success: false,
status: response.status, status: response.status,
message: response.statusText message: response.statusText
} };
} }
return response.json() return response.json();
} }
function plexAuthenticate(username, password) { function plexAuthenticate(username, password) {
const url = 'https://plex.tv/api/v2/users/signin' const url = "https://plex.tv/api/v2/users/signin";
const form = new FormData() const form = new FormData();
form.append('login', username) form.append("login", username);
form.append('password', password) form.append("password", password);
form.append('rememberMe', 'false') form.append("rememberMe", "false");
const headers = { const headers = {
'Accept': 'application/json, text/plain, */*', Accept: "application/json, text/plain, */*",
'Content-Type': form.getHeaders()['content-type'], "Content-Type": form.getHeaders()["content-type"],
'X-Plex-Client-Identifier': 'seasonedRequest' "X-Plex-Client-Identifier": "seasonedRequest"
} };
const options = { const options = {
method: 'POST', method: "POST",
headers, headers,
body: form body: form
} };
return fetch(url, options) return fetch(url, options).then(resp => handleResponse(resp));
.then(resp => handleResponse(resp))
} }
function link(req, res) { function link(req, res) {
@@ -63,22 +63,28 @@ function link(req, res) {
return plexAuthenticate(username, password) return plexAuthenticate(username, password)
.then(plexUser => userRepository.linkPlexUserId(user.username, plexUser.id)) .then(plexUser => userRepository.linkPlexUserId(user.username, plexUser.id))
.then(response => res.send({ .then(response =>
success: true, res.send({
message: "Successfully authenticated and linked plex account with seasoned request." success: true,
})) message:
.catch(error => handleError(error, res)) "Successfully authenticated and linked plex account with seasoned request."
})
)
.catch(error => handleError(error, res));
} }
function unlink(req, res) { function unlink(req, res) {
const user = req.loggedInUser; const username = req.loggedInUser ? req.loggedInUser.username : null;
return userRepository.unlinkPlexUserId(user.username) return userRepository
.then(response => res.send({ .unlinkPlexUserId(username)
success: true, .then(response =>
message: "Successfully unlinked plex account from seasoned request." res.send({
})) success: true,
.catch(error => handleError(error, res)) message: "Successfully unlinked plex account from seasoned request."
})
)
.catch(error => handleError(error, res));
} }
module.exports = { module.exports = {

View File

@@ -1,4 +1,4 @@
const RequestRepository = require('src/plex/requestRepository.js'); const RequestRepository = require("src/plex/requestRepository.js");
const requestRepository = new RequestRepository(); const requestRepository = new RequestRepository();
@@ -9,15 +9,20 @@ const requestRepository = new RequestRepository();
* @returns {Callback} * @returns {Callback}
*/ */
function requestsController(req, res) { function requestsController(req, res) {
const user = req.loggedInUser; const username = req.loggedInUser ? req.loggedInUser.username : null;
requestRepository.userRequests(user) requestRepository
.then(requests => { .userRequests(username)
res.send({ success: true, results: requests, total_results: requests.length }); .then(requests => {
}) res.send({
.catch(error => { success: true,
res.status(500).send({ success: false, message: error.message }); results: requests,
total_results: requests.length
}); });
})
.catch(error => {
res.status(500).send({ success: false, message: error.message });
});
} }
module.exports = requestsController; module.exports = requestsController;

View File

@@ -1,4 +1,4 @@
const SearchHistory = require('src/searchHistory/searchHistory'); const SearchHistory = require("src/searchHistory/searchHistory");
const searchHistory = new SearchHistory(); const searchHistory = new SearchHistory();
@@ -9,16 +9,16 @@ const searchHistory = new SearchHistory();
* @returns {Callback} * @returns {Callback}
*/ */
function historyController(req, res) { function historyController(req, res) {
const user = req.loggedInUser; const username = req.loggedInUser ? req.loggedInUser.username : null;
const username = user === undefined ? undefined : user.username;
searchHistory.read(username) searchHistory
.then(searchQueries => { .read(username)
res.send({ success: true, searchQueries }); .then(searchQueries => {
}) res.send({ success: true, searchQueries });
.catch(error => { })
res.status(404).send({ success: false, message: error.message }); .catch(error => {
}); res.status(404).send({ success: false, message: error.message });
});
} }
module.exports = historyController; module.exports = historyController;

View File

@@ -1,4 +1,4 @@
const UserRepository = require('src/user/userRepository'); const UserRepository = require("src/user/userRepository");
const userRepository = new UserRepository(); const userRepository = new UserRepository();
/** /**
* Controller: Retrieves settings of a logged in user * Controller: Retrieves settings of a logged in user
@@ -7,36 +7,35 @@ const userRepository = new UserRepository();
* @returns {Callback} * @returns {Callback}
*/ */
const getSettingsController = (req, res) => { const getSettingsController = (req, res) => {
const user = req.loggedInUser; const username = req.loggedInUser ? req.loggedInUser.username : null;
const username = user === undefined ? undefined : user.username;
userRepository.getSettings(username)
.then(settings => {
res.send({ success: true, settings });
})
.catch(error => {
res.status(404).send({ success: false, message: error.message });
});
}
userRepository
.getSettings(username)
.then(settings => {
res.send({ success: true, settings });
})
.catch(error => {
res.status(404).send({ success: false, message: error.message });
});
};
const updateSettingsController = (req, res) => { const updateSettingsController = (req, res) => {
const user = req.loggedInUser; const username = req.loggedInUser ? req.loggedInUser.username : null;
const username = user === undefined ? undefined : user.username;
const idempotencyKey = req.headers('Idempotency-Key'); // TODO implement better transactions const idempotencyKey = req.headers("Idempotency-Key"); // TODO implement better transactions
const { dark_mode, emoji } = req.body; const { dark_mode, emoji } = req.body;
userRepository.updateSettings(username, dark_mode, emoji) userRepository
.then(settings => { .updateSettings(username, dark_mode, emoji)
res.send({ success: true, settings }); .then(settings => {
}) res.send({ success: true, settings });
.catch(error => { })
res.status(404).send({ success: false, message: error.message }); .catch(error => {
}); res.status(404).send({ success: false, message: error.message });
} });
};
module.exports = { module.exports = {
getSettingsController, getSettingsController,
updateSettingsController updateSettingsController
} };