Updated all imports to be relative to itself

This commit is contained in:
2022-08-16 01:15:36 +02:00
parent 3660e88acf
commit 7094aa2bb5
70 changed files with 1066 additions and 885 deletions

View File

@@ -1,52 +1,57 @@
const establishedDatabase = require('src/database/database');
const establishedDatabase = require("../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',
};
}
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 new Promise((resolve, reject) => this.database.all(this.queries.read, user)
.then((result, error) => {
if (error) throw new Error(error);
resolve(result.map(row => row.search_query));
})
.catch((error) => {
console.log('Error when fetching history from database:', error)
reject('Unable to get history.');
}));
}
read(user) {
return new Promise((resolve, reject) =>
this.database
.all(this.queries.read, user)
.then((result, error) => {
if (error) throw new Error(error);
resolve(result.map(row => row.search_query));
})
.catch(error => {
console.log("Error when fetching history from database:", error);
reject("Unable to get history.");
})
);
}
/**
/**
* Creates a new search entry in the database.
* @param {String} username logged in user doing the search
* @param {String} searchQuery the query the user searched for
* @returns {Promise}
*/
create(username, searchQuery) {
return this.database.run(this.queries.create, [searchQuery, username])
.catch(error => {
if (error.message.includes('FOREIGN')) {
throw new Error('Could not create search history.');
}
create(username, searchQuery) {
return this.database
.run(this.queries.create, [searchQuery, username])
.catch(error => {
if (error.message.includes("FOREIGN")) {
throw new Error("Could not create search history.");
}
throw {
success: false,
status: 500,
message: 'An unexpected error occured',
source: 'database'
}
});
}
throw {
success: false,
status: 500,
message: "An unexpected error occured",
source: "database"
};
});
}
}
module.exports = SearchHistory;