#151 added to router/tasks try catch block everywhere

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