const express=require("express");
|
|
const { findAllTask, findTaskById, addTask, updateTask, deleteTask } = require("../controller/TaskController");
|
|
const router=express.Router();
|
|
|
|
const Model=require('../model/Task');
|
|
|
|
router.get("/", (req, res) =>{
|
|
findAllTask().then((value)=>{
|
|
res.status(200).send(value);
|
|
}).catch((reason)=>{
|
|
res.status(500).send(reason);
|
|
});
|
|
});
|
|
|
|
router.get("/:id", (req, res) =>{
|
|
findTaskById(req.params.id).then((value)=>{
|
|
res.status(200).send(value);
|
|
}).catch((reason)=>{
|
|
res.status(500).send(reason);
|
|
});
|
|
});
|
|
|
|
router.post("/", (req, res) =>{
|
|
//Delete parent if set to ""
|
|
if(req.body.parent===""){
|
|
delete req.body.parent;
|
|
}
|
|
try{
|
|
addTask(req.body).then((value)=>{
|
|
res.status(200).send(value);
|
|
}).catch((reason)=>{
|
|
res.status(500).send(reason);
|
|
});
|
|
}catch(error){
|
|
res.status(500).send(error);
|
|
}
|
|
|
|
});
|
|
|
|
router.put("/:id",(req,res)=>{
|
|
//Delete parent if set to ""
|
|
if(req.body.parent===""){
|
|
delete req.body.parent;
|
|
req.body=Object.assign(req.body, {$unset:{parent:1}});
|
|
}
|
|
|
|
updateTask(req.params.id,req.body)
|
|
.then((value)=>
|
|
{
|
|
if(value===null){
|
|
res.status(500).send("No changes, invalid id ?");
|
|
}else{
|
|
res.status(200).send(value);
|
|
}
|
|
},(reason)=>{
|
|
res.status(500).send(reason.toString());
|
|
})
|
|
.catch((reason)=>{
|
|
res.status(500).send(reason);
|
|
});
|
|
});
|
|
|
|
router.delete("/:id", (req, res) =>{
|
|
deleteTask(req.params.id).then((value)=>{
|
|
if(value===null){
|
|
res.status(500).send("No changes, invalid id ?");
|
|
}else{
|
|
res.status(200).send(value);
|
|
}
|
|
}).catch((reason)=>{
|
|
res.status(500).send(reason);
|
|
});
|
|
});
|
|
|
|
module.exports=router;
|