update:add ci cd files ==> do not edit those file
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
const Team = require('../models/Team')
|
||||
const {body, validationResult} = require('express-validator')
|
||||
const {_sr} = require('../plugins/serverResponses')
|
||||
const {res404, res500, checkValidations} = require('../plugins/controllersHelperFunctions')
|
||||
const sharp = require('sharp')
|
||||
const fs = require('fs')
|
||||
|
||||
// variables
|
||||
const imageWidth = 400
|
||||
const imageHeight = 500
|
||||
const imageQuality = 100
|
||||
|
||||
|
||||
module.exports.create = [
|
||||
[
|
||||
body('index')
|
||||
.notEmpty().withMessage('شماره ترتیب را انتخاب کنید'),
|
||||
|
||||
body('fa_first_name')
|
||||
.notEmpty().withMessage(_sr['fa'].required.first_name),
|
||||
|
||||
body('en_first_name')
|
||||
.notEmpty().withMessage(_sr['fa'].required.first_name),
|
||||
|
||||
body('fa_last_name')
|
||||
.notEmpty().withMessage(_sr['fa'].required.last_name),
|
||||
|
||||
body('en_last_name')
|
||||
.notEmpty().withMessage(_sr['fa'].required.last_name),
|
||||
|
||||
body('fa_position')
|
||||
.notEmpty().withMessage('سمت را بنویسید'),
|
||||
|
||||
body('en_position')
|
||||
.notEmpty().withMessage('سمت را بنویسید')
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
if (!req.files || !req.files.image) return res.status(422).json({validation: {image: {msg: _sr['fa'].required.image}}})
|
||||
|
||||
const {fa_first_name, en_first_name, fa_last_name, en_last_name, fa_position, en_position, index} = req.body
|
||||
const data = {
|
||||
locale: {
|
||||
fa: {
|
||||
first_name: fa_first_name,
|
||||
last_name: fa_last_name,
|
||||
position: fa_position
|
||||
},
|
||||
en: {
|
||||
first_name: en_first_name,
|
||||
last_name: en_last_name,
|
||||
position: en_position
|
||||
}
|
||||
},
|
||||
index
|
||||
}
|
||||
|
||||
const image = req.files.image
|
||||
const imageName = 'team_' + en_first_name + '_' + 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/team/${imageName}`)
|
||||
|
||||
} catch (e) {
|
||||
return res500(res, e)
|
||||
}
|
||||
|
||||
const teamMate = new Team(data)
|
||||
teamMate.save(err => {
|
||||
if (err) return res500(res, err)
|
||||
else return res.json({message: _sr['fa'].response.success_save})
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.getAll = [
|
||||
(req, res) => {
|
||||
Team.find({}).sort({index: 1}).exec((err, data) => {
|
||||
if (err) return res500(res, err)
|
||||
else return res.json(data)
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.getOne = [
|
||||
(req, res) => {
|
||||
Team.findById(req.params.id, (err, data) => {
|
||||
if (err || !data) return res404(res, err)
|
||||
else return res.json(data)
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.update = [
|
||||
[
|
||||
body('index')
|
||||
.notEmpty().withMessage('شماره ترتیب را انتخاب کنید'),
|
||||
|
||||
body('fa_first_name')
|
||||
.notEmpty().withMessage(_sr['fa'].required.first_name),
|
||||
|
||||
body('en_first_name')
|
||||
.notEmpty().withMessage(_sr['fa'].required.first_name),
|
||||
|
||||
body('en_last_name')
|
||||
.notEmpty().withMessage(_sr['fa'].required.last_name),
|
||||
|
||||
body('en_last_name')
|
||||
.notEmpty().withMessage(_sr['fa'].required.last_name),
|
||||
|
||||
body('fa_position')
|
||||
.notEmpty().withMessage('سمت را بنویسید'),
|
||||
|
||||
body('en_position')
|
||||
.notEmpty().withMessage('سمت را بنویسید')
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
const {fa_first_name, en_first_name, fa_last_name, en_last_name, fa_position, en_position, index} = req.body
|
||||
const data = {
|
||||
locale: {
|
||||
fa: {
|
||||
first_name: fa_first_name,
|
||||
last_name: fa_last_name,
|
||||
position: fa_position
|
||||
},
|
||||
en: {
|
||||
first_name: en_first_name,
|
||||
last_name: en_last_name,
|
||||
position: en_position
|
||||
}
|
||||
},
|
||||
index
|
||||
}
|
||||
|
||||
if (req.files && req.files.image) {
|
||||
const image = req.files.image
|
||||
const imageName = 'team_' + en_first_name + '_' + 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/team/${imageName}`)
|
||||
|
||||
} catch (e) {
|
||||
return res500(res, e)
|
||||
}
|
||||
}
|
||||
|
||||
Team.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
|
||||
if (err || !oldData) return res404(res, err)
|
||||
if (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) => {
|
||||
Team.findByIdAndRemove(req.params.id, (err, data) => {
|
||||
if (err || !data) return res404(res, err)
|
||||
fs.unlink(`./static/${data.image}`, err => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
return res.json({message: _sr['fa'].response.success_remove})
|
||||
})
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user