Files
hamid.zarghami1@gmail.com e644edbd65 transfer project in github
2024-05-12 15:29:27 +03:30

65 lines
1.7 KiB
JavaScript

const ContactUs = require('../models/ContactUs')
const {body, validationResult} = require('express-validator')
const {res404, res500} = require('../plugins/controllersHelperFunctions')
const {_sr} = require('../plugins/serverResponses')
const limit = 20
module.exports.create = [
[
body('name')
.notEmpty().withMessage(_sr['fa'].required.name),
body('phone')
.notEmpty().withMessage(_sr['fa'].required.phone_number)
.bail()
.isNumeric().withMessage(_sr['fa'].format.phone_number),
body('email')
.notEmpty().withMessage(_sr['fa'].required.email)
.bail()
.isEmail().withMessage(_sr['fa'].format.email),
body('message')
.notEmpty().withMessage(_sr['fa'].required.message)
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
const {name, phone, email, message} = req.body
const data = {
name, phone, email, message, read: false
}
new ContactUs(data).save((err, data) => {
if (err) return res500(res, err)
return res.json({message: _sr['fa'].response.success_save})
})
}
]
module.exports.getAll = [
(req, res) => {
const filter = {}
ContactUs.paginate(filter, {
page: req.query.page || 1,
limit,
sort : {index : 1}
} , (err, data) => {
if (err) return res500(res, err)
return res.json(data)
})
}
]
module.exports.getOne = [
(req, res) => {
ContactUs.findById(req.params.id, (err, data) => {
if (err || !data) return res404(res, err)
data.read = true
data.save(err => {
if (err) console.log(err)
})
return res.json(data)
})
}
]