-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π [Backend] Create POST Testimonial API (#1043)
- Loading branch information
1 parent
66c50b2
commit e09bbe7
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
router.get('/getTestimonials', getTestimonials); | ||
|
||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |