Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
planner-team-one
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
21
Issues
21
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Евгений Положенцев
planner-team-one
Commits
3132fbbd
Commit
3132fbbd
authored
Jan 17, 2023
by
Евгений Положенцев
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#151
added try blocks to users.ts
parent
96114ae9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
1 deletion
+28
-1
users.ts
planner-api/src/routers/users.ts
+28
-1
No files found.
planner-api/src/routers/users.ts
View file @
3132fbbd
...
@@ -29,15 +29,20 @@ const upload = multer({ storage })
...
@@ -29,15 +29,20 @@ const upload = multer({ storage })
//** return all users of DB */
//** return all users of DB */
router
.
get
(
'/'
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
object
>
=>
{
router
.
get
(
'/'
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
object
>
=>
{
try
{
const
users
=
await
dataSource
const
users
=
await
dataSource
.
getRepository
(
User
)
.
getRepository
(
User
)
.
createQueryBuilder
(
"user"
)
.
createQueryBuilder
(
"user"
)
.
getMany
()
.
getMany
()
return
res
.
send
({
users
})
return
res
.
send
({
users
})
}
catch
(
e
)
{
return
res
.
status
(
502
).
send
({
message
:(
e
as
Error
).
message
})
}
})
})
//** return displayName of user, requested by userId */
//** return displayName of user, requested by userId */
router
.
get
(
'/display-name/:userId'
,
auth
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
object
>
=>
{
router
.
get
(
'/display-name/:userId'
,
auth
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
object
>
=>
{
try
{
const
{
userId
}
=
req
.
params
const
{
userId
}
=
req
.
params
const
displayName
=
await
dataSource
const
displayName
=
await
dataSource
.
createQueryBuilder
()
.
createQueryBuilder
()
...
@@ -47,11 +52,15 @@ return res.send({users})
...
@@ -47,11 +52,15 @@ return res.send({users})
.
getOne
()
.
getOne
()
if
(
!
displayName
)
return
res
.
status
(
404
).
send
({
message
:
'displayName not found'
})
if
(
!
displayName
)
return
res
.
status
(
404
).
send
({
message
:
'displayName not found'
})
return
res
.
send
(
displayName
)
return
res
.
send
(
displayName
)
}
catch
(
e
)
{
return
res
.
status
(
502
).
send
({
message
:(
e
as
Error
).
message
})
}
})
})
//** return all users of DB */
//** return all users of DB */
router
.
get
(
'/all-fields/'
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
object
>
=>
{
router
.
get
(
'/all-fields/'
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
object
>
=>
{
try
{
const
users
=
await
dataSource
const
users
=
await
dataSource
.
getRepository
(
User
)
.
getRepository
(
User
)
.
find
({
.
find
({
...
@@ -63,11 +72,15 @@ router.get('/all-fields/', async (req : Request, res : Response):Promise<object>
...
@@ -63,11 +72,15 @@ router.get('/all-fields/', async (req : Request, res : Response):Promise<object>
}
}
})
})
return
res
.
send
({
users
})
return
res
.
send
({
users
})
}
catch
(
e
)
{
return
res
.
status
(
502
).
send
({
message
:(
e
as
Error
).
message
})
}
})
})
//** return displayName of user, requested by userId */
//** return displayName of user, requested by userId */
router
.
get
(
'/display-name/:userId'
,
auth
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
object
>
=>
{
router
.
get
(
'/display-name/:userId'
,
auth
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
object
>
=>
{
try
{
const
{
userId
}
=
req
.
params
const
{
userId
}
=
req
.
params
const
displayName
=
await
dataSource
const
displayName
=
await
dataSource
.
createQueryBuilder
()
.
createQueryBuilder
()
...
@@ -77,11 +90,15 @@ router.get('/all-fields/', async (req : Request, res : Response):Promise<object>
...
@@ -77,11 +90,15 @@ router.get('/all-fields/', async (req : Request, res : Response):Promise<object>
.
getOne
()
.
getOne
()
if
(
!
displayName
)
return
res
.
status
(
404
).
send
({
message
:
'displayName not found'
})
if
(
!
displayName
)
return
res
.
status
(
404
).
send
({
message
:
'displayName not found'
})
return
res
.
send
({
displayName
})
return
res
.
send
({
displayName
})
}
catch
(
e
)
{
return
res
.
status
(
502
).
send
({
message
:(
e
as
Error
).
message
})
}
})
})
/**create new user*/
/**create new user*/
router
.
post
(
'/'
,
upload
.
single
(
"avatar"
),
async
(
req
:
Request
,
res
:
Response
):
Promise
<
object
>
=>
{
router
.
post
(
'/'
,
upload
.
single
(
"avatar"
),
async
(
req
:
Request
,
res
:
Response
):
Promise
<
object
>
=>
{
try
{
const
{
name
,
surname
,
password
,
email
,
role
}
=
req
.
body
;
const
{
name
,
surname
,
password
,
email
,
role
}
=
req
.
body
;
const
displayName
=
surname
+
' '
+
name
[
0
]
+
'.'
const
displayName
=
surname
+
' '
+
name
[
0
]
+
'.'
const
user
=
new
User
();
const
user
=
new
User
();
...
@@ -118,10 +135,14 @@ router.post('/', upload.single("avatar"), async (req : Request, res : Response):
...
@@ -118,10 +135,14 @@ router.post('/', upload.single("avatar"), async (req : Request, res : Response):
email
:
user
.
email
email
:
user
.
email
})
})
return
res
.
send
({
userToFront
})
return
res
.
send
({
userToFront
})
}
catch
(
e
)
{
return
res
.
status
(
502
).
send
({
message
:(
e
as
Error
).
message
})
}
})
})
/** log in*/
/** log in*/
router
.
post
(
'/sessions/'
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
object
>
=>
{
router
.
post
(
'/sessions/'
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
object
>
=>
{
try
{
const
{
email
,
password
}
=
req
.
body
;
const
{
email
,
password
}
=
req
.
body
;
const
user
=
await
dataSource
const
user
=
await
dataSource
.
createQueryBuilder
()
.
createQueryBuilder
()
...
@@ -143,10 +164,14 @@ router.post('/sessions/', async (req : Request, res : Response):Promise<object>
...
@@ -143,10 +164,14 @@ router.post('/sessions/', async (req : Request, res : Response):Promise<object>
message
:
"message: 'Correct user & password"
,
message
:
"message: 'Correct user & password"
,
user
:
userToFront
user
:
userToFront
})
})
}
catch
(
e
)
{
return
res
.
status
(
502
).
send
({
message
:(
e
as
Error
).
message
})
}
})
})
/** log out*/
/** log out*/
router
.
delete
(
'/sessions'
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
|
object
>
=>
{
router
.
delete
(
'/sessions'
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
|
object
>
=>
{
try
{
const
token
=
req
.
get
(
'Authorization'
);
const
token
=
req
.
get
(
'Authorization'
);
const
successMsg
=
{
message
:
'success'
};
const
successMsg
=
{
message
:
'success'
};
if
(
!
token
)
return
res
.
send
(
successMsg
)
if
(
!
token
)
return
res
.
send
(
successMsg
)
...
@@ -157,7 +182,9 @@ router.delete('/sessions', async(req: Request, res: Response):Promise<void | obj
...
@@ -157,7 +182,9 @@ router.delete('/sessions', async(req: Request, res: Response):Promise<void | obj
user
.
token
=
nanoid
();
user
.
token
=
nanoid
();
await
user
.
save
();
await
user
.
save
();
return
res
.
send
(
successMsg
)
return
res
.
send
(
successMsg
)
}
catch
(
e
)
{
return
res
.
status
(
502
).
send
({
message
:(
e
as
Error
).
message
})
}
})
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment