add calculation module and fix many bugs

This commit is contained in:
Amir Mohamadi
2021-01-27 18:51:06 +03:30
parent 9433eb84ac
commit 08a17401aa
70 changed files with 7133 additions and 5528 deletions
+101 -101
View File
@@ -5,125 +5,125 @@ const {body, validationResult} = require('express-validator')
const v_m = require('../validation_messages')
module.exports.create = [
[
body('fa_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return BlogCategory.findOne({'blogCategory_details.fa.name': value}).then(category => {
if (category) return Promise.reject(v_m['fa'].duplicated.title)
return true
})
}),
body('en_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return BlogCategory.findOne({'blogCategory_details.en.name': value}).then(category => {
if (category) return Promise.reject(v_m['fa'].duplicated.title)
return true
})
[
body('fa_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return BlogCategory.findOne({'blogCategory_details.fa.name': value}).then(category => {
if (category) return Promise.reject(v_m['fa'].duplicated.title)
return true
})
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
}),
body('en_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return BlogCategory.findOne({'blogCategory_details.en.name': value}).then(category => {
if (category) return Promise.reject(v_m['fa'].duplicated.title)
return true
})
})
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
const blogCategory = new BlogCategory({
blogCategory_details: {
fa: {
name: req.body.fa_name
},
en: {
name: req.body.en_name
}
},
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
})
blogCategory.save(err => {
if (err) console.log(err)
return res.json({message: v_m['fa'].response.success_save})
})
}
const blogCategory = new BlogCategory({
blogCategory_details: {
fa: {
name: req.body.fa_name
},
en: {
name: req.body.en_name
}
},
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
})
blogCategory.save(err => {
if (err) console.log(err)
return res.json({message: v_m['fa'].response.success_save})
})
}
]
module.exports.getAll = [
(req, res) => {
BlogCategory.find({}, (err, categories) => {
if (err) return res.status(500).json({message: err})
return res.json(categories)
})
}
(req, res) => {
BlogCategory.find({}, (err, categories) => {
if (err) return res.status(500).json({message: err})
return res.json(categories)
})
}
]
module.exports.getOne = [
(req, res) => {
BlogCategory.findById(req.params.id, (err, category) => {
if (err) return res.status(404).json({message: err})
return res.json(category)
})
}
(req, res) => {
BlogCategory.findById(req.params.id, (err, category) => {
if (err) return res.status(404).json({message: err})
return res.json(category)
})
}
]
module.exports.update = [
[
body('fa_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return BlogCategory.findOne({'blogCategory_details.fa.name': value}).then(category => {
if (category && category._id.toString() !== req.params.id) return Promise.reject(v_m['fa'].duplicated.title)
return true
})
}),
body('en_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return BlogCategory.findOne({'blogCategory_details.en.name': value}).then(category => {
if (category && category._id.toString() !== req.params.id) return Promise.reject(v_m['fa'].duplicated.title)
return true
})
[
body('fa_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return BlogCategory.findOne({'blogCategory_details.fa.name': value}).then(category => {
if (category && category._id.toString() !== req.params.id) return Promise.reject(v_m['fa'].duplicated.title)
return true
})
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
}),
body('en_name')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.custom((value, {req}) => {
return BlogCategory.findOne({'blogCategory_details.en.name': value}).then(category => {
if (category && category._id.toString() !== req.params.id) return Promise.reject(v_m['fa'].duplicated.title)
return true
})
})
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
const data = {
blogCategory_details: {
fa: {
name: req.body.fa_name
},
en: {
name: req.body.en_name
}
},
updated_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
BlogCategory.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
return res.json({message: v_m['fa'].response.success_save})
})
}
const data = {
blogCategory_details: {
fa: {
name: req.body.fa_name
},
en: {
name: req.body.en_name
}
},
updated_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
BlogCategory.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
return res.json({message: v_m['fa'].response.success_save})
})
}
]
module.exports.delete = [
(req, res) => {
BlogPost.find({category: req.params.id}, (err, posts) => {
if (err) console.log(err)
if (!posts.length) {
BlogCategory.findByIdAndDelete(req.params.id, (err) => {
if (err) return res.status(404).json({message: err})
return res.json({message: v_m['fa'].response.success_save})
})
} else {
return res.status(500).json({message: 'نمیتوان دسته بندی هایی که دارای پست هستند را پاک کرد.'})
}
})
}
(req, res) => {
BlogPost.find({category: req.params.id}, (err, posts) => {
if (err) console.log(err)
if (!posts.length) {
BlogCategory.findByIdAndDelete(req.params.id, (err) => {
if (err) return res.status(404).json({message: err})
return res.json({message: v_m['fa'].response.success_save})
})
} else {
return res.status(500).json({message: 'نمیتوان دسته بندی هایی که دارای پست هستند را پاک کرد.'})
}
})
}
]
+212 -212
View File
@@ -5,246 +5,246 @@ const fs = require('fs')
const jimp = require('jimp')
const v_m = require('../validation_messages')
// local variables
const shortDescriptionLength = 500
const coverWidth = 1920
const coverHeight = 720
const coverQuality = 70
const thumbWith = 460
const thumbHeight = 320
module.exports.create = [
[
// title
body('fa_title')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.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(v_m['fa'].duplicated.title)
return true
})
}),
body('en_title')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.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(v_m['fa'].duplicated.title)
return true
})
}),
// description
body('fa_description')
.notEmpty().withMessage(v_m['fa'].required.description),
body('en_description')
.notEmpty().withMessage(v_m['fa'].required.description),
// short description
body('fa_short_description')
.notEmpty().withMessage(v_m['fa'].required.description)
.bail()
.isLength({max: 200}).withMessage(v_m['fa'].max_char.max200),
body('en_short_description')
.notEmpty().withMessage(v_m['fa'].required.description)
.bail()
.isLength({max: 200}).withMessage(v_m['fa'].max_char.max200),
body('category')
.notEmpty().withMessage(v_m['fa'].required.category)
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
let cover
let coverName
// 460 X 320
// 1920 x 720
if (req.files && req.files.cover) {
cover = req.files.cover
coverName = 'blog_' + Date.now() + '.' + cover.mimetype.split('/')[1]
} else {
return res.status(422).json({validation: {cover: {msg: v_m['fa'].required.cover}}})
}
jimp.read(cover.data)
.then(img => {
img
.cover(1920, 720)
.quality(70)
.write(`./static/uploads/images/blog/${coverName}`)
.resize(460, jimp.AUTO)
.cover(460, 320)
.write(`./static/uploads/images/blog/thumb_${coverName}`)
[
// title
body('fa_title')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.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(v_m['fa'].duplicated.title)
return true
})
}),
body('en_title')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.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(v_m['fa'].duplicated.title)
return true
})
}),
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
}
},
cover: coverName,
published: req.body.published,
category: req.body.category,
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
// description
body('fa_description')
.notEmpty().withMessage(v_m['fa'].required.description),
body('en_description')
.notEmpty().withMessage(v_m['fa'].required.description),
const post = new BlogPost(data)
post.save(err => {
if (err) console.log(err)
return res.json({message: v_m['fa'].response.success_save})
})
}
// short description
body('fa_short_description')
.notEmpty().withMessage(v_m['fa'].required.description),
body('en_short_description')
.notEmpty().withMessage(v_m['fa'].required.description),
body('category')
.notEmpty().withMessage(v_m['fa'].required.category)
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
let cover
let coverName
if (req.files && req.files.cover) {
cover = req.files.cover
coverName = 'blog_' + Date.now() + '.' + cover.mimetype.split('/')[1]
} else {
return res.status(422).json({validation: {cover: {msg: v_m['fa'].required.cover}}})
}
jimp.read(cover.data)
.then(img => {
img
.cover(coverWidth, coverHeight)
.quality(coverQuality)
.write(`./static/uploads/images/blog/${coverName}`)
.resize(thumbWith, jimp.AUTO)
.cover(thumbWith, thumbHeight)
.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.slice(0, shortDescriptionLength) + ' ... '
},
en: {
title: req.body.en_title,
description: req.body.en_description,
short_description: req.body.en_short_description.slice(0, shortDescriptionLength) + ' ... '
}
},
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: v_m['fa'].response.success_save})
})
}
]
module.exports.getAll = [
(req, res) => {
BlogPost.find({}).sort({_id: -1}).exec((err, posts) => {
if (err) console.log(err)
return res.json(posts)
})
}
(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)
})
}
(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(v_m['fa'].required.title)
.bail()
.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(v_m['fa'].duplicated.title)
return true
})
}),
body('en_title')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.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(v_m['fa'].duplicated.title)
return true
})
}),
[
// title
body('fa_title')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.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(v_m['fa'].duplicated.title)
return true
})
}),
body('en_title')
.notEmpty().withMessage(v_m['fa'].required.title)
.bail()
.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(v_m['fa'].duplicated.title)
return true
})
}),
// description
body('fa_description')
.notEmpty().withMessage(v_m['fa'].required.description),
body('en_description')
.notEmpty().withMessage(v_m['fa'].required.description),
// description
body('fa_description')
.notEmpty().withMessage(v_m['fa'].required.description),
body('en_description')
.notEmpty().withMessage(v_m['fa'].required.description),
// short description
body('fa_short_description')
.notEmpty().withMessage(v_m['fa'].required.description)
.bail()
.isLength({max: 200}).withMessage(v_m['fa'].max_char.max200),
body('en_short_description')
.notEmpty().withMessage(v_m['fa'].required.description)
.bail()
.isLength({max: 200}).withMessage(v_m['fa'].max_char.max200),
// short description
body('fa_short_description')
.notEmpty().withMessage(v_m['fa'].required.description),
body('en_short_description')
.notEmpty().withMessage(v_m['fa'].required.description),
body('category')
.notEmpty().withMessage(v_m['fa'].required.category)
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
body('category')
.notEmpty().withMessage(v_m['fa'].required.category)
],
(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
}
},
published: req.body.published,
category: req.body.category,
updated_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
const data = {
post_details: {
fa: {
title: req.body.fa_title,
description: req.body.fa_description,
short_description: req.body.fa_short_description.slice(0, shortDescriptionLength) + ' ... '
},
en: {
title: req.body.en_title,
description: req.body.en_description,
short_description: req.body.en_short_description.slice(0, shortDescriptionLength) + ' ... '
}
},
published: req.body.published,
category: req.body.category,
updated_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
let cover
let coverName
if (req.files && req.files.cover) {
cover = req.files.cover
coverName = 'blog_' + Date.now() + '.' + cover.mimetype.split('/')[1]
data.cover = coverName
jimp.read(cover.data)
.then(img => {
img
.cover(coverWidth, coverHeight)
.quality(coverQuality)
.write(`./static/uploads/images/blog/${coverName}`)
.resize(thumbWith, jimp.AUTO)
.cover(thumbWith, thumbHeight)
.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)
})
fs.unlink(`./static/${oldData.thumb}`, err => {
if (err) console.log(err)
})
}
// 460 X 320
// 1920 x 720
let cover
let coverName
if (req.files && req.files.cover) {
cover = req.files.cover
coverName = 'blog_' + Date.now() + '.' + cover.mimetype.split('/')[1]
data.cover = coverName
jimp.read(cover.data)
.then(img => {
img
.cover(1920, 720)
.write(`./static/uploads/images/blog/${coverName}`)
.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)
})
fs.unlink(`./static/${oldData.thumb}`, err => {
if (err) console.log(err)
})
}
return res.json({message: v_m['fa'].response.success_save})
BlogPost.findById(oldData._id, (err, newData) => {
if (err) console.log(err)
return res.json(newData)
})
}
})
}
]
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: v_m['fa'].response.success_remove})
(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: v_m['fa'].response.success_remove})
})
}
]
+665 -27
View File
@@ -2,34 +2,672 @@ const {body, validationResult} = require('express-validator')
const v_m = require('../validation_messages')
const validationMessages = {
fa: {},
en: {}
fa: {
method: 'روش محاسبه را انتخاب کنید',
method_not_supported: 'روش انتخاب شده پشتیبانی نشده است',
gratingType: 'نوع گریتینک را انتخاب کنید',
gratingType_not_supported: 'نوع گریتینگ پشتیبانی نشده است',
loadType: 'نوع فشار را وارد کنید',
class: 'کلاس را انتخاب کنید',
pointedLoad: 'مقدار بار متمرکز را وارد کنید',
distributedLoad: 'مقدار بار گسترده را وارد کنید',
bearingBarThickness: 'ضخامت تسمه باربر را وارد کنید',
bearingBarPitch: 'گام تسمه باربر را انتخاب کنید',
bearingBarPitch_not_ok: 'مقدار گام تسمه باربر صحیح نیست',
crossBarPitch: 'گام تسمه رابط را تنخاب کنید',
crossBarPitch_not_ok: 'مقدار گام تسمه رابط صحیح نیست',
bearingBarHeight: 'ارتفاع تسمه باربر را انتخاب کنید',
bearingBarHeight_not_ok: 'مقدار ارتفاع تسمه باربر صحیح نیست',
clearSpan: 'مقدار دهنه را وارد کنید',
sigma_ok: 'تنش مجاز',
sigma_out_of_range: 'تنش خارج از حد مجاز',
deflection_ok: 'خیز مجاز',
deflection_out_of_range: 'خیز خارج از حد مجاز'
},
en: {
method: 'Choose calculation method',
method_not_supported: 'Chosen calculation method not supported',
gratingType: 'Select the type of grating',
gratingType_not_supported: 'Grating type not supported',
loadType: 'Enter load type',
class: 'Select class',
pointedLoad: 'Enter the amount of pointed load',
distributedLoad: 'Enter the amount of distributed load',
bearingBarThickness: 'Enter bearing bar thickness',
bearingBarPitch: 'Select bearing bar pitch',
bearingBarPitch_not_ok: 'Bearing bar pitch value is incorrect',
crossBarPitch: 'Select cross bar pitch',
crossBarPitch_not_ok: 'Cross bar pitch value is incorrect',
bearingBarHeight: 'Select bearing bar height',
bearingBarHeight_not_ok: 'Bearing bar height is incorrect',
clearSpan: 'Enter clear span',
sigma_ok: 'Tension OK',
sigma_out_of_range: 'Tension Out Of Range',
deflection_ok: 'Deflection OK',
deflection_out_of_range: 'Deflection Out Of Range'
}
}
module.exports = [
[
body('method')
.notEmpty().withMessage(),
body('type')
.notEmpty().withMessage(),
body('class')
.notEmpty().withMessage(),
body('pointLoad')
.notEmpty().withMessage(),
body('distributedLoad')
.notEmpty().withMessage(),
body('bearingBarPitch')
.notEmpty().withMessage(),
body('crossBarPitCh')
.notEmpty().withMessage(),
body('clearSpan')
.notEmpty().withMessage(),
body('bearingBarThickness')
.notEmpty().withMessage(),
body('bearingBarHeight')
.notEmpty().withMessage()
],
(req, res) => {
const values = {
// ArakRail products
bearingBarPitch_forge: [30, 35, 41, 50],
bearingBarPitch_pressured: [11, 22, 33, 44, 50, 55],
// defined in formula
crossBarPitch_forge: [38, 50, 76, 100],
crossBarPitch_pressured: [11, 22, 33, 44, 50, 55, 66, 77, 88, 100],
// defined in formula
bearingBarHeight: [20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
}
}
function relativeBearingBarHeight(gratingType, crossBarPitch) {
// defined in formula
bearingBarHeight = values.bearingBarHeight
const CBPitch = Number(crossBarPitch)
if (gratingType === 'forge') {
if (CBPitch === 38) return bearingBarHeight.filter(item => item <= 80)
else return bearingBarHeight.filter(item => item <= 60)
} else if (gratingType === 'pressured') {
if (
CBPitch === 11 ||
CBPitch === 22 ||
CBPitch === 55 ||
CBPitch === 77 ||
CBPitch === 88
) return bearingBarHeight.filter(item => item <= 60)
else if (
CBPitch === 33 ||
CBPitch === 44 ||
CBPitch === 50 ||
CBPitch === 66 ||
CBPitch === 100
) return bearingBarHeight
}
}
module.exports.calculate = [
[
body('method')
.notEmpty().withMessage((value, {req}) => {
return validationMessages[req.body.locale].method
})
.bail()
.custom((value, {req}) => {
if (value === 'custom' || value === 'classified') return true
else return Promise.reject(validationMessages[req.body.locale].method_not_supported)
}),
body('gratingType')
.notEmpty().withMessage((value, {req}) => {
return validationMessages[req.body.locale].gratingType
})
.bail()
.custom((value, {req}) => {
if (value === 'forge' || value === 'pressured') return true
else return Promise.reject(validationMessages[req.body.locale].gratingType_not_supported)
}),
body('loadType')
.custom((value, {req}) => {
if (req.body.method === 'custom') {
if (value === 'pointedLoad' || value === 'distributedLoad') return true
else return Promise.reject(validationMessages[req.body.locale].loadType)
} else return true
}),
body('vehicleClass')
.custom((value, {req}) => {
if (req.body.method === 'classified') {
const classes = ['class1', 'class2', 'class3', 'class4', 'FL1', 'FL2', 'FL3', 'FL4', 'FL5', 'FL6']
if (classes.includes(value)) return true
else return Promise.reject(validationMessages[req.body.locale].class)
} else return true
}),
body('pointedLoadValue')
.custom((value, {req}) => {
if (req.body.method === 'custom') {
if (req.body.loadType === 'pointedLoad' && !value) return Promise.reject(validationMessages[req.body.locale].pointedLoad)
else return true
} else return true
})
.bail()
.if((value, {req}) => req.body.method === 'custom' && req.body.loadType === 'pointedLoad')
.isNumeric().withMessage((value, {req}) => {
return v_m[req.body.locale].format.number
}),
body('distributedLoadValue')
.custom((value, {req}) => {
if (req.body.method === 'custom') {
if (req.body.loadType === 'distributedLoad' && !value) return Promise.reject(validationMessages[req.body.locale].distributedLoad)
else return true
} else return true
})
.bail()
.if((value, {req}) => req.body.method === 'custom' && req.body.loadType === 'distributedLoad')
.isNumeric().withMessage((value, {req}) => {
return v_m[req.body.locale].format.number
}),
body('bearingBarThickness')
.notEmpty().withMessage((value, {req}) => {
return validationMessages[req.body.locale].bearingBarThickness
})
.bail()
.isNumeric().withMessage((value, {req}) => {
return v_m[req.body.locale].format.number
}),
body('bearingBarPitch')
.notEmpty().withMessage((value, {req}) => {
return validationMessages[req.body.locale].bearingBarPitch
})
.bail()
.isNumeric().withMessage((value, {req}) => {
return v_m[req.body.locale].format.number
})
.bail()
.custom((value, {req}) => {
if (req.body.gratingType === 'forge') {
if (values.bearingBarPitch_forge.includes(Number(value))) return true
else return Promise.reject(validationMessages[req.body.locale].bearingBarPitch_not_ok)
} else {
if (values.bearingBarPitch_pressured.includes(Number(value))) return true
else return Promise.reject(validationMessages[req.body.locale].bearingBarPitch_not_ok)
}
}),
body('crossBarPitch')
.notEmpty().withMessage((value, {req}) => {
return validationMessages[req.body.locale].crossBarPitch
})
.bail()
.isNumeric().withMessage((value, {req}) => {
return v_m[req.body.locale].format.number
})
.bail()
.custom((value, {req}) => {
if (req.body.gratingType === 'forge') {
if (values.crossBarPitch_forge.includes(Number(value))) return true
else return Promise.reject(validationMessages[req.body.locale].crossBarPitch_not_ok)
} else {
if (values.crossBarPitch_pressured.includes(Number(value))) return true
else return Promise.reject(validationMessages[req.body.locale].crossBarPitch_not_ok)
}
}),
body('bearingBarHeight')
.notEmpty().withMessage((value, {req}) => {
return validationMessages[req.body.locale].bearingBarHeight
})
.bail()
.isNumeric().withMessage((value, {req}) => {
return v_m[req.body.locale].format.number
})
.bail()
.custom((value, {req}) => {
if (relativeBearingBarHeight(req.body.gratingType, req.body.crossBarPitch).includes(Number(value))) return true
else return Promise.reject(validationMessages[req.body.locale].bearingBarHeight_not_ok)
}),
body('clearSpan')
.notEmpty().withMessage((value, {req}) => {
return validationMessages[req.body.locale].clearSpan
})
.bail()
.isNumeric().withMessage((value, {req}) => {
return v_m[req.body.locale].format.number
})
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
let method = req.body.method
let gratingType = req.body.gratingType
let loadType = req.body.loadType
let vehicleClass = req.body.vehicleClass
let bearingBarHeight = req.body.bearingBarHeight
let bearingBarThickness = req.body.bearingBarThickness
let bearingBarPitch = req.body.bearingBarPitch
let crossBarPitch = req.body.crossBarPitch
let clearSpan = req.body.clearSpan
let locale = req.body.locale
///////// classified vehicles
const vehiclesClasses = {
'class1': {
distributedLoad: 6
},
'class2': {
pointedLoad: 10,
c4: 200,
d4: 200
},
'class3': {
pointedLoad: 30,
c4: 200,
d4: 400
},
'class4': {
pointedLoad: 90,
c4: 250,
d4: 600
},
'FL1': {
pointedLoad: 26,
c4: 130,
d4: 130
},
'FL2': {
pointedLoad: 40,
c4: 150,
d4: 175
},
'FL3': {
pointedLoad: 63,
c4: 200,
d4: 200
},
'FL4': {
pointedLoad: 90,
c4: 200,
d4: 300
},
'FL5': {
pointedLoad: 140,
c4: 200,
d4: 375
},
'FL6': {
pointedLoad: 170,
c4: 200,
d4: 450
},
}
///////// pointed load
function pointedLoad() {
// static variables
const forge = gratingType === 'forge'
const custom = method === 'custom'
// computing m | c4 | d4 | pointedLoadValue
function m_pressured(bearingBarHeight, crossBarPitch) {
const CBPitch = Number(crossBarPitch)
const BBHeight = Number(bearingBarHeight)
if (BBHeight === 20) {
if (CBPitch === 11) return 4.00
if (CBPitch === 22) return 4.00
if (CBPitch === 33) return 3.33
if (CBPitch === 44) return 2.50
if (CBPitch === 50) return 2.22
if (CBPitch === 55) return 2.00
if (CBPitch === 66) return 1.68
if (CBPitch === 77) return 1.43
if (CBPitch === 88) return 1.25
if (CBPitch === 100) return 1.11
} else if (BBHeight === 25) {
if (CBPitch === 11) return 3.85
if (CBPitch === 22) return 3.85
if (CBPitch === 33) return 3.25
if (CBPitch === 44) return 2.45
if (CBPitch === 50) return 2.17
if (CBPitch === 55) return 1.95
if (CBPitch === 66) return 1.62
if (CBPitch === 77) return 1.40
if (CBPitch === 88) return 1.21
if (CBPitch === 100) return 1.08
} else if (BBHeight === 30) {
if (CBPitch === 11) return 3.80
if (CBPitch === 22) return 3.77
if (CBPitch === 33) return 3.17
if (CBPitch === 44) return 2.40
if (CBPitch === 50) return 2.15
if (CBPitch === 55) return 1.90
if (CBPitch === 66) return 1.58
if (CBPitch === 77) return 1.35
if (CBPitch === 88) return 1.19
if (CBPitch === 100) return 1.06
} else if (BBHeight === 35) {
if (CBPitch === 11) return 3.75
if (CBPitch === 22) return 3.75
if (CBPitch === 33) return 3.08
if (CBPitch === 44) return 2.32
if (CBPitch === 50) return 2.05
if (CBPitch === 55) return 1.86
if (CBPitch === 66) return 1.54
if (CBPitch === 77) return 1.33
if (CBPitch === 88) return 1.17
if (CBPitch === 100) return 1.02
} else if (BBHeight === 40) {
if (CBPitch === 11) return 3.70
if (CBPitch === 22) return 3.65
if (CBPitch === 33) return 3.00
if (CBPitch === 44) return 2.25
if (CBPitch === 50) return 2.00
if (CBPitch === 55) return 1.80
if (CBPitch === 66) return 1.52
if (CBPitch === 77) return 1.28
if (CBPitch === 88) return 1.12
if (CBPitch === 100) return 1.01
} else if (BBHeight === 45) {
if (CBPitch === 11) return 3.65
if (CBPitch === 22) return 3.50
if (CBPitch === 33) return 2.92
if (CBPitch === 44) return 2.18
if (CBPitch === 50) return 1.95
if (CBPitch === 55) return 1.75
if (CBPitch === 66) return 1.46
if (CBPitch === 77) return 1.25
if (CBPitch === 88) return 1.09
if (CBPitch === 100) return 0.97
} else if (BBHeight === 50) {
if (CBPitch === 11) return 3.45
if (CBPitch === 22) return 3.41
if (CBPitch === 33) return 2.83
if (CBPitch === 44) return 2.10
if (CBPitch === 50) return 1.88
if (CBPitch === 55) return 1.69
if (CBPitch === 66) return 1.42
if (CBPitch === 77) return 1.22
if (CBPitch === 88) return 1.06
if (CBPitch === 100) return 0.94
} else if (BBHeight === 55) {
if (CBPitch === 11) return 3.28
if (CBPitch === 22) return 3.26
if (CBPitch === 33) return 2.75
if (CBPitch === 44) return 2.08
if (CBPitch === 50) return 1.84
if (CBPitch === 55) return 1.66
if (CBPitch === 66) return 1.39
if (CBPitch === 77) return 1.18
if (CBPitch === 88) return 1.04
if (CBPitch === 100) return 0.92
} else if (BBHeight === 60) {
if (CBPitch === 11) return 3.20
if (CBPitch === 22) return 3.18
if (CBPitch === 33) return 2.67
if (CBPitch === 44) return 1.98
if (CBPitch === 50) return 1.76
if (CBPitch === 55) return 1.59
if (CBPitch === 66) return 1.35
if (CBPitch === 77) return 1.14
if (CBPitch === 88) return 0.99
if (CBPitch === 100) return 0.89
} else if (BBHeight === 65) {
if (CBPitch === 33) return 2.58
if (CBPitch === 44) return 1.93
if (CBPitch === 50) return 1.71
if (CBPitch === 66) return 1.30
if (CBPitch === 100) return 0.86
} else if (BBHeight === 70) {
if (CBPitch === 33) return 2.50
if (CBPitch === 44) return 1.88
if (CBPitch === 50) return 1.66
if (CBPitch === 66) return 1.25
if (CBPitch === 100) return 0.83
} else if (BBHeight === 75) {
if (CBPitch === 33) return 2.42
if (CBPitch === 44) return 1.82
if (CBPitch === 50) return 1.62
if (CBPitch === 66) return 1.20
if (CBPitch === 100) return 0.81
} else if (BBHeight === 80) {
if (CBPitch === 33) return 2.33
if (CBPitch === 44) return 1.75
if (CBPitch === 50) return 1.58
if (CBPitch === 66) return 1.15
if (CBPitch === 100) return 0.78
} else if (BBHeight === 85) {
if (CBPitch === 33) return 2.25
if (CBPitch === 44) return 1.71
if (CBPitch === 50) return 1.52
if (CBPitch === 66) return 1.13
if (CBPitch === 100) return 0.75
} else if (BBHeight === 90) {
if (CBPitch === 33) return 2.17
if (CBPitch === 44) return 1.67
if (CBPitch === 50) return 1.45
if (CBPitch === 66) return 1.11
if (CBPitch === 100) return 0.72
} else if (BBHeight === 95) {
if (CBPitch === 33) return 2.08
if (CBPitch === 44) return 1.59
if (CBPitch === 50) return 1.39
if (CBPitch === 66) return 1.05
if (CBPitch === 100) return 0.69
} else if (BBHeight === 100) {
if (CBPitch === 33) return 2.00
if (CBPitch === 44) return 1.50
if (CBPitch === 50) return 1.33
if (CBPitch === 66) return 0.99
if (CBPitch === 100) return 0.66
}
}
function m_forge(bearingBarHeight, crossBarPitch) {
const CBPitch = Number(crossBarPitch)
const BBHeight = Number(bearingBarHeight)
if (BBHeight === 20) {
if (CBPitch === 38) return 2.25
if (CBPitch === 50) return 1.35
if (CBPitch === 76) return 0.81
if (CBPitch === 100) return 0.52
} else if (BBHeight === 25) {
if (CBPitch === 38) return 2.19
if (CBPitch === 50) return 1.28
if (CBPitch === 76) return 0.77
if (CBPitch === 100) return 0.52
} else if (BBHeight === 30) {
if (CBPitch === 38) return 2.13
if (CBPitch === 50) return 1.25
if (CBPitch === 76) return 0.75
if (CBPitch === 100) return 0.48
} else if (BBHeight === 35) {
if (CBPitch === 38) return 2.06
if (CBPitch === 50) return 1.20
if (CBPitch === 76) return 0.71
if (CBPitch === 100) return 0.47
} else if (BBHeight === 40) {
if (CBPitch === 38) return 2.00
if (CBPitch === 50) return 1.17
if (CBPitch === 76) return 0.67
if (CBPitch === 100) return 0.45
} else if (BBHeight === 45) {
if (CBPitch === 38) return 1.94
if (CBPitch === 50) return 1.11
if (CBPitch === 76) return 0.65
if (CBPitch === 100) return 0.43
} else if (BBHeight === 50) {
if (CBPitch === 38) return 1.88
if (CBPitch === 50) return 1.05
if (CBPitch === 76) return 0.62
if (CBPitch === 100) return 0.40
} else if (BBHeight === 55) {
if (CBPitch === 38) return 1.81
if (CBPitch === 50) return 1.03
if (CBPitch === 76) return 0.61
if (CBPitch === 100) return 0.38
} else if (BBHeight === 60) {
if (CBPitch === 38) return 1.75
if (CBPitch === 50) return 1.00
if (CBPitch === 76) return 0.59
if (CBPitch === 100) return 0.36
} else if (BBHeight === 65) {
if (CBPitch === 38) return 1.69
} else if (BBHeight === 70) {
if (CBPitch === 38) return 1.63
} else if (BBHeight === 75) {
if (CBPitch === 38) return 1.56
} else if (BBHeight === 80) {
if (CBPitch === 38) return 1.50
}
}
let m = () => {
if (forge) return m_forge(bearingBarHeight, crossBarPitch)
else return m_pressured(bearingBarHeight, crossBarPitch)
}
const c4 = () => {
if (custom) return 200
else return vehiclesClasses[vehicleClass].c4
}
const d4 = () => {
if (custom) return 200
else return vehiclesClasses[vehicleClass].d4
}
const pointedLoadValue = () => {
if (custom) return req.body.pointedLoadValue
else return vehiclesClasses[vehicleClass].pointedLoad
}
/////////////////////////////////////////////////////////////// computed variables
const MaxM = (((pointedLoadValue() * 1000) * (clearSpan - (d4() / 2))) / 4) * 1.5
const n = (c4() / bearingBarPitch) + m()
const W = () => {
const temp = (((bearingBarThickness * Math.pow(bearingBarHeight, 2)) / 6) * n)
return forge ? temp : (temp * 0.9)
}
const sigma = MaxM / W()
const L = () => {
const temp = (((bearingBarThickness * (Math.pow(bearingBarHeight, 3))) / 12) * n)
return forge ? temp : (temp * 0.9)
}
const D = ((pointedLoadValue() * 1000) / (384 * 210000 * L())) * ((8 * Math.pow(clearSpan, 3)) - (4 * clearSpan * Math.pow(d4(), 2)) + (Math.pow(d4(), 3)))
// console.log('m =' + m())
// console.log('c4 =' + c4())
// console.log('d4 =' + d4())
// console.log('pointedLoadValue =' + pointedLoadValue())
// console.log('MaxM =' + MaxM)
// console.log('n =' + n)
// console.log('W =' + W())
// console.log('sigma =' + sigma)
// console.log('L =' + L())
// console.log('D =' + D)
//////////////////////////////////////////////////////////////// calculate result
const result = {
sigma: null,
sigmaStatus: null,
deflection: null,
deflectionStatus: null
}
if (sigma < 235) {
result.sigma = validationMessages[locale].sigma_ok
result.sigmaStatus = true
} else {
result.sigma = validationMessages[locale].sigma_out_of_range
result.sigmaStatus = false
}
if (D < (clearSpan / 200)) {
result.deflection = validationMessages[locale].deflection_ok
result.deflectionStatus = true
} else {
result.deflection = validationMessages[locale].deflection_out_of_range
result.deflectionStatus = false
}
return result
}
///////// distributed load
function distributedLoad() {
// static variables
const forge = gratingType === 'forge'
const custom = method === 'custom'
// computing distributedLoadValue
const distributedLoadValue = () => {
if (custom) return req.body.distributedLoadValue
else return vehiclesClasses[vehicleClass].distributedLoad
}
////////////////////////////////////////////////////////////////// computed variables
const MaxM = (((distributedLoadValue() * 10) * bearingBarPitch * Math.pow(clearSpan, 2)) / 80000) * 1.5
const W = () => {
const temp = (bearingBarThickness * Math.pow(bearingBarHeight, 2)) / 6
return forge ? temp : (temp * 0.9)
}
const sigma = MaxM / W()
const L = () => {
const temp = (bearingBarThickness * Math.pow(bearingBarHeight, 3)) / 12
return forge ? temp : (temp * 0.9)
}
const D = (5 * distributedLoadValue() * bearingBarPitch * Math.pow(clearSpan, 4)) / (384 * 21000 * L() * Math.pow(10, 4))
////////////////////////////////////////////////////////////////// calculate result
const result = {
sigma: null,
sigmaStatus: null,
deflection: null,
deflectionStatus: null
}
if (sigma < 235) {
result.sigma = validationMessages[locale].sigma_ok
result.sigmaStatus = true
} else {
result.sigma = validationMessages[locale].sigma_out_of_range
result.sigmaStatus = false
}
if (D < (clearSpan / 200)) {
result.deflection = validationMessages[locale].deflection_ok
result.deflectionStatus = true
} else {
result.deflection = validationMessages[locale].deflection_out_of_range
result.deflectionStatus = false
}
return result
}
////////////////////////////////////////////// calculate
if (method === 'custom') {
if (loadType === 'pointedLoad') return res.json(pointedLoad())
else return res.json(distributedLoad())
} else if (method === 'classified') {
if (vehicleClass === 'class1') return res.json(distributedLoad())
else return res.json(pointedLoad())
} else return res.json({message: 'unsupported entry'})
}
]
module.exports.getRelativeValues = [
[
body('gratingType')
.notEmpty().withMessage((value, {req}) => {
return validationMessages[req.body.locale].gratingType
})
.bail()
.custom((value, {req}) => {
if (value === 'forge' || value === 'pressured') return true
else Promise.reject(validationMessages[req.body.locale].gratingType_not_supported)
}),
body('crossBarPitch')
.notEmpty().withMessage((value, {req}) => {
return validationMessages[req.body.locale].crossBarPitch
})
.bail()
.custom((value, {req}) => {
if (req.body.gratingType === 'forge') {
if (values.crossBarPitch_forge.includes(Number(value))) return true
else return Promise.reject(validationMessages[req.body.locale].crossBarPitch_not_ok)
} else {
if (values.crossBarPitch_pressured.includes(Number(value))) return true
else return Promise.reject(validationMessages[req.body.locale].crossBarPitch_not_ok)
}
})
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json(errors.mapped())
return res.json(relativeBearingBarHeight(req.body.gratingType, req.body.crossBarPitch))
}
]
module.exports.getValues = [
(req, res) => {
return res.json(values)
}
]
+134 -128
View File
@@ -5,162 +5,168 @@ const jimp = require('jimp')
const fs = require('fs')
const v_m = require('../validation_messages')
// local variables
const imageWidth = 648
const imageHeight = 443
const imageQuality = 100
module.exports.create = [
[
body('fa_title')
.notEmpty().withMessage(v_m['fa'].required.title),
[
body('fa_title')
.notEmpty().withMessage(v_m['fa'].required.title),
body('en_title')
.notEmpty().withMessage(v_m['fa'].required.title),
body('en_title')
.notEmpty().withMessage(v_m['fa'].required.title),
body('year')
.notEmpty().withMessage(v_m['fa'].required.date)
.bail()
.isNumeric().withMessage(v_m['fa'].format.number)
.bail()
.isLength({min: 4}).withMessage(v_m['fa'].min_char.min4)
.bail()
.isLength({max: 4}).withMessage(v_m['fa'].max_char.max4)
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
if (!req.files || !req.files.image) return res.status(422).json({validation: {image: {msg: v_m['fa'].required.image}}})
body('year')
.notEmpty().withMessage(v_m['fa'].required.date)
.bail()
.isNumeric().withMessage(v_m['fa'].format.number)
.bail()
.isLength({min: 4}).withMessage(v_m['fa'].min_char.min4)
.bail()
.isLength({max: 4}).withMessage(v_m['fa'].max_char.max4)
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
if (!req.files || !req.files.image) return res.status(422).json({validation: {image: {msg: v_m['fa'].required.image}}})
const fileName = 'history_' + Date.now() + '.' + req.files.image.mimetype.split('/')[1]
const fileName = 'history_' + Date.now() + '.' + req.files.image.mimetype.split('/')[1]
const data = {
image: fileName,
year: Number(req.body.year),
history_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')
}
const data = {
image: fileName,
year: Number(req.body.year),
history_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')
}
jimp.read(req.files.image.data)
.then(img => {
img
.resize(380, jimp.AUTO)
.cover(380, 260)
.write(`./static/uploads/images/history/${fileName}`, cb => {
const history = new History(data)
history.save(err => {
if (err) console.log(err)
return res.json(v_m['fa'].response.success_save)
})
})
})
.catch(err => {
console.log(err)
})
}
jimp.read(req.files.image.data)
.then(img => {
img
.resize(imageWidth, jimp.AUTO)
.cover(imageWidth, imageHeight)
.quality(imageQuality)
.write(`./static/uploads/images/history/${fileName}`, cb => {
const history = new History(data)
history.save(err => {
if (err) console.log(err)
return res.json(v_m['fa'].response.success_save)
})
})
})
.catch(err => {
console.log(err)
})
}
]
module.exports.getAll = [
(req, res) => {
History.find({}).sort({year: 1}).exec((err, data) => {
if (err) console.log(err)
return res.json(data)
})
}
(req, res) => {
History.find({}).sort({year: 1}).exec((err, data) => {
if (err) console.log(err)
return res.json(data)
})
}
]
module.exports.getOne = [
(req, res) => {
History.findById(req.params.id, (err, data) => {
if (err) return res.status(404).json(err)
return res.json(data)
})
}
(req, res) => {
History.findById(req.params.id, (err, data) => {
if (err) return res.status(404).json(err)
return res.json(data)
})
}
]
module.exports.update = [
[
body('fa_title')
.notEmpty().withMessage(v_m['fa'].required.title),
[
body('fa_title')
.notEmpty().withMessage(v_m['fa'].required.title),
body('en_title')
.notEmpty().withMessage(v_m['fa'].required.title),
body('en_title')
.notEmpty().withMessage(v_m['fa'].required.title),
body('year')
.notEmpty().withMessage(v_m['fa'].required.date)
.bail()
.isNumeric().withMessage(v_m['fa'].format.number)
.bail()
.isLength({min: 4}).withMessage(v_m['fa'].min_char.min4)
.bail()
.isLength({max: 4}).withMessage(v_m['fa'].max_char.max4)
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
body('year')
.notEmpty().withMessage(v_m['fa'].required.date)
.bail()
.isNumeric().withMessage(v_m['fa'].format.number)
.bail()
.isLength({min: 4}).withMessage(v_m['fa'].min_char.min4)
.bail()
.isLength({max: 4}).withMessage(v_m['fa'].max_char.max4)
],
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
const data = {
year: Number(req.body.year),
history_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 = {
year: Number(req.body.year),
history_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 && req.files.image) {
let fileName = 'history_' + Date.now() + '.' + req.files.image.mimetype.split('/')[1]
data.image = fileName
jimp.read(req.files.image.data)
.then(img => {
img
.resize(380, jimp.AUTO)
.cover(380, 260)
.write(`./static/uploads/images/history/${fileName}`, cb => {
History.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
fs.unlink(`./static/${oldData.image}`, err => {
if (err) console.log(err)
return res.json(v_m['fa'].response.success_save)
})
})
if (req.files && req.files.image) {
let fileName = 'history_' + Date.now() + '.' + req.files.image.mimetype.split('/')[1]
data.image = fileName
jimp.read(req.files.image.data)
.then(img => {
img
.resize(imageWidth, jimp.AUTO)
.cover(imageWidth, imageHeight)
.quality(imageQuality)
.write(`./static/uploads/images/history/${fileName}`, cb => {
History.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
fs.unlink(`./static/${oldData.image}`, err => {
if (err) console.log(err)
return res.json(v_m['fa'].response.success_save)
})
})
.catch(err => {
console.log(err)
})
} else {
History.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
return res.json(v_m['fa'].response.success_save)
})
})
})
}
}
.catch(err => {
console.log(err)
})
} else {
History.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
return res.json(v_m['fa'].response.success_save)
})
}
}
]
module.exports.delete = [
(req, res) => {
History.findByIdAndDelete(req.params.id, (err, data) => {
if (err) console.log(err)
fs.unlink(`./static/${data.image}`, err => {
if (err) console.log(err)
return res.json(v_m['fa'].response.success_remove)
})
(req, res) => {
History.findByIdAndDelete(req.params.id, (err, data) => {
if (err) console.log(err)
fs.unlink(`./static/${data.image}`, err => {
if (err) console.log(err)
return res.json(v_m['fa'].response.success_remove)
})
}
})
}
]
+38 -36
View File
@@ -4,47 +4,49 @@ const v_m = require('../validation_messages')
const fs = require('fs')
module.exports.create = [
(req, res) => {
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}}})
(req, res) => {
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 (catalogs.length) {
let firstFile = catalogs[0]
fs.unlink(`./static/${firstFile.file}`, err => {
if (err) console.log(err)
firstFile.file = req.files.pdf.name
firstFile.created_at = dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
firstFile.save((err1, data) => {
if (err1) console.log(err1)
req.files.pdf.mv(`./static/uploads/pdf/${req.files.pdf.name}`)
return res.json(firstFile)
})
MainCatalog.find({}, (err, catalogs) => {
if (catalogs.length) {
let firstFile = catalogs[0]
})
} else {
new MainCatalog({
file: req.files.pdf.name,
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}).save((err, data) => {
if (err) console.log(err)
req.files.pdf.mv(`./static/uploads/pdf/${req.files.pdf.name}`)
return res.json(data)
})
}
})
if (firstFile.file[req.body.locale]) fs.unlink(`./static/${firstFile.file[req.body.locale]}`, err => {
if (err) console.log(err)
})
}
firstFile.file[req.body.locale] = req.body.locale + '_' + req.files.pdf.name
firstFile.updated_at = dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
firstFile.save((err1, data) => {
if (err1) console.log(err1)
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
new MainCatalog(data).save((err, data) => {
if (err) console.log(err)
req.files.pdf.mv(`./static/uploads/pdf/${req.body.locale}_${req.files.pdf.name}`)
return res.json(data)
})
}
})
}
]
module.exports.get = [
(req, res) => {
MainCatalog.find({}, (err, catalog) => {
if (err) console.log(err)
if (catalog.length) return res.json(catalog[0])
else return res.json({})
})
}
(req, res) => {
MainCatalog.find({}, (err, catalog) => {
if (err) console.log(err)
if (catalog.length) return res.json(catalog[0])
else return res.json({})
})
}
]
+76 -75
View File
@@ -5,101 +5,102 @@ const dateformat = require('dateformat')
const v_m = require('../validation_messages')
module.exports.create = [
[
body('fa_name')
.notEmpty().withMessage(v_m['fa'].required.title),
[
body('fa_name')
.notEmpty().withMessage(v_m['fa'].required.title),
body('en_name')
.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_name')
.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 = {
category_details: {
fa: {
name: req.body.fa_name
},
en: {
name: req.body.en_name
}
},
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
const data = {
category_details: {
fa: {
name: req.body.fa_name
},
en: {
name: req.body.en_name
}
},
created_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
const category = new ProductCategory(data)
category.save(err => {
if (err) return res.status(500).json({message: err})
return res.json({message: v_m['fa'].response.success_save})
})
}
const category = new ProductCategory(data)
category.save(err => {
if (err) return res.status(500).json({message: err})
return res.json({message: v_m['fa'].response.success_save})
})
}
]
module.exports.getAll = [
(req, res) => {
ProductCategory.find({}, (err, categories) => {
if (err) return res.status(500).json({message: err})
return res.json(categories)
})
}
(req, res) => {
ProductCategory.find({}, (err, categories) => {
if (err) return res.status(500).json({message: err})
return res.json(categories)
})
}
]
module.exports.getOne = [
(req, res) => {
ProductCategory.findById(req.params.id, (err, category) => {
if (err) console.log(err)
return res.json(category)
})
}
(req, res) => {
ProductCategory.findById(req.params.id, (err, category) => {
if (err) console.log(err)
return res.json(category)
})
}
]
module.exports.update = [
[
body('fa_name')
.notEmpty().withMessage(v_m['fa'].required.title),
[
body('fa_name')
.notEmpty().withMessage(v_m['fa'].required.title),
body('en_name')
.notEmpty().withMessage(v_m['fa'].required.title)
],
(req, res) => {
// check validation results
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
body('en_name')
.notEmpty().withMessage(v_m['fa'].required.title)
],
(req, res) => {
// check validation results
const errors = validationResult(req)
if (!errors.isEmpty()) return res.status(422).json({validation: errors.mapped()})
const data = {
category_details: {
fa: {
name: req.body.fa_name
},
en: {
name: req.body.en_name
}
}
}
const data = {
category_details: {
fa: {
name: req.body.fa_name
},
en: {
name: req.body.en_name
}
},
updated_at: dateformat(Date.now(), 'yyyy-mm-dd HH:MM:ss')
}
ProductCategory.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})
})
}
ProductCategory.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})
})
}
]
module.exports.delete = [
(req, res) => {
Product.findOne({category: req.params.id}, (err, product) => {
if (err) console.log(err)
if (product) return res.status(400).json({message: 'این دسته بندی دارای محصول است.'})
else {
ProductCategory.findByIdAndDelete(req.params.id, (err, category) => {
if (err) return res.status(404).json({message: err})
return res.json({message: v_m['fa'].response.success_remove})
})
}
})
}
(req, res) => {
Product.findOne({category: req.params.id}, (err, product) => {
if (err) console.log(err)
if (product) return res.status(400).json({message: 'این دسته بندی دارای محصول است.'})
else {
ProductCategory.findByIdAndDelete(req.params.id, (err, category) => {
if (err) return res.status(404).json({message: err})
return res.json({message: v_m['fa'].response.success_remove})
})
}
})
}
]
File diff suppressed because it is too large Load Diff
+9 -5
View File
@@ -1,15 +1,19 @@
const mongoose = require('mongoose')
function catalog(pdf) {
return '/uploads/pdf/' + pdf
if (pdf) return '/uploads/pdf/' + pdf
}
const MainCatalogSchema = mongoose.Schema({
file: {type: String, get: catalog},
created_at: String
file: {
fa: {type: String, get: catalog},
en: {type: String, get: catalog}
},
created_at: String,
updated_at: String
}, {
toObject: {getters: true},
toJSON: {getters: true}
toObject: {getters: true},
toJSON: {getters: true}
})
module.exports = mongoose.model('MainCatalog', MainCatalogSchema)
+68 -63
View File
@@ -1,84 +1,89 @@
const mongoose = require('mongoose')
function productImage(img) {
return '/uploads/images/products/' + img
if (img) return '/uploads/images/products/' + img
}
function productPDF(file) {
return '/uploads/pdf/' + file
if (file) return '/uploads/pdf/' + file
}
const ProductImageSchema = mongoose.Schema({
image: {type: String, get: productImage},
created_at: String
image: {type: String, get: productImage},
created_at: String
}, {
toObject: {getters: true},
toJSON: {getters: true}
toObject: {getters: true},
toJSON: {getters: true}
})
const ProductPDFSchema = mongoose.Schema({
file: {type: String, get: productPDF},
pdf_details: {
fa: {
name: String
},
en: {
name: String
}
},
created_at: String
file: {
fa: {type: String, get: productPDF},
en: {type: String, get: productPDF}
},
pdf_details: {
fa: {
name: String
},
en: {
name: String
}
},
created_at: String
}, {
toObject: {getters: true},
toJSON: {getters: true}
toObject: {getters: true},
toJSON: {getters: true}
})
const ProductSchema = mongoose.Schema({
cover: {type: String, get: productImage},
images: [ProductImageSchema],
more_section_image1: {type: String, get: productImage},
more_section_image2: {type: String, get: productImage},
render_image1: {type: String, get: productImage},
render_image2: {type: String, get: productImage},
chart_image: {type: String, get: productImage},
pdf_files: [ProductPDFSchema],
category: String,
more_section: {type: Boolean, default: true},
download_section: {type: Boolean, default: true},
product_details: {
fa: {
name: String,
short_description: String,
page_title: String,
page_description: String,
more_title1: String,
more_description1: String,
more_title2: String,
more_description2: String,
render_caption1: String,
render_caption2: String,
chart_title: String,
chart_description: String
},
en: {
name: String,
short_description: String,
page_title: String,
page_description: String,
more_title1: String,
more_description1: String,
more_title2: String,
more_description2: String,
render_caption1: String,
render_caption2: String,
chart_title: String,
chart_description: String
}
},
created_at: String,
updated_at: String
cover: {type: String, get: productImage},
thumb: {type: String, get: productImage},
images: [ProductImageSchema],
more_section_image1: {type: String, get: productImage},
more_section_image2: {type: String, get: productImage},
render_image1: {type: String, get: productImage},
render_image2: {type: String, get: productImage},
chart_image: {type: String, get: productImage},
pdf_files: [ProductPDFSchema],
category: String,
more_section: {type: Boolean, default: true},
download_section: {type: Boolean, default: true},
favorite: {type: Boolean, default: false},
product_details: {
fa: {
name: String,
short_description: String,
page_title: String,
page_description: String,
more_title1: String,
more_description1: String,
more_title2: String,
more_description2: String,
render_caption1: String,
render_caption2: String,
chart_title: String,
chart_description: String
},
en: {
name: String,
short_description: String,
page_title: String,
page_description: String,
more_title1: String,
more_description1: String,
more_title2: String,
more_description2: String,
render_caption1: String,
render_caption2: String,
chart_title: String,
chart_description: String
}
},
created_at: String,
updated_at: String
}, {
toObject: {getters: true},
toJSON: {getters: true}
toObject: {getters: true},
toJSON: {getters: true}
})
+7 -1
View File
@@ -12,6 +12,7 @@ const contactPageController = require('../controllers/contactPageController')
const contactUsReasonController = require('../controllers/contactUsReasonController')
const mainCatalogController = require('../controllers/mainCatalogController')
const historyController = require('../controllers/historyController')
const calculationModule = require('../controllers/calculationModule')
/////////////////////////////////////////////////////////// routes
//////////////// slider
@@ -31,7 +32,7 @@ router.get('/productCategories', productCategoryController.getAll)
router.get('/productCategories/:id', productCategoryController.getOne)
//////////////// products
router.get('/products/:category', productController.getAllProductsByCategory)
router.get('/favoriteProducts', productController.getFavoriteProducts)
router.get('/products/', productController.getAllProducts)
router.get('/product/:id', productController.getOneProduct)
@@ -51,4 +52,9 @@ router.get('/catalog', mainCatalogController.get)
router.get('/history', historyController.getAll)
router.get('/history/:id', historyController.getOne)
//////////////// calculation
router.get('/calculation/values', calculationModule.getValues)
router.post('/calculation/relativeValues', calculationModule.getRelativeValues)
router.post('/calculate', calculationModule.calculate)
module.exports = router