From e09bbe7ef6f74fad51540b9d18f7df4c7dc59ca9 Mon Sep 17 00:00:00 2001 From: Shivam Gaur <128178418+shivamgaur99@users.noreply.github.com> Date: Wed, 12 Jun 2024 21:54:39 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=8D=81=20[Backend]=20Create=20POST=20Test?= =?UTF-8?q?imonial=20API=20(#1043)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../testimonial/@validationSchema/index.js | 22 +++++++++++++++++++ backend/app/routes/testimonial/index.js | 6 +++++ .../app/routes/testimonial/postTestimonial.js | 21 ++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 backend/app/routes/testimonial/@validationSchema/index.js create mode 100644 backend/app/routes/testimonial/postTestimonial.js 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(); +};