front-end almost done | started back-end
This commit is contained in:
@@ -0,0 +1,385 @@
|
||||
const AboutPageContent = require('../models/AboutPageContent')
|
||||
const dateformat = require('dateformat')
|
||||
const {body, validationResult} = require('express-validator')
|
||||
const jimp = require('jimp')
|
||||
const fs = require('fs')
|
||||
|
||||
module.exports.create = [
|
||||
[
|
||||
// fa
|
||||
body('fa_page_title')
|
||||
.notEmpty().withMessage('تیتر بالای صفحه نباید خالی باشد.'),
|
||||
body('fa_s1_title')
|
||||
.notEmpty().withMessage('تیتر بخش اول نباید خالی باشد.'),
|
||||
body('fa_s1_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش اول نباید خالی باشد.'),
|
||||
body('fa_s2_title')
|
||||
.notEmpty().withMessage('تیتر بخش دوم نباید خالی باشد.'),
|
||||
body('fa_s2_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش دوم نباید خالی باشد.'),
|
||||
body('fa_s2_imageCaption')
|
||||
.notEmpty().withMessage('توضیحات عکس بخش دوم نباید خالی باشد.'),
|
||||
body('fa_s3_title')
|
||||
.notEmpty().withMessage('تیتر بخش سوم نباید خالی باشد.'),
|
||||
body('fa_s3_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش سوم نباید خالی باشد.'),
|
||||
body('fa_s3_imageCaption')
|
||||
.notEmpty().withMessage('توضیحات عکس بخش سوم نباید خالی باشد.'),
|
||||
body('fa_s4_text')
|
||||
.notEmpty().withMessage('متن بخش چهارم نباید خالی باشد.'),
|
||||
body('fa_last_section_title')
|
||||
.notEmpty().withMessage('تیتر بخش آخر نباید خالی باشد.'),
|
||||
body('fa_last_section_text')
|
||||
.notEmpty().withMessage('متن بخش آخر نباید خالی باشد.'),
|
||||
|
||||
// en
|
||||
body('en_page_title')
|
||||
.notEmpty().withMessage('تیتر بالای صفحه نباید خالی باشد.'),
|
||||
body('en_s1_title')
|
||||
.notEmpty().withMessage('تیتر بخش اول نباید خالی باشد.'),
|
||||
body('en_s1_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش اول نباید خالی باشد.'),
|
||||
body('en_s2_title')
|
||||
.notEmpty().withMessage('تیتر بخش دوم نباید خالی باشد.'),
|
||||
body('en_s2_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش دوم نباید خالی باشد.'),
|
||||
body('en_s2_imageCaption')
|
||||
.notEmpty().withMessage('توضیحات عکس بخش دوم نباید خالی باشد.'),
|
||||
body('en_s3_title')
|
||||
.notEmpty().withMessage('تیتر بخش سوم نباید خالی باشد.'),
|
||||
body('en_s3_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش سوم نباید خالی باشد.'),
|
||||
body('en_s3_imageCaption')
|
||||
.notEmpty().withMessage('توضیحات عکس بخش سوم نباید خالی باشد.'),
|
||||
body('en_s4_text')
|
||||
.notEmpty().withMessage('متن بخش چهارم نباید خالی باشد.'),
|
||||
body('en_last_section_title')
|
||||
.notEmpty().withMessage('تیتر بخش آخر نباید خالی باشد.'),
|
||||
body('en_last_section_text')
|
||||
.notEmpty().withMessage('متن بخش آخر نباید خالی باشد.'),
|
||||
|
||||
// de
|
||||
body('de_page_title')
|
||||
.notEmpty().withMessage('تیتر بالای صفحه نباید خالی باشد.'),
|
||||
body('de_s1_title')
|
||||
.notEmpty().withMessage('تیتر بخش اول نباید خالی باشد.'),
|
||||
body('de_s1_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش اول نباید خالی باشد.'),
|
||||
body('de_s2_title')
|
||||
.notEmpty().withMessage('تیتر بخش دوم نباید خالی باشد.'),
|
||||
body('de_s2_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش دوم نباید خالی باشد.'),
|
||||
body('de_s2_imageCaption')
|
||||
.notEmpty().withMessage('توضیحات عکس بخش دوم نباید خالی باشد.'),
|
||||
body('de_s3_title')
|
||||
.notEmpty().withMessage('تیتر بخش سوم نباید خالی باشد.'),
|
||||
body('de_s3_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش سوم نباید خالی باشد.'),
|
||||
body('de_s3_imageCaption')
|
||||
.notEmpty().withMessage('توضیحات عکس بخش سوم نباید خالی باشد.'),
|
||||
body('de_s4_text')
|
||||
.notEmpty().withMessage('متن بخش چهارم نباید خالی باشد.'),
|
||||
body('de_last_section_title')
|
||||
.notEmpty().withMessage('تیتر بخش آخر نباید خالی باشد.'),
|
||||
body('de_last_section_text')
|
||||
.notEmpty().withMessage('متن بخش آخر نباید خالی باشد.'),
|
||||
|
||||
// tr
|
||||
body('tr_page_title')
|
||||
.notEmpty().withMessage('تیتر بالای صفحه نباید خالی باشد.'),
|
||||
body('tr_s1_title')
|
||||
.notEmpty().withMessage('تیتر بخش اول نباید خالی باشد.'),
|
||||
body('tr_s1_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش اول نباید خالی باشد.'),
|
||||
body('tr_s2_title')
|
||||
.notEmpty().withMessage('تیتر بخش دوم نباید خالی باشد.'),
|
||||
body('tr_s2_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش دوم نباید خالی باشد.'),
|
||||
body('tr_s2_imageCaption')
|
||||
.notEmpty().withMessage('توضیحات عکس بخش دوم نباید خالی باشد.'),
|
||||
body('tr_s3_title')
|
||||
.notEmpty().withMessage('تیتر بخش سوم نباید خالی باشد.'),
|
||||
body('tr_s3_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش سوم نباید خالی باشد.'),
|
||||
body('tr_s3_imageCaption')
|
||||
.notEmpty().withMessage('توضیحات عکس بخش سوم نباید خالی باشد.'),
|
||||
body('tr_s4_text')
|
||||
.notEmpty().withMessage('متن بخش چهارم نباید خالی باشد.'),
|
||||
body('tr_last_section_title')
|
||||
.notEmpty().withMessage('تیتر بخش آخر نباید خالی باشد.'),
|
||||
body('tr_last_section_text')
|
||||
.notEmpty().withMessage('متن بخش آخر نباید خالی باشد.'),
|
||||
|
||||
// it
|
||||
body('it_page_title')
|
||||
.notEmpty().withMessage('تیتر بالای صفحه نباید خالی باشد.'),
|
||||
body('it_s1_title')
|
||||
.notEmpty().withMessage('تیتر بخش اول نباید خالی باشد.'),
|
||||
body('it_s1_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش اول نباید خالی باشد.'),
|
||||
body('it_s2_title')
|
||||
.notEmpty().withMessage('تیتر بخش دوم نباید خالی باشد.'),
|
||||
body('it_s2_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش دوم نباید خالی باشد.'),
|
||||
body('it_s2_imageCaption')
|
||||
.notEmpty().withMessage('توضیحات عکس بخش دوم نباید خالی باشد.'),
|
||||
body('it_s3_title')
|
||||
.notEmpty().withMessage('تیتر بخش سوم نباید خالی باشد.'),
|
||||
body('it_s3_caption')
|
||||
.notEmpty().withMessage('توضیحات بخش سوم نباید خالی باشد.'),
|
||||
body('it_s3_imageCaption')
|
||||
.notEmpty().withMessage('توضیحات عکس بخش سوم نباید خالی باشد.'),
|
||||
body('it_s4_text')
|
||||
.notEmpty().withMessage('متن بخش چهارم نباید خالی باشد.'),
|
||||
body('it_last_section_title')
|
||||
.notEmpty().withMessage('تیتر بخش آخر نباید خالی باشد.'),
|
||||
body('it_last_section_text')
|
||||
.notEmpty().withMessage('متن بخش آخر نباید خالی باشد.'),
|
||||
|
||||
],
|
||||
(req, res) => {
|
||||
// check validations
|
||||
const errors = validationResult(req)
|
||||
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
|
||||
|
||||
|
||||
////////////////// catch form data
|
||||
const data = {
|
||||
aboutPage_details: {
|
||||
fa: {
|
||||
page_title: req.body.fa_page_title,
|
||||
s1_title: req.body.fa_s1_title,
|
||||
s1_caption: req.body.fa_s1_caption,
|
||||
s2_title: req.body.fa_s2_title,
|
||||
s2_caption: req.body.fa_s2_caption,
|
||||
s2_imageCaption: req.body.fa_s2_imageCaption,
|
||||
s3_title: req.body.fa_s3_title,
|
||||
s3_caption: req.body.fa_s3_caption,
|
||||
s3_imageCaption: req.body.fa_s3_imageCaption,
|
||||
s4_text: req.body.fa_s4_text,
|
||||
last_section_title: req.body.fa_last_section_title,
|
||||
last_section_text: req.body.fa_last_section_text
|
||||
},
|
||||
en: {
|
||||
page_title: req.body.en_page_title,
|
||||
s1_title: req.body.en_s1_title,
|
||||
s1_caption: req.body.en_s1_caption,
|
||||
s2_title: req.body.en_s2_title,
|
||||
s2_caption: req.body.en_s2_caption,
|
||||
s2_imageCaption: req.body.en_s2_imageCaption,
|
||||
s3_title: req.body.en_s3_title,
|
||||
s3_caption: req.body.en_s3_caption,
|
||||
s3_imageCaption: req.body.en_s3_imageCaption,
|
||||
s4_text: req.body.en_s4_text,
|
||||
last_section_title: req.body.en_last_section_title,
|
||||
last_section_text: req.body.en_last_section_text
|
||||
},
|
||||
de: {
|
||||
page_title: req.body.de_page_title,
|
||||
s1_title: req.body.de_s1_title,
|
||||
s1_caption: req.body.de_s1_caption,
|
||||
s2_title: req.body.de_s2_title,
|
||||
s2_caption: req.body.de_s2_caption,
|
||||
s2_imageCaption: req.body.de_s2_imageCaption,
|
||||
s3_title: req.body.de_s3_title,
|
||||
s3_caption: req.body.de_s3_caption,
|
||||
s3_imageCaption: req.body.de_s3_imageCaption,
|
||||
s4_text: req.body.de_s4_text,
|
||||
last_section_title: req.body.de_last_section_title,
|
||||
last_section_text: req.body.de_last_section_text
|
||||
},
|
||||
tr: {
|
||||
page_title: req.body.tr_page_title,
|
||||
s1_title: req.body.tr_s1_title,
|
||||
s1_caption: req.body.tr_s1_caption,
|
||||
s2_title: req.body.tr_s2_title,
|
||||
s2_caption: req.body.tr_s2_caption,
|
||||
s2_imageCaption: req.body.tr_s2_imageCaption,
|
||||
s3_title: req.body.tr_s3_title,
|
||||
s3_caption: req.body.tr_s3_caption,
|
||||
s3_imageCaption: req.body.tr_s3_imageCaption,
|
||||
s4_text: req.body.tr_s4_text,
|
||||
last_section_title: req.body.tr_last_section_title,
|
||||
last_section_text: req.body.tr_last_section_text
|
||||
},
|
||||
it: {
|
||||
page_title: req.body.it_page_title,
|
||||
s1_title: req.body.it_s1_title,
|
||||
s1_caption: req.body.it_s1_caption,
|
||||
s2_title: req.body.it_s2_title,
|
||||
s2_caption: req.body.it_s2_caption,
|
||||
s2_imageCaption: req.body.it_s2_imageCaption,
|
||||
s3_title: req.body.it_s3_title,
|
||||
s3_caption: req.body.it_s3_caption,
|
||||
s3_imageCaption: req.body.it_s3_imageCaption,
|
||||
s4_text: req.body.it_s4_text,
|
||||
last_section_title: req.body.it_last_section_title,
|
||||
last_section_text: req.body.it_last_section_text
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function createImages(image, imageName) {
|
||||
if (image) {
|
||||
jimp.read(image.data)
|
||||
.then(img => {
|
||||
img
|
||||
.cover(380, 510)
|
||||
.write(`./static/uploads/images/about/${imageName}`, (err, file) => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let heroFile
|
||||
let heroFileName
|
||||
let s2File
|
||||
let s2FileName
|
||||
let s3File
|
||||
let s3FileName
|
||||
let lastSectionFile
|
||||
let lastSectionFileName
|
||||
|
||||
if (req.files) {
|
||||
if (req.files.page_hero) {
|
||||
heroFile = req.files.page_hero
|
||||
heroFileName = 'image_' + Date.now() + 1 + '.' + heroFile.mimetype.split('/')[1]
|
||||
}
|
||||
if (req.files.s2_image) {
|
||||
s2File = req.files.s2_image
|
||||
s2FileName = 'image_' + Date.now() + 2 + '.' + s2File.mimetype.split('/')[1]
|
||||
}
|
||||
if (req.files.s3_image) {
|
||||
s3File = req.files.s3_image
|
||||
s3FileName = 'image_' + Date.now() + 3 + '.' + s3File.mimetype.split('/')[1]
|
||||
}
|
||||
if (req.files.last_section_image) {
|
||||
lastSectionFile = req.files.last_section_image
|
||||
lastSectionFileName = 'image_' + Date.now() + 4 + '.' + lastSectionFile.mimetype.split('/')[1]
|
||||
}
|
||||
}
|
||||
|
||||
/////////////// save to database
|
||||
if (req.body._id) {
|
||||
if (req.files) {
|
||||
if (heroFile) {
|
||||
if (heroFile.size / 1024 > 500) return res.status(422).json({validation: {page_hero: {msg: 'حجم عکس نباید بیشتر از 500 کیلوبایت باشد.'}}})
|
||||
data.page_hero = heroFileName
|
||||
heroFile.mv(`./static/uploads/images/about/${heroFileName}`, err => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
}
|
||||
|
||||
if (s2File) {
|
||||
data.s2_image = s2FileName
|
||||
createImages(s2File, s2FileName)
|
||||
}
|
||||
|
||||
if (s3File) {
|
||||
data.s3_image = s3FileName
|
||||
createImages(s3File, s3FileName)
|
||||
}
|
||||
|
||||
if (lastSectionFile) {
|
||||
if (lastSectionFile.size / 1024 > 500) return res.status(422).json({validation: {last_section_image: {msg: 'حجم عکس نباید بیشتر از 500 کیلوبایت باشد.'}}})
|
||||
data.last_section_image = lastSectionFileName
|
||||
lastSectionFile.mv(`./static/uploads/images/about/${lastSectionFileName}`, err => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
data.updated_at = dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
|
||||
AboutPageContent.findByIdAndUpdate(req.body._id, data, (err, oldData) => {
|
||||
if (err) console.log(err)
|
||||
if (req.files) {
|
||||
if (heroFile) {
|
||||
fs.unlink(`./static/${oldData.page_hero}`, err => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
}
|
||||
if (s2File) {
|
||||
fs.unlink(`./static/${oldData.s2_image}`, err => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
}
|
||||
if (s3File) {
|
||||
fs.unlink(`./static/${oldData.s3_image}`, err => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
}
|
||||
if (lastSectionFile) {
|
||||
fs.unlink(`./static/${oldData.last_section_image}`, err => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
AboutPageContent.findByIdAndUpdate(req.body._id)
|
||||
.then(data => {
|
||||
return res.json(data)
|
||||
})
|
||||
})
|
||||
} else {
|
||||
if (req.files) {
|
||||
if (heroFile) {
|
||||
if (heroFile.size / 1024 > 500) return res.status(422).json({validation: {page_hero: {msg: 'حجم عکس نباید بیشتر از 500 کیلوبایت باشد.'}}})
|
||||
data.page_hero = heroFileName
|
||||
heroFile.mv(`./static/uploads/images/about/${heroFileName}`, err => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
} else {
|
||||
return res.status(422).json({validation: {page_hero: {msg: 'عکس بخش اول اجباری است.'}}})
|
||||
}
|
||||
|
||||
if (s2File) {
|
||||
data.s2_image = s2FileName
|
||||
createImages(s2File, s2FileName)
|
||||
} else {
|
||||
return res.status(422).json({validation: {s2_image: {msg: 'عکس بخش دوم اجباری است.'}}})
|
||||
}
|
||||
|
||||
if (s3File) {
|
||||
data.s3_image = s3FileName
|
||||
createImages(s3File, s3FileName)
|
||||
} else {
|
||||
return res.status(422).json({validation: {s3_image: {msg: 'عکس بخش سوم اجباری است.'}}})
|
||||
}
|
||||
|
||||
if (lastSectionFile) {
|
||||
if (lastSectionFile.size / 1024 > 500) return res.status(422).json({validation: {last_section_image: {msg: 'حجم عکس نباید بیشتر از 500 کیلوبایت باشد.'}}})
|
||||
data.last_section_image = lastSectionFileName
|
||||
lastSectionFile.mv(`./static/uploads/images/about/${lastSectionFileName}`, err => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
} else {
|
||||
return res.status(422).json({validation: {last_section_image: {msg: 'عکس بخش آخر اجباری است.'}}})
|
||||
}
|
||||
} else {
|
||||
return res.status(422).json({
|
||||
validation: {
|
||||
page_hero: {msg: 'عکس بخش اول اجباری است.'},
|
||||
s2_image: {msg: 'عکس بخش دوم اجباری است.'},
|
||||
s3_image: {msg: 'عکس بخش سوم اجباری است.'},
|
||||
last_section_image: {msg: 'عکس بخش آخر اجباری است.'}
|
||||
}
|
||||
})
|
||||
}
|
||||
data.created_at = dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
|
||||
const aboutContent = new AboutPageContent(data)
|
||||
aboutContent.save((err, data) => {
|
||||
if (err) return res.status(500).json(err)
|
||||
return res.json(data)
|
||||
})
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.get = [
|
||||
(req, res) => {
|
||||
AboutPageContent.findOne({}, (err, data) => {
|
||||
if (err) return res.status(404).json({message: err})
|
||||
return res.json(data)
|
||||
})
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user