added update func in generic controller

parent 3a2941ee
...@@ -67,9 +67,24 @@ const deleteDoc = (model: Model<any>) => (req: Request, res: Response) => { ...@@ -67,9 +67,24 @@ const deleteDoc = (model: Model<any>) => (req: Request, res: Response) => {
}); });
}; };
const update = (model: Model<any>) => (req: Request, res: Response) => {
const id = req.params.id;
const data = {...req.body};
model
.findByIdAndUpdate(id, data)
.then((result) => {
return res.status(200).send(result);
})
.catch((error) => {
console.log(error);
return res.status(500).json({error});
});
};
export default { export default {
create, create,
getAll, getAll,
get, get,
deleteDoc, deleteDoc,
update,
}; };
...@@ -9,6 +9,7 @@ const router: Router = express.Router(); ...@@ -9,6 +9,7 @@ const router: Router = express.Router();
router.get('/', controller.getAll(model)); router.get('/', controller.getAll(model));
router.post('/', controller.create(model)); router.post('/', controller.create(model));
router.get('/:id', controller.get(model)); router.get('/:id', controller.get(model));
router.put('/:id', controller.update(model));
router.delete('/:id', (req: Request, res: Response) => { router.delete('/:id', (req: Request, res: Response) => {
try { try {
......
...@@ -8,6 +8,8 @@ const router: Router = express.Router(); ...@@ -8,6 +8,8 @@ const router: Router = express.Router();
router.get('/', controller.getAll(model)); router.get('/', controller.getAll(model));
router.post('/', controller.create(model)); router.post('/', controller.create(model));
router.get('/:id', controller.get(model)); router.get('/:id', controller.get(model));
router.put('/:id', controller.update(model));
router.delete('/:id', (req: Request, res: Response) => { router.delete('/:id', (req: Request, res: Response) => {
try { try {
const id = req.params.id; const id = req.params.id;
......
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