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 { ...@@ -47,9 +47,8 @@ class DBHandler {
var okay = await this.comparePassword(user) var okay = await this.comparePassword(user)
else else
throw JSON.stringify({message: "Username does not exists"}); throw JSON.stringify({message: "Username does not exists"});
if (okay) { if (okay)
return await asyncdb.all("SELECT username, user_type FROM users WHERE username='" + user.username + "'") return await asyncdb.get("SELECT username, user_type FROM users WHERE username='" + user.username + "'")
}
else else
throw JSON.stringify({message: "Incorrect Password"}); throw JSON.stringify({message: "Incorrect Password"});
} }
...@@ -69,7 +68,8 @@ class DBHandler { ...@@ -69,7 +68,8 @@ class DBHandler {
} }
async getUsers(res:express.Response) { 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()) .then(result => res.status(StatusCodes.OK).json(result).end())
.catch(err => console.log(err)); .catch(err => console.log(err));
} }
...@@ -77,14 +77,10 @@ class DBHandler { ...@@ -77,14 +77,10 @@ class DBHandler {
async postUser(req:express.Request, res:express.Response) { async postUser(req:express.Request, res:express.Response) {
const a = req.body as User_t; const a = req.body as User_t;
const query = "INSERT INTO users (username, user_pass, user_type) \
const request = "INSERT INTO users (username, user_pass, user_type) \
VALUES ('" + a.username + "','" + a.password + "','" + a.type +"')"; VALUES ('" + a.username + "','" + a.password + "','" + a.type +"')";
asyncdb.all(query)
asyncdb.all(request)
.then( () => res.status(StatusCodes.OK)) .then( () => res.status(StatusCodes.OK))
.catch(err => console.log(err)); .catch(err => console.log(err));
} }
...@@ -115,10 +111,10 @@ class DBHandler { ...@@ -115,10 +111,10 @@ class DBHandler {
const insert_question = "INSERT INTO questions (question, category) VALUES ('"+ a.question +"', '"+ a.category +"')"; const insert_question = "INSERT INTO questions (question, category) VALUES ('"+ a.question +"', '"+ a.category +"')";
const get_id = "SELECT id FROM questions WHERE question='" + a.question + "'"; const get_id = "SELECT id FROM questions WHERE question='" + a.question + "'";
asyncdb.all(insert_question) await asyncdb.all(insert_question);
.then( () => {
asyncdb.get(get_id) const id_json = await asyncdb.get(get_id)
.then(id_json => {
const id_obj = id_json as Question_t; const id_obj = id_json as Question_t;
const insert_correct = "INSERT INTO answer (text_answer, id_question, correct) \ const insert_correct = "INSERT INTO answer (text_answer, id_question, correct) \
VALUES ('" + a.answers.CORRECT + "', " + id_obj.id + ", 'true')"; VALUES ('" + a.answers.CORRECT + "', " + id_obj.id + ", 'true')";
...@@ -130,10 +126,7 @@ class DBHandler { ...@@ -130,10 +126,7 @@ class DBHandler {
a.answers.WRONG.forEach(answer => asyncdb.all("INSERT INTO answer (text_answer, id_question, correct) \ a.answers.WRONG.forEach(answer => asyncdb.all("INSERT INTO answer (text_answer, id_question, correct) \
VALUES ('" + answer + "', " + id_obj.id + ", 'false')") VALUES ('" + answer + "', " + id_obj.id + ", 'false')")
.catch(err => console.log(err))); .catch(err => console.log(err)));
})
.catch(e => console.log(e));
})
.catch(e => console.log(e));
res.end(); res.end();
} }
... ...
......
...@@ -167,7 +167,7 @@ router.post(ROUTE+"/login", (req: express.Request, res: express.Response) => { ...@@ -167,7 +167,7 @@ router.post(ROUTE+"/login", (req: express.Request, res: express.Response) => {
DBHandler.getLoginJSON(user) DBHandler.getLoginJSON(user)
.then(loginJSON => { .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 => { .catch(err => {
console.log(err) console.log(err)
res.status(StatusCodes.UNAUTHORIZED).json(JSON.parse(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 to comment