Skip to content
Snippets Groups Projects
Commit ce0d2afc authored by alec.schmidt's avatar alec.schmidt
Browse files

a bit better db handling in backend

parent fd2091af
No related branches found
No related tags found
No related merge requests found
......@@ -47,9 +47,8 @@ class DBHandler {
var okay = await this.comparePassword(user)
else
throw JSON.stringify({message: "Username does not exists"});
if (okay) {
return await asyncdb.all("SELECT username, user_type FROM users WHERE username='" + user.username + "'")
}
if (okay)
return await asyncdb.get("SELECT username, user_type FROM users WHERE username='" + user.username + "'")
else
throw JSON.stringify({message: "Incorrect Password"});
}
......@@ -69,7 +68,8 @@ class DBHandler {
}
async getUsers(res:express.Response) {
asyncdb.all("SELECT * FROM users")
const query = "SELECT * FROM users";
asyncdb.all(query)
.then(result => res.status(StatusCodes.OK).json(result).end())
.catch(err => console.log(err));
}
......@@ -77,14 +77,10 @@ class DBHandler {
async postUser(req:express.Request, res:express.Response) {
const a = req.body as User_t;
const request = "INSERT INTO users (username, user_pass, user_type) \
const query = "INSERT INTO users (username, user_pass, user_type) \
VALUES ('" + a.username + "','" + a.password + "','" + a.type +"')";
asyncdb.all(request)
asyncdb.all(query)
.then( () => res.status(StatusCodes.OK))
.catch(err => console.log(err));
}
......@@ -115,10 +111,10 @@ class DBHandler {
const insert_question = "INSERT INTO questions (question, category) VALUES ('"+ a.question +"', '"+ a.category +"')";
const get_id = "SELECT id FROM questions WHERE question='" + a.question + "'";
asyncdb.all(insert_question)
.then( () => {
asyncdb.get(get_id)
.then(id_json => {
await asyncdb.all(insert_question);
const id_json = await asyncdb.get(get_id)
const id_obj = id_json as Question_t;
const insert_correct = "INSERT INTO answer (text_answer, id_question, correct) \
VALUES ('" + a.answers.CORRECT + "', " + id_obj.id + ", 'true')";
......@@ -130,10 +126,7 @@ class DBHandler {
a.answers.WRONG.forEach(answer => asyncdb.all("INSERT INTO answer (text_answer, id_question, correct) \
VALUES ('" + answer + "', " + id_obj.id + ", 'false')")
.catch(err => console.log(err)));
})
.catch(e => console.log(e));
})
.catch(e => console.log(e));
res.end();
}
......
......@@ -167,7 +167,7 @@ router.post(ROUTE+"/login", (req: express.Request, res: express.Response) => {
DBHandler.getLoginJSON(user)
.then(loginJSON => {
res.status(StatusCodes.OK).json(generateToken(loginJSON[0] as User_t))})
res.status(StatusCodes.OK).json(generateToken(loginJSON as User_t))})
.catch(err => {
console.log(err)
res.status(StatusCodes.UNAUTHORIZED).json(JSON.parse(err));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment