Skip to content

Commit

Permalink
Create POST Testimonial API
Browse files Browse the repository at this point in the history
  • Loading branch information
shivamgaur99 committed Jun 11, 2024
1 parent ce35ae3 commit 56bcd55
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
22 changes: 22 additions & 0 deletions backend/app/routes/testimonial/@validationSchema/index.js
Original file line number Diff line number Diff line change
@@ -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,
};
6 changes: 6 additions & 0 deletions backend/app/routes/testimonial/index.js
Original file line number Diff line number Diff line change
@@ -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);

Check failure

Code scanning / CodeQL

Missing rate limiting High test

This route handler performs
authorization
, but is not rate-limited.
router.get('/getTestimonials', getTestimonials);


module.exports = router;
21 changes: 21 additions & 0 deletions backend/app/routes/testimonial/postTestimonial.js
Original file line number Diff line number Diff line change
@@ -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();
};

0 comments on commit 56bcd55

Please sign in to comment.