Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
archiWEB-TP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hepia_2022-2023
Web
archiWEB-TP
Commits
8dd3a614
Commit
8dd3a614
authored
2 years ago
by
alec.schmidt
Browse files
Options
Downloads
Patches
Plain Diff
backend refactor done
parent
5b941608
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
API/db/app.db
+0
-0
0 additions, 0 deletions
API/db/app.db
API/db/create_db.sql
+3
-3
3 additions, 3 deletions
API/db/create_db.sql
API/src/database/Database.ts
+9
-9
9 additions, 9 deletions
API/src/database/Database.ts
API/src/routes/BaseRoutes.ts
+2
-2
2 additions, 2 deletions
API/src/routes/BaseRoutes.ts
with
14 additions
and
14 deletions
API/db/app.db
+
0
−
0
View file @
8dd3a614
No preview for this file type
This diff is collapsed.
Click to expand it.
API/db/create_db.sql
+
3
−
3
View file @
8dd3a614
...
@@ -5,7 +5,7 @@ CREATE TABLE users (
...
@@ -5,7 +5,7 @@ CREATE TABLE users (
id
INTEGER
PRIMARY
KEY
AUTOINCREMENT
,
id
INTEGER
PRIMARY
KEY
AUTOINCREMENT
,
username
TEXT
UNIQUE
,
username
TEXT
UNIQUE
,
password
TEXT
,
password
TEXT
,
type
TEXT
admin
BOOLEAN
);
);
CREATE
TABLE
questions
(
CREATE
TABLE
questions
(
...
@@ -28,8 +28,8 @@ CREATE TABLE answer (
...
@@ -28,8 +28,8 @@ CREATE TABLE answer (
);
);
INSERT
INTO
users
(
username
,
password
,
type
)
INSERT
INTO
users
(
username
,
password
,
admin
)
VALUES
(
'alec'
,
'alec'
,
'admin'
);
VALUES
(
'alec'
,
'alec'
,
true
);
INSERT
INTO
category
(
title
)
INSERT
INTO
category
(
title
)
VALUES
(
'Histoire'
),
(
'Géographie'
);
VALUES
(
'Histoire'
),
(
'Géographie'
);
...
...
This diff is collapsed.
Click to expand it.
API/src/database/Database.ts
+
9
−
9
View file @
8dd3a614
...
@@ -15,7 +15,7 @@ export type User_t = {
...
@@ -15,7 +15,7 @@ export type User_t = {
id
:
number
;
id
:
number
;
username
:
string
;
username
:
string
;
password
:
string
;
password
:
string
;
type
:
string
;
admin
:
boolean
;
};
};
export
type
Question_t
=
{
export
type
Question_t
=
{
...
@@ -47,7 +47,7 @@ class DBHandler {
...
@@ -47,7 +47,7 @@ class DBHandler {
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
.
get
(
"
SELECT username,
type
FROM users WHERE username='
"
+
user
.
username
+
"
'
"
)
return
await
asyncdb
.
get
(
"
SELECT username,
admin
FROM users WHERE username='
"
+
user
.
username
+
"
'
"
)
else
else
throw
JSON
.
stringify
({
message
:
"
Incorrect Password
"
});
throw
JSON
.
stringify
({
message
:
"
Incorrect Password
"
});
}
}
...
@@ -61,13 +61,13 @@ class DBHandler {
...
@@ -61,13 +61,13 @@ class DBHandler {
}
}
async
comparePassword
(
user
:
User_t
)
:
Promise
<
boolean
>
{
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
)
const
password
=
await
asyncdb
.
all
<
User_t
>
(
query
)
return
(
password
[
0
].
password
==
user
.
password
);
return
(
password
[
0
].
password
==
user
.
password
);
}
}
async
getUsers
(
res
:
express
.
Response
)
{
async
getUsers
(
res
:
express
.
Response
)
{
const
query
=
"
SELECT id, username,
type
FROM users
"
;
const
query
=
"
SELECT id, username,
admin
FROM users
"
;
asyncdb
.
all
(
query
)
asyncdb
.
all
(
query
)
.
then
(
result
=>
{
.
then
(
result
=>
{
res
.
status
(
StatusCodes
.
OK
).
json
(
result
).
end
()}
res
.
status
(
StatusCodes
.
OK
).
json
(
result
).
end
()}
...
@@ -78,11 +78,11 @@ class DBHandler {
...
@@ -78,11 +78,11 @@ class DBHandler {
async
postUser
(
req
:
express
.
Request
,
res
:
express
.
Response
)
{
async
postUser
(
req
:
express
.
Request
,
res
:
express
.
Response
)
{
let
a
=
req
.
body
as
User_t
;
let
a
=
req
.
body
as
User_t
;
if
(
a
.
type
===
undefined
)
if
(
a
.
admin
===
undefined
)
a
.
type
=
"
player
"
a
.
admin
=
false
;
const
query
=
"
INSERT INTO users (username, password,
type
)
\
const
query
=
"
INSERT INTO users (username, password,
admin
)
\
VALUES ('
"
+
a
.
username
+
"
','
"
+
a
.
password
+
"
','
"
+
a
.
type
+
"
')
"
;
VALUES ('
"
+
a
.
username
+
"
','
"
+
a
.
password
+
"
','
"
+
a
.
admin
+
"
')
"
;
asyncdb
.
all
(
query
)
asyncdb
.
all
(
query
)
.
then
(
()
=>
res
.
status
(
StatusCodes
.
OK
))
.
then
(
()
=>
res
.
status
(
StatusCodes
.
OK
))
...
@@ -93,7 +93,7 @@ class DBHandler {
...
@@ -93,7 +93,7 @@ class DBHandler {
const
a
=
req
.
body
as
User_t
;
const
a
=
req
.
body
as
User_t
;
const
request
=
"
UPDATE users
\
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
)
asyncdb
.
all
(
request
)
.
then
(()
=>
res
.
status
(
StatusCodes
.
OK
).
end
())
.
then
(()
=>
res
.
status
(
StatusCodes
.
OK
).
end
())
...
...
This diff is collapsed.
Click to expand it.
API/src/routes/BaseRoutes.ts
+
2
−
2
View file @
8dd3a614
...
@@ -29,7 +29,7 @@ function isAdmin(token: string): Boolean {
...
@@ -29,7 +29,7 @@ function isAdmin(token: string): Boolean {
// console.log(err);
// console.log(err);
if
(
err
)
return
false
;
if
(
err
)
return
false
;
adminToken
=
user
.
type
===
"
admin
"
adminToken
=
user
.
admin
});
});
return
adminToken
;
return
adminToken
;
...
@@ -99,7 +99,7 @@ router.patch(ROUTE+'/user/:id', (req: express.Request, res: express.Response) =>
...
@@ -99,7 +99,7 @@ router.patch(ROUTE+'/user/:id', (req: express.Request, res: express.Response) =>
console
.
log
(
a
);
console
.
log
(
a
);
if
(
a
.
type
===
"
admin
"
)
if
(
a
.
admin
===
true
)
if
(
!
isAdmin
(
req
.
headers
[
'
authorization
'
]
&&
req
.
headers
[
'
authorization
'
].
split
(
'
'
)[
1
]))
if
(
!
isAdmin
(
req
.
headers
[
'
authorization
'
]
&&
req
.
headers
[
'
authorization
'
].
split
(
'
'
)[
1
]))
res
.
status
(
StatusCodes
.
UNAUTHORIZED
).
end
();
res
.
status
(
StatusCodes
.
UNAUTHORIZED
).
end
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment