back-end done

This commit is contained in:
Amir Mohamadi
2020-12-10 16:36:48 +03:30
parent 80a7abafb7
commit bc5e617778
92 changed files with 5217 additions and 3379 deletions
+49 -185
View File
@@ -3,102 +3,48 @@ const dateformat = require('dateformat')
const {body, validationResult} = require('express-validator')
const fs = require('fs')
const jimp = require('jimp')
const v_m = require('../validation_messages')
module.exports.create = [
[
// title
body('fa_title')
.notEmpty().withMessage('عنوان نباید خالی باشد.')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.isLength({max: 60}).withMessage('حداکثر 60 کاراکتر مجاز است.')
.isLength({max: 120}).withMessage(v_m['fa'].max_char.max120)
.bail()
.custom((value, {req}) => {
return BlogPost.findOne({'post_details.fa.title': value}).then(post => {
if (post) return Promise.reject('عنوان تکراری است.')
if (post) return Promise.reject(v_m['fa'].duplicated.title)
return true
})
}),
body('en_title')
.notEmpty().withMessage('عنوان نباید خالی باشد.')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.isLength({max: 60}).withMessage('حداکثر 60 کاراکتر مجاز است.')
.isLength({max: 120}).withMessage(v_m['fa'].max_char.max120)
.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('عنوان تکراری است.')
if (post) return Promise.reject(v_m['fa'].duplicated.title)
return true
})
}),
// description
body('fa_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
.notEmpty().withMessage(v_m['fa'].required.description),
body('en_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
body('de_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
body('tr_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
body('it_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
.notEmpty().withMessage(v_m['fa'].required.description),
// short description
body('fa_short_description')
.notEmpty().withMessage('توضیح کوتاه نباید خالی باشد.')
.bail()
.isLength({max: 120}).withMessage('حداکثر 120 کاراکتر مجاز است.'),
.notEmpty().withMessage(v_m['fa'].required.description),
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 کاراکتر مجاز است.'),
.notEmpty().withMessage(v_m['fa'].required.description),
body('category')
.notEmpty().withMessage('دسته بندی را انتخاب کنید.')
.notEmpty().withMessage(v_m['fa'].required.category)
],
(req, res) => {
const errors = validationResult(req)
@@ -107,25 +53,24 @@ module.exports.create = [
let cover
let coverName
// 305 X 170
// 1370 x 556
// 460 X 320
// 1920 x 720
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: 'کاور اجباری است.'}}})
return res.status(422).json({validation: {cover: {msg: v_m['fa'].required.cover}}})
}
jimp.read(cover.data)
.then(img => {
img
.cover(1370, 688)
.cover(1920, 720)
.quality(70)
.write(`./static/uploads/images/blog/${coverName}`)
.resize(305, jimp.AUTO)
.cover(305, 170)
.resize(460, jimp.AUTO)
.cover(460, 320)
.write(`./static/uploads/images/blog/thumb_${coverName}`)
})
@@ -140,21 +85,6 @@ module.exports.create = [
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,
@@ -166,7 +96,7 @@ module.exports.create = [
const post = new BlogPost(data)
post.save(err => {
if (err) console.log(err)
return res.json({message: 'پست با موفقیت اضافه شد.'})
return res.json({message: v_m['fa'].response.success_save})
})
}
]
@@ -196,97 +126,44 @@ module.exports.update = [
[
// title
body('fa_title')
.notEmpty().withMessage('عنوان نباید خالی باشد.')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.isLength({max: 60}).withMessage('حداکثر 60 کاراکتر مجاز است.')
.isLength({max: 120}).withMessage(v_m['fa'].max_char.max120)
.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('عنوان تکراری است.')
if (post && post._id.toString() !== req.params.id) return Promise.reject(v_m['fa'].duplicated.title)
return true
})
}),
body('en_title')
.notEmpty().withMessage('عنوان نباید خالی باشد.')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.isLength({max: 60}).withMessage('حداکثر 60 کاراکتر مجاز است.')
.isLength({max: 120}).withMessage(v_m['fa'].max_char.max120)
.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('عنوان تکراری است.')
if (post && post._id.toString() !== req.params.id) return Promise.reject(v_m['fa'].duplicated.title)
return true
})
}),
// description
body('fa_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
.notEmpty().withMessage(v_m['fa'].required.description),
body('en_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
body('de_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
body('tr_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
body('it_description')
.notEmpty().withMessage('توضیحات نباید خالی باشد.'),
.notEmpty().withMessage(v_m['fa'].required.description),
// short description
body('fa_short_description')
.notEmpty().withMessage('توضیح کوتاه نباید خالی باشد.')
.bail()
.isLength({max: 120}).withMessage('حداکثر 120 کاراکتر مجاز است.'),
.notEmpty().withMessage(v_m['fa'].required.description),
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 کاراکتر مجاز است.'),
.notEmpty().withMessage(v_m['fa'].required.description),
body('category')
.notEmpty().withMessage('دسته بندی را انتخاب کنید.')
.notEmpty().withMessage(v_m['fa'].required.category)
],
(req, res) => {
const errors = validationResult(req)
@@ -304,21 +181,6 @@ module.exports.update = [
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,
@@ -326,8 +188,9 @@ module.exports.update = [
updated_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
// 305 X 170
// 1370 x 556
// 460 X 320
// 1920 x 720
let cover
let coverName
@@ -335,28 +198,29 @@ module.exports.update = [
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)
.cover(1920, 720)
.write(`./static/uploads/images/blog/${coverName}`)
.resize(305, jimp.AUTO)
.cover(305, 170)
.resize(460, jimp.AUTO)
.cover(460, 320)
.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: 'پست با موفقیت بروزرسانی شد.'})
if (cover && cover.data) {
fs.unlink(`./static/${oldData.cover}`, err => {
if (err) console.log(err)
})
fs.unlink(`./static/${oldData.thumb}`, err => {
if (err) console.log(err)
})
}
return res.json({message: v_m['fa'].response.success_save})
})
}
]
@@ -372,7 +236,7 @@ module.exports.delete = [
fs.unlink(`./static/${post.thumb}`, err => {
if (err) console.log(err)
})
return res.json({message: 'پست حذف شد.'})
return res.json({message: v_m['fa'].response.success_remove})
})
}
]