Files
arakrail/api/controllers/blogPostController.js
T
2020-11-23 19:39:26 +03:30

379 lines
15 KiB
JavaScript

const BlogPost = require('../models/blog/BlogPost')
const dateformat = require('dateformat')
const {body, validationResult} = require('express-validator')
const fs = require('fs')
const jimp = require('jimp')
module.exports.create = [
[
// title
body('fa_title')
.notEmpty().withMessage('عنوان نباید خالی باشد.')
.bail()
.isLength({max: 60}).withMessage('حداکثر 60 کاراکتر مجاز است.')
.bail()
.custom((value, {req}) => {
return BlogPost.findOne({'post_details.fa.title': value}).then(post => {
if (post) return Promise.reject('عنوان تکراری است.')
return true
})
}),
body('en_title')
.notEmpty().withMessage('عنوان نباید خالی باشد.')
.bail()
.isLength({max: 60}).withMessage('حداکثر 60 کاراکتر مجاز است.')
.bail()
.custom((value, {req}) => {
return BlogPost.findOne({'post_details.en.title': value}).then(post => {
if (post) return Promise.reject('عنوان تکراری است.')
return true
})
}),
body('de_title')
.notEmpty().withMessage('عنوان نباید خالی باشد.')
.bail()
.isLength({max: 60}).withMessage('حداکثر 60 کاراکتر مجاز است.')
.bail()
.custom((value, {req}) => {
return BlogPost.findOne({'post_details.de.title': value}).then(post => {
if (post) return Promise.reject('عنوان تکراری است.')
return true
})
}),
body('tr_title')
.notEmpty().withMessage('عنوان نباید خالی باشد.')
.bail()
.isLength({max: 60}).withMessage('حداکثر 60 کاراکتر مجاز است.')
.bail()
.custom((value, {req}) => {
return BlogPost.findOne({'post_details.tr.title': value}).then(post => {
if (post) return Promise.reject('عنوان تکراری است.')
return true
})
}),
body('it_title')
.notEmpty().withMessage('عنوان نباید خالی باشد.')
.bail()
.isLength({max: 60}).withMessage('حداکثر 60 کاراکتر مجاز است.')
.bail()
.custom((value, {req}) => {
return BlogPost.findOne({'post_details.it.title': value}).then(post => {
if (post) return Promise.reject('عنوان تکراری است.')
return true
})
}),
// description
body('fa_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
body('en_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
body('de_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
body('tr_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
body('it_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
// short description
body('fa_short_description')
.notEmpty().withMessage('توضیح کوتاه نباید خالی باشد.')
.bail()
.isLength({max: 120}).withMessage('حداکثر 120 کاراکتر مجاز است.'),
body('en_short_description')
.notEmpty().withMessage('توضیح کوتاه نباید خالی باشد.')
.bail()
.isLength({max: 120}).withMessage('حداکثر 120 کاراکتر مجاز است.'),
body('de_short_description')
.notEmpty().withMessage('توضیح کوتاه نباید خالی باشد.')
.bail()
.isLength({max: 120}).withMessage('حداکثر 120 کاراکتر مجاز است.'),
body('tr_short_description')
.notEmpty().withMessage('توضیح کوتاه نباید خالی باشد.')
.bail()
.isLength({max: 120}).withMessage('حداکثر 120 کاراکتر مجاز است.'),
body('it_short_description')
.notEmpty().withMessage('توضیح کوتاه نباید خالی باشد.')
.bail()
.isLength({max: 120}).withMessage('حداکثر 120 کاراکتر مجاز است.'),
body('category')
.notEmpty().withMessage('دسته بندی را انتخاب کنید.')
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
let cover
let coverName
// 305 X 170
// 1370 x 556
if (req.files && req.files.cover) {
cover = req.files.cover
coverName = 'blog_' + Date.now() + '.' + cover.mimetype.split('/')[1]
if (cover.size / 1024 > 500) return res.status(422).json({validation: {cover: {msg: 'حجم عکس نباید بیشتر از 500 کیلوبایت باشد.'}}})
} else {
return res.status(422).json({validation: {cover: {msg: 'کاور اجباری است.'}}})
}
jimp.read(cover.data)
.then(img => {
img
.cover(1370, 688)
.write(`./static/uploads/images/blog/${coverName}`)
.resize(305, jimp.AUTO)
.cover(305, 170)
.write(`./static/uploads/images/blog/thumb_${coverName}`)
})
const data = {
post_details: {
fa: {
title: req.body.fa_title,
description: req.body.fa_description,
short_description: req.body.fa_short_description
},
en: {
title: req.body.en_title,
description: req.body.en_description,
short_description: req.body.en_short_description
},
de: {
title: req.body.de_title,
description: req.body.de_description,
short_description: req.body.de_short_description
},
tr: {
title: req.body.tr_title,
description: req.body.tr_description,
short_description: req.body.tr_short_description
},
it: {
title: req.body.it_title,
description: req.body.it_description,
short_description: req.body.it_short_description
}
},
cover: coverName,
published: req.body.published,
category: req.body.category,
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
const post = new BlogPost(data)
post.save(err => {
if (err) console.log(err)
return res.json({message: 'پست با موفقیت اضافه شد.'})
})
}
]
module.exports.getAll = [
(req, res) => {
BlogPost.find({}).sort({_id: -1}).exec((err, posts) => {
if (err) console.log(err)
return res.json(posts)
})
}
]
module.exports.getOne = [
(req, res) => {
BlogPost.findById(req.params.id, (err, post) => {
if (err) console.log(err)
return res.json(post)
})
}
]
module.exports.update = [
[
// title
body('fa_title')
.notEmpty().withMessage('عنوان نباید خالی باشد.')
.bail()
.isLength({max: 60}).withMessage('حداکثر 60 کاراکتر مجاز است.')
.bail()
.custom((value, {req}) => {
return BlogPost.findOne({'post_details.fa.title': value}).then(post => {
if (post && post._id.toString() !== req.params.id) return Promise.reject('عنوان تکراری است.')
return true
})
}),
body('en_title')
.notEmpty().withMessage('عنوان نباید خالی باشد.')
.bail()
.isLength({max: 60}).withMessage('حداکثر 60 کاراکتر مجاز است.')
.bail()
.custom((value, {req}) => {
return BlogPost.findOne({'post_details.en.title': value}).then(post => {
if (post && post._id.toString() !== req.params.id) return Promise.reject('عنوان تکراری است.')
return true
})
}),
body('de_title')
.notEmpty().withMessage('عنوان نباید خالی باشد.')
.bail()
.isLength({max: 60}).withMessage('حداکثر 60 کاراکتر مجاز است.')
.bail()
.custom((value, {req}) => {
return BlogPost.findOne({'post_details.de.title': value}).then(post => {
if (post && post._id.toString() !== req.params.id) return Promise.reject('عنوان تکراری است.')
return true
})
}),
body('tr_title')
.notEmpty().withMessage('عنوان نباید خالی باشد.')
.bail()
.isLength({max: 60}).withMessage('حداکثر 60 کاراکتر مجاز است.')
.bail()
.custom((value, {req}) => {
return BlogPost.findOne({'post_details.tr.title': value}).then(post => {
if (post && post._id.toString() !== req.params.id) return Promise.reject('عنوان تکراری است.')
return true
})
}),
body('it_title')
.notEmpty().withMessage('عنوان نباید خالی باشد.')
.bail()
.isLength({max: 60}).withMessage('حداکثر 60 کاراکتر مجاز است.')
.bail()
.custom((value, {req}) => {
return BlogPost.findOne({'post_details.it.title': value}).then(post => {
if (post && post._id.toString() !== req.params.id) return Promise.reject('عنوان تکراری است.')
return true
})
}),
// description
body('fa_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
body('en_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
body('de_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
body('tr_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
body('it_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
// short description
body('fa_short_description')
.notEmpty().withMessage('توضیح کوتاه نباید خالی باشد.')
.bail()
.isLength({max: 120}).withMessage('حداکثر 120 کاراکتر مجاز است.'),
body('en_short_description')
.notEmpty().withMessage('توضیح کوتاه نباید خالی باشد.')
.bail()
.isLength({max: 120}).withMessage('حداکثر 120 کاراکتر مجاز است.'),
body('de_short_description')
.notEmpty().withMessage('توضیح کوتاه نباید خالی باشد.')
.bail()
.isLength({max: 120}).withMessage('حداکثر 120 کاراکتر مجاز است.'),
body('tr_short_description')
.notEmpty().withMessage('توضیح کوتاه نباید خالی باشد.')
.bail()
.isLength({max: 120}).withMessage('حداکثر 120 کاراکتر مجاز است.'),
body('it_short_description')
.notEmpty().withMessage('توضیح کوتاه نباید خالی باشد.')
.bail()
.isLength({max: 120}).withMessage('حداکثر 120 کاراکتر مجاز است.'),
body('category')
.notEmpty().withMessage('دسته بندی را انتخاب کنید.')
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
const data = {
post_details: {
fa: {
title: req.body.fa_title,
description: req.body.fa_description,
short_description: req.body.fa_short_description
},
en: {
title: req.body.en_title,
description: req.body.en_description,
short_description: req.body.en_short_description
},
de: {
title: req.body.de_title,
description: req.body.de_description,
short_description: req.body.de_short_description
},
tr: {
title: req.body.tr_title,
description: req.body.tr_description,
short_description: req.body.tr_short_description
},
it: {
title: req.body.it_title,
description: req.body.it_description,
short_description: req.body.it_short_description
}
},
published: req.body.published,
category: req.body.category,
updated_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
// 305 X 170
// 1370 x 556
let cover
let coverName
if (req.files && req.files.cover) {
cover = req.files.cover
coverName = 'blog_' + Date.now() + '.' + cover.mimetype.split('/')[1]
if (cover.size / 1024 > 500) return res.status(422).json({validation: {cover: {msg: 'حجم عکس نباید بیشتر از 500 کیلوبایت باشد.'}}})
data.cover = coverName
jimp.read(cover.data)
.then(img => {
img
.cover(1370, 688)
.write(`./static/uploads/images/blog/${coverName}`)
.resize(305, jimp.AUTO)
.cover(305, 170)
.write(`./static/uploads/images/blog/thumb_${coverName}`)
})
}
BlogPost.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
if (cover && cover.data) fs.unlink(`./static/${oldData.cover}`, err => {
if (err) console.log(err)
})
if (cover && cover.data) fs.unlink(`./static/${oldData.thumb}`, err => {
if (err) console.log(err)
})
return res.json({message: 'پست با موفقیت بروزرسانی شد.'})
})
}
]
module.exports.delete = [
(req, res) => {
BlogPost.findByIdAndDelete(req.params.id, (err, post) => {
if (err) return res.status(404).json({message: err})
fs.unlink(`./static/${post.cover}`, err => {
if (err) console.log(err)
})
fs.unlink(`./static/${post.thumb}`, err => {
if (err) console.log(err)
})
return res.json({message: 'پست حذف شد.'})
})
}
]