#151 added to router/tasks try catch block everywhere

parent fe9eacab
...@@ -14,6 +14,7 @@ const dataSource = myDataSource; ...@@ -14,6 +14,7 @@ const dataSource = myDataSource;
/**get all tasks */ /**get all tasks */
router.get('/', async(req:Request, res:Response):Promise<Response> => { router.get('/', async(req:Request, res:Response):Promise<Response> => {
try{
const tasks = await dataSource const tasks = await dataSource
.getRepository(Task) .getRepository(Task)
.find({ .find({
...@@ -22,14 +23,16 @@ router.get('/', async(req:Request, res:Response):Promise<Response> => { ...@@ -22,14 +23,16 @@ router.get('/', async(req:Request, res:Response):Promise<Response> => {
author:true, author:true,
}}) }})
return res.send({tasks}) return res.send({tasks})
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
}) })
/**create new task for table MyTask*/ /**create new task for table MyTask*/
router.post('/', auth, async(req:Request, res:Response):Promise<Response>=>{ router.post('/', auth, async(req:Request, res:Response):Promise<Response>=>{
try{
const {user,title,description,project,executor,dateTimeStart,dateTimeDue, dateTimeDeadLine,priority, calendar} = req.body; const {user,title,description,project,executor,dateTimeStart,dateTimeDue, dateTimeDeadLine,priority, calendar} = req.body;
console.log('dateTimeStart \n', dateTimeStart, "dateTimeDue\n", dateTimeDue, 'dateTimeDeadLine\n ', dateTimeDeadLine)
console.log(req.body)
let dateTimeDueFinal = dateTimeDue let dateTimeDueFinal = dateTimeDue
let dateTimeStartFinal = dateTimeStart let dateTimeStartFinal = dateTimeStart
let dateTimeDeadlineFinal= dateTimeDeadLine let dateTimeDeadlineFinal= dateTimeDeadLine
...@@ -105,11 +108,16 @@ router.post('/', auth, async(req:Request, res:Response):Promise<Response>=>{ ...@@ -105,11 +108,16 @@ router.post('/', auth, async(req:Request, res:Response):Promise<Response>=>{
await newDateTimeTask.save() await newDateTimeTask.save()
} }
return res.send({newTask}); return res.send({newTask});
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
}) })
//** create new task for table UsersTasks*/ //** create new task for table UsersTasks*/
router.post('/users', auth, async(req:Request, res:Response):Promise<Response>=>{ router.post('/users', auth, async(req:Request, res:Response):Promise<Response>=>{
try{
const {user,title,description,project,executor,dateTimeStart,dateTimeDue, dateTimeDeadLine,priority} = req.body; const {user,title,description,project,executor,dateTimeStart,dateTimeDue, dateTimeDeadLine,priority} = req.body;
const newTask = new Task(); const newTask = new Task();
newTask.title = title; newTask.title = title;
...@@ -174,11 +182,16 @@ router.post('/users', auth, async(req:Request, res:Response):Promise<Response>=> ...@@ -174,11 +182,16 @@ router.post('/users', auth, async(req:Request, res:Response):Promise<Response>=>
await newDateTimeTask.save() await newDateTimeTask.save()
} }
return res.send({newTask}); return res.send({newTask});
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
}) })
/**check tasks of specific user by userID */ /**check tasks of specific user by userID */
router.get('/user/:userId', async (req: Request, res: Response):Promise<Response>=>{ router.get('/user/:userId', async (req: Request, res: Response):Promise<Response>=>{
try{
const userId = req.params.userId; const userId = req.params.userId;
const tasks = await dataSource const tasks = await dataSource
.getRepository(Task) .getRepository(Task)
...@@ -194,6 +207,9 @@ router.get('/user/:userId', async (req: Request, res: Response):Promise<Response ...@@ -194,6 +207,9 @@ router.get('/user/:userId', async (req: Request, res: Response):Promise<Response
}, },
}) })
return res.send({tasks}) return res.send({tasks})
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
}) })
...@@ -258,6 +274,7 @@ router.get('/my',auth, async (req: Request, res: Response):Promise<Response>=>{ ...@@ -258,6 +274,7 @@ router.get('/my',auth, async (req: Request, res: Response):Promise<Response>=>{
/**return tasks & users of projects in which current user is involved, search by token*/ /**return tasks & users of projects in which current user is involved, search by token*/
router.get('/related', auth,async (req: Request, res: Response):Promise<Response>=>{ router.get('/related', auth,async (req: Request, res: Response):Promise<Response>=>{
try{
const user = req.body.user const user = req.body.user
const tasks = await dataSource const tasks = await dataSource
.createQueryBuilder() .createQueryBuilder()
...@@ -296,11 +313,15 @@ router.get('/related', auth,async (req: Request, res: Response):Promise<Response ...@@ -296,11 +313,15 @@ router.get('/related', auth,async (req: Request, res: Response):Promise<Response
}) })
} }
return res.send({tasks, users}) return res.send({tasks, users})
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
}) })
/**delete of task by task id */ /**delete of task by task id */
router.delete('/:taskId',async (req: Request, res: Response):Promise<Response>=>{ router.delete('/:taskId',async (req: Request, res: Response):Promise<Response>=>{
try{
const taskId = req.params.taskId; const taskId = req.params.taskId;
await myDataSource await myDataSource
.createQueryBuilder() .createQueryBuilder()
...@@ -309,21 +330,22 @@ router.delete('/:taskId',async (req: Request, res: Response):Promise<Response>=> ...@@ -309,21 +330,22 @@ router.delete('/:taskId',async (req: Request, res: Response):Promise<Response>=>
.where("id = :id", { id: taskId }) .where("id = :id", { id: taskId })
.execute() .execute()
return res.send({message: 'Task deleted successfully'}) return res.send({message: 'Task deleted successfully'})
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
}) })
/**change of task by task id */ /**change of task by task id */
router.put('/:taskId',auth,authAuthorOrExecutorOfTask,async(req:Request, res:Response)=> { router.put('/:taskId',auth,authAuthorOrExecutorOfTask,async(req:Request, res:Response):Promise<Response>=> {
try{
const {taskId} = req.params const {taskId} = req.params
const {authorStatus,executorStatus,title,description,note, archive,project,dateTimeTaskId,dateTimeStart, const {authorStatus,executorStatus,title,description,note, archive,project,dateTimeTaskId,dateTimeStart,
dateTimeDue,executor,accomplish,dateTimeDeadLine, dateTimeFactDeadLine,priority} = req.body; dateTimeDue,executor,accomplish,dateTimeDeadLine, dateTimeFactDeadLine,priority} = req.body;
const task = await taskFinderById(taskId) const task = await taskFinderById(taskId)
if (!task) return res.status(404).send({Message:'task not found'}) if (!task) return res.status(404).send({Message:'task not found'})
console.log(req.body)
console.log ('dateTimeDeadLine ', dateTimeDeadLine,'authorStatus '
,authorStatus,'executorStatus ',executorStatus)
let dateTimeTask = null; let dateTimeTask = null;
if (dateTimeTaskId) { if (dateTimeTaskId) {
const dateTimeTaskData = await dataSource const dateTimeTaskData = await dataSource
...@@ -357,13 +379,18 @@ router.put('/:taskId',auth,authAuthorOrExecutorOfTask,async(req:Request, res:Res ...@@ -357,13 +379,18 @@ router.put('/:taskId',auth,authAuthorOrExecutorOfTask,async(req:Request, res:Res
} }
task.accomplish= accomplish task.accomplish= accomplish
await task.save() await task.save()
res.send({message:'update task successfully'}) return res.send({message:'update task successfully'})
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
}) })
/** search all tasks on given projects: project Id array[] */ /** search all tasks on given projects: project Id array[] */
router.post('/project',async (req: Request, res: Response):Promise<Response>=>{ router.post('/project',async (req: Request, res: Response):Promise<Response>=>{
let projectArray :string[]= req.body; let projectArray :string[]= req.body;
try{
if (projectArray.length===0) { if (projectArray.length===0) {
const rawTasks = await const rawTasks = await
dataSource dataSource
...@@ -392,6 +419,9 @@ router.post('/project',async (req: Request, res: Response):Promise<Response>=>{ ...@@ -392,6 +419,9 @@ router.post('/project',async (req: Request, res: Response):Promise<Response>=>{
return res.send({tasks}) return res.send({tasks})
} 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