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
ce0d2afc
Commit
ce0d2afc
authored
2 years ago
by
alec.schmidt
Browse files
Options
Downloads
Patches
Plain Diff
a bit better db handling in backend
parent
fd2091af
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
API/src/database/Database.ts
+24
-31
24 additions, 31 deletions
API/src/database/Database.ts
API/src/routes/BaseRoutes.ts
+1
-1
1 addition, 1 deletion
API/src/routes/BaseRoutes.ts
with
25 additions
and
32 deletions
API/src/database/Database.ts
+
24
−
31
View file @
ce0d2afc
...
...
@@ -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
();
}
...
...
This diff is collapsed.
Click to expand it.
API/src/routes/BaseRoutes.ts
+
1
−
1
View file @
ce0d2afc
...
...
@@ -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
));
...
...
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