diff --git a/backend/app/routes/testimonial/deleteTestimonial.js b/backend/app/routes/testimonial/deleteTestimonial.js new file mode 100644 index 00000000..4f5ad91e --- /dev/null +++ b/backend/app/routes/testimonial/deleteTestimonial.js @@ -0,0 +1,27 @@ +const to = require('await-to-js').default; +const Testimonial = require('../../models/Testimonial'); +const { ErrorHandler } = require('../../../helpers/error'); +const constants = require('../../../constants'); + +module.exports = async (req, res, next) => { + const [err, testimonial] = await to(Testimonial.findByIdAndDelete(req.params.id)); + if (!testimonial) { + const error = new ErrorHandler(constants.ERRORS.INPUT, { + statusCode: 400, + message: "Testimonial doesn't exist", + }); + return next(error); + } + if (err) { + const error = new ErrorHandler(constants.ERRORS.DATABASE, { + statusCode: 500, + message: 'Mongo Error: Deletion Failed', + errStack: err, + }); + return next(error); + } + res.status(200).send({ + message: 'Testimonial deleted successfully', + }); + return next(); +}; diff --git a/backend/app/routes/testimonial/index.js b/backend/app/routes/testimonial/index.js index 5708c755..86e44f23 100644 --- a/backend/app/routes/testimonial/index.js +++ b/backend/app/routes/testimonial/index.js @@ -5,9 +5,10 @@ const { authMiddleware } = require('../../../helpers/middlewares/auth'); const { postTestimonialValidationSchema } = require('./@validationSchema'); const postTestimonial = require('./postTestimonial'); const getTestimonials = require('./getTestimonials'); +const deleteTestimonial = require('./deleteTestimonial'); router.post('/', validationMiddleware(postTestimonialValidationSchema), authMiddleware, postTestimonial); router.get('/getTestimonials', getTestimonials); - +router.delete('/:id', authMiddleware, deleteTestimonial); module.exports = router;