Added searchHistory for adding a logged in users history trace. (More like a test function of the page)
This commit is contained in:
39
seasoned_api/src/searchHistory/searchHistory.js
Normal file
39
seasoned_api/src/searchHistory/searchHistory.js
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
const establishedDatabase = require('src/database/database');
|
||||||
|
|
||||||
|
class SearchHistory {
|
||||||
|
|
||||||
|
constructor(database) {
|
||||||
|
this.database = database || establishedDatabase;
|
||||||
|
this.queries = {
|
||||||
|
'create': 'insert into search_history (search_query, user_name) values (?, ?)',
|
||||||
|
'read': 'select search_query from search_history where user_name = ? order by id desc',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrive a search queries for a user from the database.
|
||||||
|
* @param {User} user existing user
|
||||||
|
* @returns {Promise}
|
||||||
|
*/
|
||||||
|
read(user) {
|
||||||
|
return this.database.all(this.queries.read, user.username)
|
||||||
|
.then(rows => rows.map(row => row.search_query));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new search entry in the database.
|
||||||
|
* @param {User} user a new user
|
||||||
|
* @param {String} searchQuery the query the user searched for
|
||||||
|
* @returns {Promise}
|
||||||
|
*/
|
||||||
|
create(user, searchQuery) {
|
||||||
|
return this.database.run(this.queries.create, [searchQuery, user.username]).catch((error) => {
|
||||||
|
if (error.message.includes('FOREIGN')) {
|
||||||
|
throw new Error('Could not create search history.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = SearchHistory;
|
||||||
Reference in New Issue
Block a user