Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fahrtenbuch
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SWP WS21_22 - Fahrtenbuch
Team Einhorn
fahrtenbuch
Commits
61f48e4e
Commit
61f48e4e
authored
3 years ago
by
alrwasheda
Browse files
Options
Downloads
Patches
Plain Diff
showCurrentUser + updateCurrentUser Controllers
parent
42cef9e5
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server/src/controllers/userControllers.controllers.ts
+65
-0
65 additions, 0 deletions
server/src/controllers/userControllers.controllers.ts
with
65 additions
and
0 deletions
server/src/controllers/userControllers.controllers.ts
0 → 100644
+
65
−
0
View file @
61f48e4e
import
Worker
from
"
../db/models/Worker
"
;
import
{
Request
,
Response
}
from
"
express
"
;
//show current user GET
const
showCurrentUserController
=
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
if
(
!
(
res
.
locals
.
user
.
role
===
"
coordinator
"
))
{
return
res
.
status
(
403
)
.
json
({
success
:
false
,
error
:
"
MustBeCoordinator
"
});
}
//get current user (coordinator) data from DB
const
currentUserData
=
await
Worker
.
findAll
({
where
:
{
role
:
"
coordinator
"
,
},
});
res
.
status
(
200
).
json
({
success
:
true
,
result
:
currentUserData
});
}
catch
(
error
)
{
console
.
log
(
"
server error:
"
,
error
.
message
);
res
.
status
(
500
).
json
({
success
:
false
,
error
:
"
serverError!
"
});
}
};
//update current user PUT
const
updateCurrentUser
=
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
//check role
if
(
!
(
res
.
locals
.
user
.
role
===
"
coordinator
"
))
{
return
res
.
status
(
403
)
.
json
({
success
:
false
,
error
:
"
MustBeCoordinator
"
});
}
//take what ever in req.body is, and pass it to update()
const
input
=
req
.
body
;
//Data are saved in the DB in camelCase
//update coordinator data
await
Worker
.
update
(
input
,
{
where
:
{
role
:
"
coordinator
"
,
},
});
//send new coordinator-data back
const
updatedWorker
=
await
Worker
.
findAll
({
where
:
{
role
:
"
coordinator
"
,
},
});
return
res
.
status
(
200
).
json
({
success
:
true
,
result
:
updatedWorker
});
}
catch
(
error
)
{
console
.
error
(
"
server error:
"
,
error
.
message
);
return
res
.
status
(
500
).
json
({
success
:
false
,
error
:
"
serverError
"
});
}
};
const
userControllers
=
{
showCurrentUserController
,
updateCurrentUser
,
};
export
default
userControllers
;
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