added delete method

parent b7d78e7b
...@@ -50,8 +50,26 @@ const get = ...@@ -50,8 +50,26 @@ const get =
}); });
}; };
const deleteDoc = (model: Model<any>) => (req: Request, res: Response) => {
const _id = req.params.id;
model
.findOneAndDelete<Document>({_id})
.then((result) => {
if (result) {
return res.status(200).send('Successfuly deleted');
} else {
return res.status(404).json({message: 'Not found'});
}
})
.catch((error) => {
console.log(error);
return res.status(500).json({error});
});
};
export default { export default {
create, create,
getAll, getAll,
get, get,
deleteDoc,
}; };
import express, {Router} from 'express'; import express, {Router, Request, Response} from 'express';
import {Document} from 'mongoose';
import controller from '../controllers/Generic'; import controller from '../controllers/Generic';
import model from '../models/Location'; import model from '../models/Location';
import productModel from '../models/Product';
const router: Router = express.Router(); const router: Router = express.Router();
...@@ -8,4 +10,37 @@ router.get('/', controller.getAll(model)); ...@@ -8,4 +10,37 @@ 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.delete('/:id', (req: Request, res: Response) => {
try {
const id = req.params.id;
if (!id.match(/^[0-9a-fA-F]{24}$/)) {
res.json({error: 'This is not a vaild object id'});
return;
}
productModel.find({location: id}).then((result) => {
if (!result.length) {
model
.findByIdAndDelete(id)
.then((result) => {
if (result) {
res.send(`Deleted ${result}`);
} else {
res.send(`Could not find document by this id`);
}
})
.catch((err) => {
console.log(err);
res.status(404).send('Not found');
});
} else {
res.json({error: 'Cannot delete since its used in products'});
}
});
} catch (err: unknown) {
console.log(err);
res.status(500).json({err});
}
});
export = router; export = router;
...@@ -10,6 +10,7 @@ const router: Router = express.Router(); ...@@ -10,6 +10,7 @@ const router: Router = express.Router();
router.get('/', controller.getAll(model, ['category', 'location'])); router.get('/', controller.getAll(model, ['category', 'location']));
router.get('/:id', controller.get(model, ['category', 'location'])); router.get('/:id', controller.get(model, ['category', 'location']));
router.delete('/:id', controller.deleteDoc(model));
router.post( router.post(
'/', '/',
......
import express, {Router} from 'express'; import express, {Router, Request, Response} from 'express';
import controller from '../controllers/Generic'; import controller from '../controllers/Generic';
import model from '../models/Category'; import model from '../models/Category';
import productModel from '../models/Product';
const router: Router = express.Router(); 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.delete('/:id', (req: Request, res: Response) => {
try {
const id = req.params.id;
if (!id.match(/^[0-9a-fA-F]{24}$/)) {
res.json({error: 'This is not a vaild object id'});
return;
}
productModel.find({category: id}).then((result) => {
if (!result.length) {
model
.findByIdAndDelete(id)
.then((result) => {
if (result) {
res.send(`Deleted ${result}`);
} else {
res.send(`Could not find document by this id`);
}
})
.catch((err) => {
console.log(err);
res.status(404).send('Not found');
});
} else {
res.json({error: 'Cannot delete since its used in products'});
}
});
} catch (err: unknown) {
console.log(err);
res.status(500).json({err});
}
});
export = router; export = router;
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