From 8dd3a614f3889d586dfad6fd4a9bad37eaa1fe7f Mon Sep 17 00:00:00 2001
From: "alec.schmidt" <alec.schmidt@etu.hesge.ch>
Date: Wed, 7 Jun 2023 18:17:08 +0200
Subject: [PATCH] backend refactor done

---
 API/db/app.db                | Bin 36864 -> 36864 bytes
 API/db/create_db.sql         |   6 +++---
 API/src/database/Database.ts |  18 +++++++++---------
 API/src/routes/BaseRoutes.ts |   4 ++--
 4 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/API/db/app.db b/API/db/app.db
index 56182c95bd846eb45693d59fe952a2c56c3ec843..c1b39459ffb292715ef4a0060a6bbfe525c322b4 100644
GIT binary patch
delta 210
zcmZozz|^pSX@WE(^F$eES!M>kw3od6KNvW83>o-M_|Nh^;@!))p4W)ChsSWUpukq{
z&89po%$#D3?Be3$j7{8=o%kDg6H{_C^Aw!?{e4^={U)F0x8!2xKhD7ak^e6L@y&t?
zJNey3m{}R5MMcXp^O95ZN=j0TOG=7LQ-zpV8AL_J!Q9m1lC;E};#6KnkQirTPHHj;
yb1^V5F!MiV;Qzt@p8xS?L4&LOqTI~Vj8K!9IhaKmLHe0C|IlX_U}Rq85C8yzzB(iT

delta 210
zcmZozz|^pSX@WE(&qNt#Ssn(xw3od6KNvW8Od0qs_|Nh^<2}f?nb(YW0*~otL4lpz
zn+<tbm^p<Q*~P`h8Jn0V+wnJWmQ)s`DulR3giJooZ^^~Pe}#en4gVGXeVYXZHuL+6
ziHk~>q!yPLgNTBh#LCnnF;-RvX;I0t%)I2(yb^@C5F;ytsHk{iPHHk3r{relaWOD3
sF!8@(;Qzt@hX2iGL4)i3!dxIN#%$co(u`0uSU3OBXBS{(TjUS`09Kwn{{R30

diff --git a/API/db/create_db.sql b/API/db/create_db.sql
index 2bdd0de..ceb75d8 100644
--- a/API/db/create_db.sql
+++ b/API/db/create_db.sql
@@ -5,7 +5,7 @@ CREATE TABLE users (
     id INTEGER PRIMARY KEY AUTOINCREMENT,
     username TEXT UNIQUE,
     password TEXT,
-    type TEXT
+    admin BOOLEAN
 );
 
 CREATE TABLE questions (
@@ -28,8 +28,8 @@ CREATE TABLE answer (
 );
 
 
-INSERT INTO users (username, password, type)
-VALUES ('alec', 'alec', 'admin');
+INSERT INTO users (username, password, admin)
+VALUES ('alec', 'alec', true);
 
 INSERT INTO category (title)
 VALUES ('Histoire'), ('Géographie');
diff --git a/API/src/database/Database.ts b/API/src/database/Database.ts
index 5a94f6f..d5f1a10 100644
--- a/API/src/database/Database.ts
+++ b/API/src/database/Database.ts
@@ -15,7 +15,7 @@ export type User_t = {
     id: number;
     username: string;
     password: string;
-    type: string;
+    admin: boolean;
 };
 
 export type Question_t = {
@@ -47,7 +47,7 @@ class DBHandler {
         else
             throw JSON.stringify({message: "Username does not exists"});
         if (okay)
-            return await asyncdb.get("SELECT username, type FROM users WHERE username='" + user.username + "'")
+            return await asyncdb.get("SELECT username, admin FROM users WHERE username='" + user.username + "'")
         else
             throw JSON.stringify({message: "Incorrect Password"});
     }
@@ -61,13 +61,13 @@ class DBHandler {
     }
 
     async comparePassword(user: User_t) : Promise<boolean> {
-        const query = "SELECT password, type FROM users WHERE username='"+ user.username + "'";
+        const query = "SELECT password, admin FROM users WHERE username='"+ user.username + "'";
         const password = await asyncdb.all<User_t>(query)
         return (password[0].password == user.password);
     }
 
     async getUsers(res:express.Response) {
-        const query = "SELECT id, username, type FROM users";
+        const query = "SELECT id, username, admin FROM users";
         asyncdb.all(query)
             .then(result => {
                 res.status(StatusCodes.OK).json(result).end()}
@@ -78,11 +78,11 @@ class DBHandler {
     async postUser(req:express.Request, res:express.Response) {
         let a = req.body as User_t;
         
-        if (a.type === undefined)
-            a.type = "player"
+        if (a.admin === undefined)
+            a.admin = false;
 
-        const query = "INSERT INTO users (username, password, type) \
-                        VALUES ('" + a.username + "','" + a.password + "','" + a.type +"')";
+        const query = "INSERT INTO users (username, password, admin) \
+                        VALUES ('" + a.username + "','" + a.password + "','" + a.admin +"')";
 
         asyncdb.all(query)
             .then( () => res.status(StatusCodes.OK))
@@ -93,7 +93,7 @@ class DBHandler {
         const a = req.body as User_t;
 
         const request = "UPDATE users \
-                        SET username = '"+ a.username + "', type = '" + a.type + "' WHERE id = " + req.params.id;
+                        SET username = '"+ a.username + "', admin = '" + a.admin + "' WHERE id = " + req.params.id;
 
         asyncdb.all(request)
         .then(() => res.status(StatusCodes.OK).end())
diff --git a/API/src/routes/BaseRoutes.ts b/API/src/routes/BaseRoutes.ts
index 5c9db80..0382783 100644
--- a/API/src/routes/BaseRoutes.ts
+++ b/API/src/routes/BaseRoutes.ts
@@ -29,7 +29,7 @@ function isAdmin(token: string): Boolean {
         // console.log(err);
         
         if (err) return false;
-            adminToken = user.type === "admin"
+            adminToken = user.admin
     });
 
     return adminToken;
@@ -99,7 +99,7 @@ router.patch(ROUTE+'/user/:id', (req: express.Request, res: express.Response) =>
 
     console.log(a);
 
-    if (a.type === "admin")
+    if (a.admin === true)
         if (!isAdmin(req.headers['authorization'] && req.headers['authorization'].split(' ')[1]))
             res.status(StatusCodes.UNAUTHORIZED).end();
     
-- 
GitLab