diff --git a/seasoned_api/src/searchHistory/searchHistory.js b/seasoned_api/src/searchHistory/searchHistory.js new file mode 100644 index 0000000..fbe2f35 --- /dev/null +++ b/seasoned_api/src/searchHistory/searchHistory.js @@ -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;