#151 added try blocks to users.ts

parent 96114ae9
...@@ -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})
}
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment