Files
orisoxin/server/controllers/customerCommentController.js
T
2024-10-10 21:57:37 +03:30

185 lines
5.2 KiB
JavaScript

const CustomerComment = require('../models/CustomerComment')
const sharp = require('sharp')
const fs = require('fs')
const {body, validationResult} = require('express-validator')
const {res404, res500, checkValidations} = require('../plugins/controllersHelperFunctions')
const {_sr} = require('../plugins/serverResponses')
// variables
const imageWidth = 350
const imageHeight = 350
const imageQuality = 100
module.exports.create = [
[
body('fa_name')
.notEmpty().withMessage((value, {req}) => _sr['fa'].required.name)
.bail()
.isLength({min: 2}).withMessage((value, {req}) => _sr['fa'].min_char.min2),
body('en_name')
.notEmpty().withMessage((value, {req}) => _sr['fa'].required.name)
.bail()
.isLength({min: 2}).withMessage((value, {req}) => _sr['fa'].min_char.min2),
body('fa_comment')
.notEmpty().withMessage((value, {req}) => _sr['fa'].required.caption)
.bail()
.isLength({min: 2}).withMessage((value, {req}) => _sr['fa'].min_char.min2),
body('en_comment')
.notEmpty().withMessage((value, {req}) => _sr['fa'].required.caption)
.bail()
.isLength({min: 2}).withMessage((value, {req}) => _sr['fa'].min_char.min2)
],
checkValidations(validationResult),
async (req, res) => {
const {fa_name, en_name, fa_position, en_position, fa_comment, en_comment} = req.body
const data = {
locale: {
fa: {
name: fa_name,
position: fa_position,
comment: fa_comment
},
en: {
name: en_name,
position: en_position,
comment: en_comment
}
}
}
if (req.files && req.files.image) {
const image = req.files.image
const imageName = 'customer_' + Date.now() + '.jpg'
data.image = imageName
try {
const target = await sharp(image.data)
await target
.resize(imageWidth, imageHeight, {fit: 'cover'})
.withMetadata()
.toFormat('jpg')
.jpeg({quality: imageQuality})
.toFile(`./static/uploads/images/customer/${imageName}`)
} catch (e) {
return res500(res, e)
}
}
new CustomerComment(data).save((err, data) => {
if (err) return res500(res, err)
return res.json({message: _sr['fa'].response.success_save})
})
}
]
module.exports.getAll = [
(req, res) => {
CustomerComment.find({}, (err, data) => {
if (err) return res500(res, err)
return res.json(data)
})
}
]
module.exports.getOne = [
(req, res) => {
CustomerComment.findById(req.params.id, (err, data) => {
if (err || !data) return res404(res, err)
else return res.json(data)
})
}
]
module.exports.update = [
[
body('fa_name')
.notEmpty().withMessage((value, {req}) => _sr['fa'].required.name)
.bail()
.isLength({min: 2}).withMessage((value, {req}) => _sr['fa'].min_char.min2),
body('en_name')
.notEmpty().withMessage((value, {req}) => _sr['fa'].required.name)
.bail()
.isLength({min: 2}).withMessage((value, {req}) => _sr['fa'].min_char.min2),
body('fa_comment')
.notEmpty().withMessage((value, {req}) => _sr['fa'].required.caption)
.bail()
.isLength({min: 2}).withMessage((value, {req}) => _sr['fa'].min_char.min2),
body('en_comment')
.notEmpty().withMessage((value, {req}) => _sr['fa'].required.caption)
.bail()
.isLength({min: 2}).withMessage((value, {req}) => _sr['fa'].min_char.min2)
],
checkValidations(validationResult),
async (req, res) => {
const {fa_name, en_name, fa_position, en_position, fa_comment, en_comment} = req.body
const data = {
locale: {
fa: {
name: fa_name,
position: fa_position,
comment: fa_comment
},
en: {
name: en_name,
position: en_position,
comment: en_comment
}
}
}
if (req.files && req.files.image) {
const image = req.files.image
const imageName = 'customer_' + Date.now() + '.jpg'
data.image = imageName
try {
const target = await sharp(image.data)
await target
.resize(imageWidth, imageHeight, {fit: 'cover'})
.withMetadata()
.toFormat('jpg')
.jpeg({quality: imageQuality})
.toFile(`./static/uploads/images/customer/${imageName}`)
} catch (e) {
return res500(res, e)
}
}
CustomerComment.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err || !oldData) return res404(res, err)
if (oldData.image && req.files && req.files.image) {
fs.unlink(`./static/${oldData.image}`, err => {
if (err) console.log(err)
})
}
return res.json({message: _sr['fa'].response.success_save})
})
}
]
module.exports.delete = [
(req, res) => {
CustomerComment.findByIdAndRemove(req.params.id, (err, oldData) => {
if (err || !oldData) return res404(res, err)
if (oldData.image) {
fs.unlink(`./static/${oldData.image}`, err => {
if (err) console.log(err)
})
}
return res.json({message: _sr['fa'].response.success_remove})
})
}
]