#151 added try blocks to users.ts

parent 96114ae9
......@@ -29,15 +29,20 @@ const upload = multer({ storage })
//** return all users of DB */
router.get('/', async (req : Request, res : Response):Promise<object> => {
try{
const users = await dataSource
.getRepository(User)
.createQueryBuilder("user")
.getMany()
return res.send({users})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
//** return displayName of user, requested by userId */
router.get('/display-name/:userId', auth, async (req : Request, res : Response):Promise<object> => {
try{
const {userId} = req.params
const displayName = await dataSource
.createQueryBuilder()
......@@ -47,11 +52,15 @@ return res.send({users})
.getOne()
if (!displayName) return res.status(404).send({message:'displayName not found'})
return res.send(displayName)
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
//** return all users of DB */
router.get('/all-fields/', async (req : Request, res : Response):Promise<object> => {
try{
const users = await dataSource
.getRepository(User)
.find({
......@@ -63,11 +72,15 @@ router.get('/all-fields/', async (req : Request, res : Response):Promise<object>
}
})
return res.send({users})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
//** return displayName of user, requested by userId */
router.get('/display-name/:userId',auth, async (req : Request, res : Response):Promise<object> => {
try{
const {userId} =req.params
const displayName = await dataSource
.createQueryBuilder()
......@@ -77,11 +90,15 @@ router.get('/all-fields/', async (req : Request, res : Response):Promise<object>
.getOne()
if (!displayName) return res.status(404).send({message:'displayName not found'})
return res.send({displayName})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
/**create new user*/
router.post('/', upload.single("avatar"), async (req : Request, res : Response):Promise<object> => {
try{
const {name,surname,password,email, role} = req.body;
const displayName = surname+' '+name[0]+'.'
const user = new User();
......@@ -118,10 +135,14 @@ router.post('/', upload.single("avatar"), async (req : Request, res : Response):
email: user.email
})
return res.send({userToFront})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
/** log in*/
router.post('/sessions/', async (req : Request, res : Response):Promise<object> => {
try{
const {email, password} = req.body;
const user = await dataSource
.createQueryBuilder()
......@@ -143,10 +164,14 @@ router.post('/sessions/', async (req : Request, res : Response):Promise<object>
message: "message: 'Correct user & password",
user: userToFront
})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
/** log out*/
router.delete('/sessions', async(req: Request, res: Response):Promise<void | object> => {
try{
const token = req.get('Authorization');
const successMsg = {message:'success'};
if(!token) return res.send(successMsg)
......@@ -157,7 +182,9 @@ router.delete('/sessions', async(req: Request, res: Response):Promise<void | obj
user.token = nanoid();
await user.save();
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