handle all not found errors

This commit is contained in:
Amir Mohamadi
2021-12-31 17:01:17 +03:30
parent 5c07791225
commit 064d642b7e
47 changed files with 3386 additions and 3238 deletions
+113 -113
View File
@@ -8,136 +8,136 @@ const v_m = require('../validation_messages')
/////////////////////////////////////////////////////////////////////// register
module.exports.register = [
[
body('name')
.isLength({min: 2}).withMessage(v_m['fa'].min_char.min2),
[
body('name')
.isLength({min: 2}).withMessage(v_m['fa'].min_char.min2),
body('username')
.isLength({min: 4}).withMessage(v_m['fa'].min_char.min4)
.custom((value, {req}) => {
return Admin.findOne({username: value})
.then(admin => {
if (admin && admin.username === value) return Promise.reject(v_m['fa'].duplicated.username)
else return true
})
}),
body('password')
.isLength({min: 4}).withMessage(v_m['fa'].min_char.min4)
.custom((value, {req}) => {
if (value === req.body.password_confirmation) return true
else return Promise.reject(v_m['fa'].response.passwords_not_match)
})
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
const data = {}
data.name = req.body.name
data.username = req.body.username
data.created_at = dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(req.body.password, salt, (err, hash) => {
if (err) console.log(err)
data.password = hash
const admin = new Admin(data)
admin.save(err => {
if (err) console.log(err)
return res.json({message: v_m['fa'].response.success_save})
body('username')
.isLength({min: 4}).withMessage(v_m['fa'].min_char.min4)
.custom((value, {req}) => {
return Admin.findOne({username: value})
.then(admin => {
if (admin && admin.username === value) return Promise.reject(v_m['fa'].duplicated.username)
else return true
})
})
})
}),
}
body('password')
.isLength({min: 4}).withMessage(v_m['fa'].min_char.min4)
.custom((value, {req}) => {
if (value === req.body.password_confirmation) return true
else return Promise.reject(v_m['fa'].response.passwords_not_match)
})
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
const data = {}
data.name = req.body.name
data.username = req.body.username
data.created_at = dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(req.body.password, salt, (err, hash) => {
if (err) console.log(err)
data.password = hash
const admin = new Admin(data)
admin.save(err => {
if (err) console.log(err)
return res.json({message: v_m['fa'].response.success_save})
})
})
})
}
]
/////////////////////////////////////////////////////////////////////// login
module.exports.login = [
[
body('username')
.isLength({min: 4}).withMessage(v_m['fa'].min_char.min4)
.bail()
.if((value, {req}) => {
return req.body.password && req.body.password.length >= 4
})
.custom((value, {req}) => {
return Admin.findOne({username: value})
.then(admin => {
if (!admin) return Promise.reject(v_m['fa'].not_found.admin_id)
else return true
})
}),
body('password')
.notEmpty().withMessage(v_m['fa'].required.password)
.bail()
.isLength({min: 4}).withMessage(v_m['fa'].min_char.min4),
body('remember_me')
.exists().withMessage(v_m['fa'].required.remember_me)
.bail()
.isBoolean().withMessage('مقدار باید از جنس Boolean باشد.')
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
Admin.findOne({username: req.body.username}, (err, user) => {
if (err) console.log(err)
bcrypt.compare(req.body.password, user.password, (err, isMatch) => {
if (err) console.log(err)
if (!isMatch) return res.status(422).json({validation: {password: {msg: v_m['fa'].not_found.password}}})
let token
if (req.body.remember_me) token = jwt.sign({_id: user._id}, config.secretKey)
else token = jwt.sign({_id: user._id}, config.secretKey, {expiresIn: '1h'})
user.token = token
user.save(err => {
if (err) console.log(err)
return res.json({token: token})
[
body('username')
.isLength({min: 4}).withMessage(v_m['fa'].min_char.min4)
.bail()
.if((value, {req}) => {
return req.body.password && req.body.password.length >= 4
})
.custom((value, {req}) => {
return Admin.findOne({username: value})
.then(admin => {
if (!admin) return Promise.reject(v_m['fa'].not_found.admin_id)
else return true
})
})
}),
body('password')
.notEmpty().withMessage(v_m['fa'].required.password)
.bail()
.isLength({min: 4}).withMessage(v_m['fa'].min_char.min4),
body('remember_me')
.exists().withMessage(v_m['fa'].required.remember_me)
.bail()
.isBoolean().withMessage('مقدار باید از جنس Boolean باشد.')
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
Admin.findOne({username: req.body.username}, (err, user) => {
if (err) return res.status(404).json({message: err})
bcrypt.compare(req.body.password, user.password, (err, isMatch) => {
if (err) console.log(err)
if (!isMatch) return res.status(422).json({validation: {password: {msg: v_m['fa'].not_found.password}}})
let token
if (req.body.remember_me) token = jwt.sign({_id: user._id}, config.secretKey)
else token = jwt.sign({_id: user._id}, config.secretKey, {expiresIn: '1h'})
user.token = token
user.save(err => {
if (err) console.log(err)
return res.json({token: token})
})
})
}
})
}
]
/////////////////////////////////////////////////////////////////////// logout
module.exports.logout = [
(req, res) => {
const token = req.headers.authorization
if (token) {
Admin.findOneAndUpdate({token: token}, {token: null}, (err, oldData) => {
if (err) console.log(err)
if (oldData) return res.json({message: v_m['fa'].response.logged_out})
return res.status(401).json({message: v_m['fa'].response.not_logged_in})
})
} else {
return res.status(401).json({message: v_m['fa'].response.not_logged_in})
}
}
(req, res) => {
const token = req.headers.authorization
if (token) {
Admin.findOneAndUpdate({token: token}, {token: null}, (err, oldData) => {
if (err) return res.status(404).json({message: err})
if (oldData) return res.json({message: v_m['fa'].response.logged_out})
return res.status(401).json({message: v_m['fa'].response.not_logged_in})
})
} else {
return res.status(401).json({message: v_m['fa'].response.not_logged_in})
}
}
]
/////////////////////////////////////////////////////////////////////// get user
module.exports.getUser = [
config.isAdmin,
(req, res) => {
const token = req.headers.authorization
if (token) {
jwt.verify(token, config.secretKey, (err, decoded) => {
if (err) return res.status(401).json({message: 'unauthenticated'})
Admin.findById(decoded._id, (err, data) => {
if (err) console.log(err)
return res.json({user: data})
})
})
} else {
return res.status(401).json({message: 'unauthenticated'})
}
}
config.isAdmin,
(req, res) => {
const token = req.headers.authorization
if (token) {
jwt.verify(token, config.secretKey, (err, decoded) => {
if (err) return res.status(401).json({message: 'unauthenticated'})
Admin.findById(decoded._id, (err, data) => {
if (err) return res.status(404).json({message: err})
return res.json({user: data})
})
})
} else {
return res.status(401).json({message: 'unauthenticated'})
}
}
]
/////////////////////////////////////////////////////////////////////// delete
+4 -4
View File
@@ -105,7 +105,7 @@ module.exports.update = [
updated_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
BlogCategory.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
if (err) return res.status(404).json({message: err})
return res.json({message: v_m['fa'].response.success_save})
})
}
@@ -115,10 +115,10 @@ module.exports.update = [
module.exports.delete = [
(req, res) => {
BlogPost.find({category: req.params.id}, (err, posts) => {
if (err) console.log(err)
if (err) return res.status(404).json({message: err})
if (!posts.length) {
BlogCategory.findByIdAndDelete(req.params.id, (err) => {
if (err) return res.status(404).json({message: err})
BlogCategory.findByIdAndDelete(req.params.id, (err1) => {
if (err1) return res.status(404).json({message: err1})
return res.json({message: v_m['fa'].response.success_save})
})
} else {
+15 -9
View File
@@ -80,6 +80,9 @@ module.exports.create = [
.cover(thumbWith, thumbHeight)
.write(`./static/uploads/images/blog/thumb_${coverName}`)
})
.catch(err => {
console.log(err)
})
const data = {
post_details: {
@@ -112,7 +115,7 @@ module.exports.create = [
module.exports.getAll = [
(req, res) => {
BlogPost.find({}).sort({_id: -1}).exec((err, posts) => {
if (err) console.log(err)
if (err) return res.status(500).json({message: err})
return res.json(posts)
})
}
@@ -122,7 +125,7 @@ module.exports.getAll = [
module.exports.getOne = [
(req, res) => {
BlogPost.findById(req.params.id, (err, post) => {
if (err) console.log(err)
if (err) return res.status(404).json({message: err})
return res.json(post)
})
}
@@ -213,10 +216,13 @@ module.exports.update = [
.cover(thumbWith, thumbHeight)
.write(`./static/uploads/images/blog/thumb_${coverName}`)
})
.catch(err => {
console.log(err)
})
}
BlogPost.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
if (err) return res.status(404).json({message: err})
if (cover && cover.data) {
fs.unlink(`./static/${oldData.cover}`, err => {
if (err) console.log(err)
@@ -225,8 +231,8 @@ module.exports.update = [
if (err) console.log(err)
})
}
BlogPost.findById(oldData._id, (err, newData) => {
if (err) console.log(err)
BlogPost.findById(oldData._id, (err1, newData) => {
if (err1) return res.status(404).json({message: err1})
return res.json(newData)
})
})
@@ -238,11 +244,11 @@ 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.cover}`, err1 => {
if (err1) console.log(err1)
})
fs.unlink(`./static/${post.thumb}`, err => {
if (err) console.log(err)
fs.unlink(`./static/${post.thumb}`, err2 => {
if (err2) console.log(err2)
})
return res.json({message: v_m['fa'].response.success_remove})
})
+91 -91
View File
@@ -4,110 +4,110 @@ const dateformat = require('dateformat')
const v_m = require('../validation_messages')
module.exports.create = [
[
body('first_name')
.notEmpty()
.withMessage((value, {req}) => {
return v_m[req.body.locale].required.first_name
})
.bail()
.isLength({min: 2})
.withMessage((value, {req}) => {
return v_m[req.body.locale].min_char.min2
}),
[
body('first_name')
.notEmpty()
.withMessage((value, {req}) => {
return v_m[req.body.locale].required.first_name
})
.bail()
.isLength({min: 2})
.withMessage((value, {req}) => {
return v_m[req.body.locale].min_char.min2
}),
body('last_name')
.notEmpty()
.withMessage((value, {req}) => {
return v_m[req.body.locale].required.last_name
})
.bail()
.isLength({min: 2})
.withMessage((value, {req}) => {
return v_m[req.body.locale].min_char.min2
}),
body('last_name')
.notEmpty()
.withMessage((value, {req}) => {
return v_m[req.body.locale].required.last_name
})
.bail()
.isLength({min: 2})
.withMessage((value, {req}) => {
return v_m[req.body.locale].min_char.min2
}),
body('phone_number')
.notEmpty()
.withMessage((value, {req}) => {
return v_m[req.body.locale].required.phone_number
})
.bail()
.isNumeric()
.withMessage((value, {req}) => {
return v_m[req.body.locale].format.phone_number
}),
body('phone_number')
.notEmpty()
.withMessage((value, {req}) => {
return v_m[req.body.locale].required.phone_number
})
.bail()
.isNumeric()
.withMessage((value, {req}) => {
return v_m[req.body.locale].format.phone_number
}),
body('email')
.notEmpty()
.withMessage((value, {req}) => {
return v_m[req.body.locale].required.email
})
.bail()
.isEmail()
.withMessage((value, {req}) => {
return v_m[req.body.locale].format.email
}),
body('email')
.notEmpty()
.withMessage((value, {req}) => {
return v_m[req.body.locale].required.email
})
.bail()
.isEmail()
.withMessage((value, {req}) => {
return v_m[req.body.locale].format.email
}),
body('reason')
.custom((value, {req}) => {
if (!value) return Promise.reject(v_m[req.body.locale].required.reason)
else return true
}),
body('reason')
.custom((value, {req}) => {
if (!value) return Promise.reject(v_m[req.body.locale].required.reason)
else return true
}),
body('message')
.isLength({min: 50})
.withMessage((value, {req}) => {
return v_m[req.body.locale].min_char.min50
})
body('message')
.isLength({min: 50})
.withMessage((value, {req}) => {
return v_m[req.body.locale].min_char.min50
})
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
const data = {
first_name: req.body.first_name,
last_name: req.body.last_name,
phone_number: req.body.phone_number,
email: req.body.email,
message: req.body.message,
reason: req.body.reason,
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
const message = new ContactPageMessage(data)
message.save((err, message) => {
if (err) console.log(err)
return res.json({message: v_m[req.body.locale].response.success_save})
})
}
const data = {
first_name: req.body.first_name,
last_name: req.body.last_name,
phone_number: req.body.phone_number,
email: req.body.email,
message: req.body.message,
reason: req.body.reason,
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
const message = new ContactPageMessage(data)
message.save((err, message) => {
if (err) console.log(err)
return res.json({message: v_m[req.body.locale].response.success_save})
})
}
]
module.exports.getAll = [
(req, res) => {
ContactPageMessage.find({}).select('-message')
.then(messages => {
return res.json(messages)
})
.catch(err => {
console.log(err)
return res.status(500).json(err)
})
}
(req, res) => {
ContactPageMessage.find({}).select('-message')
.then(messages => {
return res.json(messages)
})
.catch(err => {
console.log(err)
return res.status(500).json({message: err})
})
}
]
module.exports.getOne = [
(req, res) => {
ContactPageMessage.findById(req.params.id)
.then(message => {
message.read = true
message.save()
return res.json(message)
})
.catch(err => {
return res.status(404).json(err)
})
}
(req, res) => {
ContactPageMessage.findById(req.params.id)
.then(message => {
message.read = true
message.save()
return res.json(message)
})
.catch(err => {
return res.status(404).json({message: err})
})
}
]
+40 -40
View File
@@ -5,54 +5,54 @@ const v_m = require('../validation_messages')
module.exports.create = [
[
body('fa_reason')
.notEmpty().withMessage(v_m['fa'].required.title),
[
body('fa_reason')
.notEmpty().withMessage(v_m['fa'].required.title),
body('en_reason')
.notEmpty().withMessage(v_m['fa'].required.title)
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
body('en_reason')
.notEmpty().withMessage(v_m['fa'].required.title)
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
const data = {
reason_details: {
fa: {
reason: req.body.fa_reason
},
en: {
reason: req.body.en_reason
}
},
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
const data = {
reason_details: {
fa: {
reason: req.body.fa_reason
},
en: {
reason: req.body.en_reason
}
},
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
const reason = new Reason(data)
reason.save(err => {
if (err) console.log(err)
Reason.find({}, (err2, data) => {
if (err2) console.log(err2)
return res.json(data)
})
const reason = new Reason(data)
reason.save(err => {
if (err) console.log(err)
Reason.find({}, (err2, data) => {
if (err2) console.log(err2)
return res.json(data)
})
}
})
}
]
module.exports.getAll = [
(req, res) => {
Reason.find({}, (err, data) => {
if (err) console.log(err)
return res.json(data)
})
}
(req, res) => {
Reason.find({}, (err, data) => {
if (err) console.log(err)
return res.json(data)
})
}
]
module.exports.delete = [
(req, res) => {
Reason.findByIdAndDelete(req.params.id, (err, reason) => {
if (err) console.log(err)
return res.json({message: v_m['fa'].response.success_remove})
})
}
(req, res) => {
Reason.findByIdAndDelete(req.params.id, (err, reason) => {
if (err) return res.status(404).json({message: err})
return res.json({message: v_m['fa'].response.success_remove})
})
}
]
+3 -3
View File
@@ -84,7 +84,7 @@ module.exports.getAll = [
module.exports.getOne = [
(req, res) => {
History.findById(req.params.id, (err, data) => {
if (err) return res.status(404).json(err)
if (err) return res.status(404).json({message: err})
return res.json(data)
})
}
@@ -151,7 +151,7 @@ module.exports.update = [
})
} else {
History.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
if (err) return res.status(404).json({message: err})
return res.json(v_m['fa'].response.success_save)
})
}
@@ -162,7 +162,7 @@ module.exports.update = [
module.exports.delete = [
(req, res) => {
History.findByIdAndDelete(req.params.id, (err, data) => {
if (err) console.log(err)
if (err) return res.status(404).json({message: err})
fs.unlink(`./static/${data.image}`, err => {
if (err) console.log(err)
return res.json(v_m['fa'].response.success_remove)
+1 -4
View File
@@ -8,11 +8,10 @@ module.exports.create = [
if (!req.files || !req.files.pdf) return res.status(422).json({validation: {pdf: {msg: v_m['fa'].required.file}}})
if (req.files.pdf.mimetype.split('/')[1] !== 'pdf') return res.status(422).json({validation: {pdf: {msg: v_m['fa'].file_types.pdf}}})
MainCatalog.find({}, (err, catalogs) => {
if (err) console.log(err)
if (catalogs.length) {
let firstFile = catalogs[0]
if (firstFile.file[req.body.locale]) fs.unlink(`./static/${firstFile.file[req.body.locale]}`, err => {
if (err) console.log(err)
})
@@ -24,8 +23,6 @@ module.exports.create = [
req.files.pdf.mv(`./static/uploads/pdf/${req.body.locale}_${req.files.pdf.name}`)
return res.json(firstFile)
})
} else {
const data = {created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss'), file: {}}
data.file[req.body.locale] = req.body.locale + '_' + req.files.pdf.name
+41 -2
View File
@@ -168,6 +168,9 @@ module.exports.createProduct = [
.quality(thumbQuality)
.write(`./static/uploads/images/products/${thumb}`)
})
.catch(err => {
console.log(err)
})
}
if (req.files.more_section_image1) {
await jimp.read(req.files.more_section_image1.data)
@@ -178,6 +181,9 @@ module.exports.createProduct = [
.quality(moreSectionQuality)
.write(`./static/uploads/images/products/${more_section_image1}`)
})
.catch(err => {
console.log(err)
})
}
if (req.files.more_section_image2) {
await jimp.read(req.files.more_section_image2.data)
@@ -188,6 +194,9 @@ module.exports.createProduct = [
.quality(moreSectionQuality)
.write(`./static/uploads/images/products/${more_section_image2}`)
})
.catch(err => {
console.log(err)
})
}
if (req.files.render_image1) {
await jimp.read(req.files.render_image1.data)
@@ -198,6 +207,9 @@ module.exports.createProduct = [
.quality(renderImageQuality)
.write(`./static/uploads/images/products/${render_image1}`)
})
.catch(err => {
console.log(err)
})
}
if (req.files.render_image2) {
await jimp.read(req.files.render_image2.data)
@@ -208,6 +220,9 @@ module.exports.createProduct = [
.quality(renderImageQuality)
.write(`./static/uploads/images/products/${render_image2}`)
})
.catch(err => {
console.log(err)
})
}
if (req.files.chart_image) {
await jimp.read(req.files.chart_image.data)
@@ -216,6 +231,9 @@ module.exports.createProduct = [
.quality(chartImageQuality)
.write(`./static/uploads/images/products/${chart_image}`)
})
.catch(err => {
console.log(err)
})
}
}
@@ -249,8 +267,8 @@ module.exports.getAllProducts = [
]
module.exports.getOneProduct = [
async (req, res) => {
await Product.findById(req.params.id, async (err, product) => {
(req, res) => {
Product.findById(req.params.id, (err, product) => {
if (err) return res.status(404).json({message: err})
return res.json(product)
})
@@ -394,6 +412,9 @@ module.exports.updateProduct = [
.quality(thumbQuality)
.write(`./static/uploads/images/products/${thumb}`)
})
.catch(err => {
console.log(err)
})
}
if (req.files.more_section_image1) {
@@ -406,6 +427,9 @@ module.exports.updateProduct = [
.quality(moreSectionQuality)
.write(`./static/uploads/images/products/${more_section_image1}`)
})
.catch(err => {
console.log(err)
})
}
if (req.files.more_section_image2) {
@@ -418,6 +442,9 @@ module.exports.updateProduct = [
.quality(moreSectionQuality)
.write(`./static/uploads/images/products/${more_section_image2}`)
})
.catch(err => {
console.log(err)
})
}
if (req.files.render_image1) {
@@ -430,6 +457,9 @@ module.exports.updateProduct = [
.quality(renderImageQuality)
.write(`./static/uploads/images/products/${render_image1}`)
})
.catch(err => {
console.log(err)
})
}
if (req.files.render_image2) {
@@ -442,6 +472,9 @@ module.exports.updateProduct = [
.quality(renderImageQuality)
.write(`./static/uploads/images/products/${render_image2}`)
})
.catch(err => {
console.log(err)
})
}
if (req.files.chart_image) {
@@ -452,6 +485,9 @@ module.exports.updateProduct = [
.quality(chartImageQuality)
.write(`./static/uploads/images/products/${chart_image}`)
})
.catch(err => {
console.log(err)
})
}
}
@@ -584,6 +620,9 @@ module.exports.createProductImage = [
return res.json(product)
})
})
.catch(err => {
console.log(err)
})
} else {
return res.status(422).json({validation: {images: {image: {msg: v_m['fa'].required.image}}}})
+160 -156
View File
@@ -7,196 +7,200 @@ const v_m = require('../validation_messages')
///////////////////////////////////////// create project
module.exports.create = [
[
body('fa_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return Project.findOne({'project_details.fa.name': value})
.then(project => {
if (project) return Promise.reject(v_m['fa'].duplicated.name)
else return true
})
}),
[
body('fa_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return Project.findOne({'project_details.fa.name': value})
.then(project => {
if (project) return Promise.reject(v_m['fa'].duplicated.name)
else return true
})
}),
body('en_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return Project.findOne({'project_details.en.name': value})
.then(project => {
if (project) return Promise.reject(v_m['fa'].duplicated.name)
else return true
})
}),
body('en_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return Project.findOne({'project_details.en.name': value})
.then(project => {
if (project) return Promise.reject(v_m['fa'].duplicated.name)
else return true
})
}),
body('date')
.notEmpty().withMessage(v_m['fa'].required.date),
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
body('date')
.notEmpty().withMessage(v_m['fa'].required.date),
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
const data = {
date: req.body.date,
project_details: {
fa: {
name: req.body.fa_name,
description: req.body.fa_description,
},
en: {
name: req.body.en_name,
description: req.body.en_description,
}
},
created_at: dateFormat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
const data = {
date: req.body.date,
project_details: {
fa: {
name: req.body.fa_name,
description: req.body.fa_description,
},
en: {
name: req.body.en_name,
description: req.body.en_description,
}
},
created_at: dateFormat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
const project = new Project(data)
project.save((err, project) => {
if (err) console.log(err)
return res.json(project)
})
}
const project = new Project(data)
project.save((err, project) => {
if (err) console.log(err)
return res.json(project)
})
}
]
///////////////////////////////////////// get all projects
module.exports.getAll = [
(req, res) => {
Project.find({}, (err, projects) => {
if (err) return res.json({errors: err})
return res.json(projects)
})
}
(req, res) => {
Project.find({}, (err, projects) => {
if (err) return res.json({errors: err})
return res.json(projects)
})
}
]
///////////////////////////////////////// get one project
module.exports.getOne = [
(req, res) => {
Project.findById(req.params.id, (err, project) => {
if (err) return res.json({errors: err})
return res.json(project)
})
}
(req, res) => {
Project.findById(req.params.id, (err, project) => {
if (err) return res.status(404).json({errors: err})
return res.json(project)
})
}
]
///////////////////////////////////////// update one project
module.exports.update = [
[
body('fa_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return Project.findOne({'project_details.fa.name': value})
.then(project => {
if (project && project._id.toString() !== req.params.id) return Promise.reject(v_m['fa'].duplicated.name)
else return true
})
}),
[
body('fa_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return Project.findOne({'project_details.fa.name': value})
.then(project => {
if (project && project._id.toString() !== req.params.id) return Promise.reject(v_m['fa'].duplicated.name)
else return true
})
}),
body('en_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return Project.findOne({'project_details.en.name': value})
.then(project => {
if (project && project._id.toString() !== req.params.id) return Promise.reject(v_m['fa'].duplicated.name)
else return true
})
}),
body('en_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return Project.findOne({'project_details.en.name': value})
.then(project => {
if (project && project._id.toString() !== req.params.id) return Promise.reject(v_m['fa'].duplicated.name)
else return true
})
}),
body('date')
.notEmpty().withMessage(v_m['fa'].required.date),
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
body('date')
.notEmpty().withMessage(v_m['fa'].required.date),
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
const data = {
date: req.body.date,
project_details: {
fa: {
name: req.body.fa_name,
description: req.body.fa_description,
},
en: {
name: req.body.en_name,
description: req.body.en_description,
}
},
updated_at: dateFormat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
const data = {
date: req.body.date,
project_details: {
fa: {
name: req.body.fa_name,
description: req.body.fa_description,
},
en: {
name: req.body.en_name,
description: req.body.en_description,
}
},
updated_at: dateFormat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
Project.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
return res.json({message: v_m['fa'].response.success_save})
})
}
Project.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) return res.status(404).json({message: err})
return res.json({message: v_m['fa'].response.success_save})
})
}
]
///////////////////////////////////////// delete project
module.exports.delete = [
(req, res) => {
Project.findByIdAndRemove(req.params.id, (err, data) => {
if (err) return res.json({error: err})
data.images.forEach(async item => {
await fs.unlink(`./static/${item.image}`, err => {
if (err) console.log(err)
})
})
return res.json({message: v_m['fa'].response.success_remove})
(req, res) => {
Project.findByIdAndRemove(req.params.id, (err, data) => {
if (err) return res.status(404).json({message: err})
data.images.forEach(async item => {
try {
await fs.unlink(`./static/${item.image}`, err => {
if (err) console.log(err)
})
} catch (e) {
console.log(e)
}
})
}
return res.json({message: v_m['fa'].response.success_remove})
})
}
]
////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// add images to project
module.exports.addImage = [
(req, res) => {
Project.findById(req.params.id)
.then(project => {
if (!req.files || !req.files.image) return res.status(422).json({validation: {image: {msg: v_m['fa'].required.image}}})
(req, res) => {
Project.findById(req.params.id)
.then(project => {
if (!req.files || !req.files.image) return res.status(422).json({validation: {image: {msg: v_m['fa'].required.image}}})
let fileName = 'project_' + Date.now() + '.' + req.files.image.mimetype.split('/')[1]
jimp.read(req.files.image.data)
.then(img => {
img
.cover(1010, 534)
.quality(80)
.write(`./static/uploads/images/projects/${fileName}`)
let fileName = 'project_' + Date.now() + '.' + req.files.image.mimetype.split('/')[1]
jimp.read(req.files.image.data)
.then(img => {
img
.cover(1010, 534)
.quality(80)
.write(`./static/uploads/images/projects/${fileName}`)
////
const data = {
image: fileName,
created_at: dateFormat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
project.images.push(data)
project.save(cb => {
return res.json(project)
})
})
.catch(err => {
console.log(err)
})
})
}
////
const data = {
image: fileName,
created_at: dateFormat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
project.images.push(data)
project.save(cb => {
return res.json(project)
})
})
.catch(err => {
console.log(err)
})
})
}
]
module.exports.deleteImage = [
(req, res) => {
Project.findOne({'images._id': req.params.id}, (err, project) => {
if (err) console.log(err)
if (project) {
let image = project.images.id(req.params.id)
fs.unlink(`./static/${image.image}`, err => {
if (err) console.log(err)
image.remove()
project.save(cb => {
return res.json({message: v_m['fa'].response.success_remove})
})
})
}
})
}
(req, res) => {
Project.findOne({'images._id': req.params.id}, (err, project) => {
if (err) console.log(err)
if (project) {
let image = project.images.id(req.params.id)
fs.unlink(`./static/${image.image}`, err => {
if (err) console.log(err)
image.remove()
project.save(cb => {
return res.json({message: v_m['fa'].response.success_remove})
})
})
}
})
}
]
+135 -135
View File
@@ -7,167 +7,167 @@ const v_m = require('../validation_messages')
//////////////////////////////////////////////////////////////////////////////// create
module.exports.create = [
[
body('fa_title')
.isLength({max: 40}).withMessage(v_m['fa'].max_char.max40),
[
body('fa_title')
.isLength({max: 40}).withMessage(v_m['fa'].max_char.max40),
body('en_title')
.isLength({max: 40}).withMessage(v_m['fa'].max_char.max40),
body('en_title')
.isLength({max: 40}).withMessage(v_m['fa'].max_char.max40),
body('fa_caption')
.isLength({max: 100}).withMessage(v_m['fa'].max_char.max100),
body('fa_caption')
.isLength({max: 100}).withMessage(v_m['fa'].max_char.max100),
body('en_caption')
.isLength({max: 100}).withMessage(v_m['fa'].max_char.max100)
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
body('en_caption')
.isLength({max: 100}).withMessage(v_m['fa'].max_char.max100)
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
if (req.files) {
let file = req.files.image
jimp.read(file.data)
.then(img => {
let fileName = 'image_' + Date.now() + '.' + img.getExtension()
if (img.bitmap.width >= 1920 && img.bitmap.height >= 1080) {
if (file.size / 1024 < 450) {
file.mv(`./static/uploads/images/slider/${fileName}`, (err, img) => {
const slider = new Slider({
image: fileName,
slider_details: {
fa: {
title: req.body.fa_title,
caption: req.body.fa_caption
},
en: {
title: req.body.en_title,
caption: req.body.en_caption
}
},
created_at: dateFormat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
})
slider.save((err, data) => {
if (err) return res.status(500).json({error: err})
return res.json({message: v_m['fa'].response.success_save})
})
})
} else {
return res.status(422).json({validation: {image: {msg: 'حجم تصویر نباید بیشتر از 450 کیلوبایت باشد'}}})
}
} else {
return res.status(422).json({validation: {image: {msg: 'ابعاد تصویر حداقل 1920*1080 پیکسل قابل قبول میباشد.'}}})
}
})
.catch(err => {
console.log(err)
})
} else {
return res.status(422).json({validation: {image: {msg: v_m['fa'].required.image}}})
}
}
if (req.files) {
let file = req.files.image
jimp.read(file.data)
.then(img => {
let fileName = 'image_' + Date.now() + '.' + img.getExtension()
if (img.bitmap.width >= 1920 && img.bitmap.height >= 1080) {
if (file.size / 1024 < 450) {
file.mv(`./static/uploads/images/slider/${fileName}`, (err, img) => {
const slider = new Slider({
image: fileName,
slider_details: {
fa: {
title: req.body.fa_title,
caption: req.body.fa_caption
},
en: {
title: req.body.en_title,
caption: req.body.en_caption
}
},
created_at: dateFormat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
})
slider.save((err, data) => {
if (err) return res.status(500).json({error: err})
return res.json({message: v_m['fa'].response.success_save})
})
})
} else {
return res.status(422).json({validation: {image: {msg: 'حجم تصویر نباید بیشتر از 450 کیلوبایت باشد'}}})
}
} else {
return res.status(422).json({validation: {image: {msg: 'ابعاد تصویر حداقل 1920*1080 پیکسل قابل قبول میباشد.'}}})
}
})
.catch(err => {
console.log(err)
})
} else {
return res.status(422).json({validation: {image: {msg: v_m['fa'].required.image}}})
}
}
]
//////////////////////////////////////////////////////////////////////////////// get all
module.exports.getAll = [
(req, res) => {
Slider.find({}, (err, slides) => {
if (err) return res.status(404).json({error: err})
return res.json(slides)
})
}
(req, res) => {
Slider.find({}, (err, slides) => {
if (err) return res.status(404).json({error: err})
return res.json(slides)
})
}
]
//////////////////////////////////////////////////////////////////////////////// get one
module.exports.getOne = [
(req, res) => {
Slider.findById(req.params.id, (err, slide) => {
if (err) return res.status(404).json({error: v_m['fa'].not_found.item_id})
return res.json(slide)
})
}
(req, res) => {
Slider.findById(req.params.id, (err, slide) => {
if (err) return res.status(404).json({error: v_m['fa'].not_found.item_id})
return res.json(slide)
})
}
]
//////////////////////////////////////////////////////////////////////////////// update
module.exports.update = [
[
body('fa_title')
.isLength({max: 40}).withMessage(v_m['fa'].max_char.max40),
[
body('fa_title')
.isLength({max: 40}).withMessage(v_m['fa'].max_char.max40),
body('en_title')
.isLength({max: 40}).withMessage(v_m['fa'].max_char.max40),
body('en_title')
.isLength({max: 40}).withMessage(v_m['fa'].max_char.max40),
body('fa_caption')
.isLength({max: 100}).withMessage(v_m['fa'].max_char.max100),
body('fa_caption')
.isLength({max: 100}).withMessage(v_m['fa'].max_char.max100),
body('en_caption')
.isLength({max: 100}).withMessage(v_m['fa'].max_char.max100)
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
body('en_caption')
.isLength({max: 100}).withMessage(v_m['fa'].max_char.max100)
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
const data = {
slider_details: {
fa: {
title: req.body.fa_title,
caption: req.body.fa_caption
},
en: {
title: req.body.en_title,
caption: req.body.en_caption
}
},
updated_at: dateFormat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
const data = {
slider_details: {
fa: {
title: req.body.fa_title,
caption: req.body.fa_caption
},
en: {
title: req.body.en_title,
caption: req.body.en_caption
}
},
updated_at: dateFormat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
if (req.files) {
let file = req.files.image
jimp.read(file.data)
.then(img => {
let fileName = 'image_' + Date.now() + '.' + img.getExtension()
data.image = fileName
if (img.bitmap.width >= 1920 && img.bitmap.height >= 1080) {
if (file.size / 1024 < 450) {
file.mv(`./static/uploads/images/slider/${fileName}`, (err) => {
if (err) console.log(err)
if (req.files) {
let file = req.files.image
jimp.read(file.data)
.then(img => {
let fileName = 'image_' + Date.now() + '.' + img.getExtension()
data.image = fileName
if (img.bitmap.width >= 1920 && img.bitmap.height >= 1080) {
if (file.size / 1024 < 450) {
file.mv(`./static/uploads/images/slider/${fileName}`, (err) => {
if (err) console.log(err)
Slider.findByIdAndUpdate(req.params.id, data, (err, oldSlide) => {
if (err) return res.status(500).json({error: err})
fs.unlink(`./static/${oldSlide.image}`, err => {
if (err) console.log(err)
})
return res.json({message: v_m['fa'].response.success_save})
})
})
} else {
return res.status(422).json({validation: {image: {msg: 'حجم تصویر نباید بیشتر از 450 کیلوبایت باشد'}}})
}
} else {
return res.status(422).json({validation: {image: {msg: 'ابعاد تصویر حداقل 1920*1080 پیکسل قابل قبول میباشد.'}}})
}
})
.catch(err => {
console.log(err)
})
} else {
Slider.findByIdAndUpdate(req.params.id, data, (err, oldSlide) => {
if (err) return res.status(500).json({error: err})
return res.json({message: v_m['fa'].response.success_save})
Slider.findByIdAndUpdate(req.params.id, data, (err, oldSlide) => {
if (err) return res.status(500).json({error: err})
fs.unlink(`./static/${oldSlide.image}`, err => {
if (err) console.log(err)
})
return res.json({message: v_m['fa'].response.success_save})
})
})
} else {
return res.status(422).json({validation: {image: {msg: 'حجم تصویر نباید بیشتر از 450 کیلوبایت باشد'}}})
}
} else {
return res.status(422).json({validation: {image: {msg: 'ابعاد تصویر حداقل 1920*1080 پیکسل قابل قبول میباشد.'}}})
}
})
}
}
.catch(err => {
console.log(err)
})
} else {
Slider.findByIdAndUpdate(req.params.id, data, (err, oldSlide) => {
if (err) return res.status(500).json({error: err})
return res.json({message: v_m['fa'].response.success_save})
})
}
}
]
//////////////////////////////////////////////////////////////////////////////// delete
module.exports.delete = [
(req, res) => {
Slider.findByIdAndRemove(req.params.id, (err, data) => {
if (err) return res.json({error: err})
fs.unlink(`./static/${data.image}`, err => {
if (err) console.log(err)
})
return res.json({message: v_m['fa'].response.success_remove})
(req, res) => {
Slider.findByIdAndRemove(req.params.id, (err, data) => {
if (err) return res.json({error: err})
fs.unlink(`./static/${data.image}`, err => {
if (err) console.log(err)
})
}
return res.json({message: v_m['fa'].response.success_remove})
})
}
]