diff --git a/backend/app/routes/testimonial/@validationSchema/index.js b/backend/app/routes/testimonial/@validationSchema/index.js new file mode 100644 index 00000000..08a62101 --- /dev/null +++ b/backend/app/routes/testimonial/@validationSchema/index.js @@ -0,0 +1,22 @@ +const Joi = require('joi'); + +const postTestimonialValidationSchema = Joi.object().keys({ + name: Joi.string().required(), + position: Joi.string().required(), + company: Joi.string().required(), + image: Joi.string().uri().required(), + text: Joi.string().required(), + rating: Joi.number().min(1).max(5).required(), +}); + +const getTestimonialsValidationSchema = Joi.object().keys({ + page: Joi.number().min(1).optional(), + company: Joi.string().optional(), + year: Joi.number().optional(), + month: Joi.number().min(1).max(12).optional(), +}); + +module.exports = { + postTestimonialValidationSchema, + getTestimonialsValidationSchema, +}; diff --git a/backend/app/routes/testimonial/index.js b/backend/app/routes/testimonial/index.js index 189481d0..5708c755 100644 --- a/backend/app/routes/testimonial/index.js +++ b/backend/app/routes/testimonial/index.js @@ -1,7 +1,13 @@ const router = require('express').Router({ mergeParams: true }); +const validationMiddleware = require('../../../helpers/middlewares/validation'); +const { authMiddleware } = require('../../../helpers/middlewares/auth'); +const { postTestimonialValidationSchema } = require('./@validationSchema'); +const postTestimonial = require('./postTestimonial'); const getTestimonials = require('./getTestimonials'); +router.post('/', validationMiddleware(postTestimonialValidationSchema), authMiddleware, postTestimonial); router.get('/getTestimonials', getTestimonials); + module.exports = router; diff --git a/backend/app/routes/testimonial/postTestimonial.js b/backend/app/routes/testimonial/postTestimonial.js new file mode 100644 index 00000000..bda23b77 --- /dev/null +++ b/backend/app/routes/testimonial/postTestimonial.js @@ -0,0 +1,21 @@ +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, { _id }] = await to(Testimonial.create({ ...req.body })); + if (err) { + const error = new ErrorHandler(constants.ERRORS.DATABASE, { + statusCode: 500, + message: 'Mongo Error: Insertion Failed', + errStack: err, + }); + return next(error); + } + res.status(200).send({ + message: 'Testimonial added successfully', + id: _id, + }); + return next(); +};