transfer project in github
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
const PartySet = require('../models/PartySet')
|
||||
const {body, validationResult} = require('express-validator')
|
||||
const sharp = require('sharp')
|
||||
const fs = require('fs')
|
||||
const {_sr} = require('../plugins/serverResponses')
|
||||
const {res404, res500, checkValidations} = require('../plugins/controllersHelperFunctions')
|
||||
|
||||
///// variables
|
||||
const foodImageWidth = 500
|
||||
const foodImageHeight = 500
|
||||
const foodImageQuality = 100
|
||||
const limit = 20
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////// create
|
||||
module.exports.create = [
|
||||
[
|
||||
body('title')
|
||||
.isLength({min: 2}).withMessage(_sr['fa'].min_char.min2)
|
||||
.bail()
|
||||
.custom((value, {req}) => {
|
||||
return PartySet.findOne({title: value})
|
||||
.then(item => {
|
||||
if (item) return Promise.reject(_sr['fa'].duplicated.title)
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
|
||||
body('index')
|
||||
.notEmpty().withMessage(_sr['fa'].required.index)
|
||||
.bail()
|
||||
.isNumeric().withMessage(_sr['fa'].format.number),
|
||||
|
||||
body('price')
|
||||
.notEmpty().withMessage(_sr['fa'].required.price)
|
||||
.bail()
|
||||
.isNumeric().withMessage(_sr['fa'].format.number),
|
||||
|
||||
body('description')
|
||||
.notEmpty().withMessage(_sr['fa'].required.description)
|
||||
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
if (req.files?.image && !_sr.supportedImageFormats.includes(req.files.image.mimetype.split('/')[1])) return res.status(422).json({validation: {image: {msg: _sr['fa'].format.image}}})
|
||||
|
||||
const {title, description, active, index} = req.body
|
||||
const data = {title, description, active, index}
|
||||
data.price = Number(req.body.price)
|
||||
|
||||
if (req.files?.image) {
|
||||
const image = req.files.image
|
||||
const imageName = 'partySet_' + Date.now() + '.' + image.mimetype.split('/')[1]
|
||||
data.image = imageName
|
||||
try {
|
||||
const target = sharp(image.data)
|
||||
await target.jpeg({quality: foodImageQuality}).resize(foodImageWidth, foodImageHeight, {fit: 'cover'}).toFile(`./static/uploads/images/partySet/${imageName}`)
|
||||
} catch (e) {
|
||||
return res500(res, e)
|
||||
}
|
||||
}
|
||||
const partySet = new PartySet(data)
|
||||
partySet.save((err, data) => {
|
||||
if (err) return res500(res, err)
|
||||
return res.json({message: _sr['fa'].response.success_save})
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////// get all
|
||||
module.exports.getAll = [
|
||||
(req, res) => {
|
||||
PartySet.find({active: true}).sort({index: 1}).exec((err, foods) => {
|
||||
if (err) return res500(res, err)
|
||||
return res.json(foods)
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.getAll_for_admin = [
|
||||
(req, res) => {
|
||||
const filter = {}
|
||||
PartySet.paginate(filter, {
|
||||
page: req.query.page || 1,
|
||||
limit,
|
||||
sort : {index : 1}
|
||||
} , (err, foods) => {
|
||||
if (err) return res500(res, err)
|
||||
return res.json(foods)
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////// get one
|
||||
module.exports.getOne = [
|
||||
(req, res) => {
|
||||
PartySet.findById(req.params.id, (err, food) => {
|
||||
if (err || !food) return res404(res, err)
|
||||
return res.json(food)
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////// update
|
||||
module.exports.update = [
|
||||
[
|
||||
body('title')
|
||||
.isLength({min: 2}).withMessage(_sr['fa'].min_char.min2)
|
||||
.bail()
|
||||
.custom((value, {req}) => {
|
||||
return PartySet.findOne({title: value})
|
||||
.then(item => {
|
||||
if (item && item._id.toString() !== req.params.id) return Promise.reject(_sr['fa'].duplicated.title)
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
|
||||
body('index')
|
||||
.notEmpty().withMessage(_sr['fa'].required.index)
|
||||
.bail()
|
||||
.isNumeric().withMessage(_sr['fa'].format.number),
|
||||
|
||||
body('price')
|
||||
.notEmpty().withMessage(_sr['fa'].required.price)
|
||||
.bail()
|
||||
.isNumeric().withMessage(_sr['fa'].format.number),
|
||||
|
||||
body('description')
|
||||
.notEmpty().withMessage(_sr['fa'].required.description)
|
||||
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
if (req.files?.image && !_sr.supportedImageFormats.includes(req.files.image.mimetype.split('/')[1])) return res.status(422).json({validation: {image: {msg: _sr['fa'].format.image}}})
|
||||
|
||||
const {title, description, active, index} = req.body
|
||||
const data = {title, description, active, index}
|
||||
data.price = Number(req.body.price)
|
||||
|
||||
if (req.files?.image) {
|
||||
const image = req.files.image
|
||||
const imageName = 'partySet_' + Date.now() + '.' + image.mimetype.split('/')[1]
|
||||
data.image = imageName
|
||||
try {
|
||||
const target = sharp(image.data)
|
||||
await target
|
||||
.resize(foodImageWidth, foodImageHeight, {fit: 'cover'})
|
||||
.jpeg({quality: foodImageQuality})
|
||||
.toFile(`./static/uploads/images/partySet/${imageName}`)
|
||||
} catch (e) {
|
||||
return res500(res, e)
|
||||
}
|
||||
}
|
||||
|
||||
PartySet.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
|
||||
if (err || !oldData) return res404(res, err)
|
||||
if (req.files && req.files.image && oldData.image) {
|
||||
fs.unlink(`./static/${oldData.image}`, err => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
}
|
||||
return res.json({message: _sr['fa'].response.success_save})
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////// delete
|
||||
module.exports.delete = [
|
||||
(req, res) => {
|
||||
PartySet.findByIdAndRemove(req.params.id, (err, data) => {
|
||||
if (err || !data) return res404(res, err)
|
||||
if (data.image) {
|
||||
fs.unlink(`./static/${data.image}`, err => {
|
||||
if (err) console.log(err)
|
||||
})
|
||||
}
|
||||
return res.json({message: _sr['fa'].response.success_remove})
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user