update:add ci cd files ==> do not edit those file
This commit is contained in:
@@ -0,0 +1,221 @@
|
||||
const GalleryCategory = require('../models/GalleryCategory')
|
||||
const Gallery = require('../models/Gallery')
|
||||
const {body, validationResult} = require('express-validator')
|
||||
const {res404, res500, checkValidations} = require('../plugins/controllersHelperFunctions')
|
||||
const {_sr} = require('../plugins/serverResponses')
|
||||
const sharp = require('sharp')
|
||||
const fs = require('fs')
|
||||
|
||||
const imageQuality = 100
|
||||
const imageThumbWidth = 500
|
||||
const imageThumbHeight = 500
|
||||
const imageThumbQuality = 80
|
||||
|
||||
module.exports.create = [
|
||||
[
|
||||
body('fa_title')
|
||||
.notEmpty().withMessage(_sr['fa'].required.title)
|
||||
.bail()
|
||||
.custom((value, {req}) => {
|
||||
return GalleryCategory.findOne({$or: [{'locale.fa.title': value}, {'locale.en.title': value}]})
|
||||
.then(category => {
|
||||
if (category) return Promise.reject(_sr['fa'].duplicated.title)
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
|
||||
body('en_title')
|
||||
.notEmpty().withMessage(_sr['fa'].required.title)
|
||||
.bail()
|
||||
.custom((value, {req}) => {
|
||||
return GalleryCategory.findOne({$or: [{'locale.fa.title': value}, {'locale.en.title': value}]})
|
||||
.then(category => {
|
||||
if (category) return Promise.reject(_sr['fa'].duplicated.title)
|
||||
else return true
|
||||
})
|
||||
})
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
const {fa_title, en_title, fa_description, en_description , index} = req.body
|
||||
if (!req.files || !req.files.cover) return res.status(422).json({validation: {cover: {msg: _sr['fa'].required.cover}}})
|
||||
if (!_sr.supportedImageFormats.includes(req.files.cover.mimetype.split('/')[1])) return res.status(422).json({validation: {cover: {msg: _sr['fa'].format.image}}})
|
||||
|
||||
const image = req.files.cover
|
||||
const imageName = 'categoryIMG_' + Date.now() + '.jpg'
|
||||
const imageThumb = 'thumb_' + imageName
|
||||
|
||||
const data = {
|
||||
locale: {
|
||||
fa: {
|
||||
title: fa_title,
|
||||
description: fa_description
|
||||
},
|
||||
en: {
|
||||
title: en_title,
|
||||
description: en_description
|
||||
}
|
||||
},
|
||||
index
|
||||
}
|
||||
|
||||
try {
|
||||
const target = await sharp(image.data)
|
||||
await target
|
||||
.toFormat('jpg')
|
||||
.jpeg({quality: imageQuality})
|
||||
.toFile(`./static/uploads/images/galleryCover/${imageName}`)
|
||||
|
||||
await target
|
||||
.resize(imageThumbWidth, imageThumbHeight, {fit: 'cover'})
|
||||
.withMetadata()
|
||||
.toFormat('jpg')
|
||||
.jpeg({quality: imageThumbQuality})
|
||||
.toFile(`./static/uploads/images/galleryCover/thumb/${imageThumb}`)
|
||||
|
||||
data.cover = imageName
|
||||
data.thumb = imageThumb
|
||||
|
||||
} catch (e) {
|
||||
return res500(res, e)
|
||||
}
|
||||
const category = new GalleryCategory(data)
|
||||
category.save(err => {
|
||||
if (err) return res500(res, err)
|
||||
else return res.json({message: _sr['fa'].response.success_save})
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.getAll = [
|
||||
(req, res) => {
|
||||
GalleryCategory.find({}, (err, categories) => {
|
||||
if (err) return res500(res, err)
|
||||
else return res.json(categories)
|
||||
}).sort({index : 1})
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.getOne = [
|
||||
(req, res) => {
|
||||
GalleryCategory.findById(req.params.id, (err, category) => {
|
||||
if (err) return res500(res, err)
|
||||
else return res.json(category)
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.update = [
|
||||
[
|
||||
body('fa_title')
|
||||
.notEmpty().withMessage(_sr['fa'].required.title)
|
||||
.bail()
|
||||
.custom((value, {req}) => {
|
||||
return GalleryCategory.findOne({$or: [{'locale.fa.title': value}, {'locale.en.title': value}]})
|
||||
.then(category => {
|
||||
if (category && category._id.toString() !== req.params.id) return Promise.reject(_sr['fa'].duplicated.title)
|
||||
else return true
|
||||
})
|
||||
}),
|
||||
|
||||
body('en_title')
|
||||
.notEmpty().withMessage(_sr['fa'].required.title)
|
||||
.bail()
|
||||
.custom((value, {req}) => {
|
||||
return GalleryCategory.findOne({$or: [{'locale.fa.title': value}, {'locale.en.title': value}]})
|
||||
.then(category => {
|
||||
if (category && category._id.toString() !== req.params.id) return Promise.reject(_sr['fa'].duplicated.title)
|
||||
else return true
|
||||
})
|
||||
})
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
const {fa_title, en_title, fa_description, en_description,index} = req.body
|
||||
const image = req?.files?.cover
|
||||
|
||||
const data = {
|
||||
locale: {
|
||||
fa: {
|
||||
title: fa_title,
|
||||
description: fa_description
|
||||
},
|
||||
en: {
|
||||
title: en_title,
|
||||
description: en_description
|
||||
}
|
||||
},
|
||||
index
|
||||
}
|
||||
|
||||
|
||||
if (image) {
|
||||
if (!_sr.supportedImageFormats.includes(req.files.cover.mimetype.split('/')[1])) return res.status(422).json({validation: {cover: {msg: _sr['fa'].format.image}}})
|
||||
|
||||
|
||||
const imageName = 'categoryIMG_' + Date.now() + '.jpg'
|
||||
const imageThumb = 'thumb_' + imageName
|
||||
try {
|
||||
const target = await sharp(image.data)
|
||||
await target
|
||||
.toFormat('jpg')
|
||||
.jpeg({quality: imageQuality})
|
||||
.toFile(`./static/uploads/images/galleryCover/${imageName}`)
|
||||
|
||||
await target
|
||||
.resize(imageThumbWidth, imageThumbHeight, {fit: 'cover'})
|
||||
.withMetadata()
|
||||
.toFormat('jpg')
|
||||
.jpeg({quality: imageThumbQuality})
|
||||
.toFile(`./static/uploads/images/galleryCover/thumb/${imageThumb}`)
|
||||
|
||||
data.cover = imageName
|
||||
data.thumb = imageThumb
|
||||
|
||||
} catch (e) {
|
||||
return res500(res, e)
|
||||
}
|
||||
}
|
||||
|
||||
GalleryCategory.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
|
||||
if (err || !oldData) return res404(res, err)
|
||||
else {
|
||||
if (image && oldData.cover) {
|
||||
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: _sr['fa'].response.success_save})
|
||||
}
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.delete = [
|
||||
async (req, res) => {
|
||||
try {
|
||||
const posts = await Gallery.find({category: req.params.id})
|
||||
if (posts.length) return res.status(403).json({message: 'Can not remove categories that have posts.'})
|
||||
GalleryCategory.findByIdAndRemove(req.params.id, (err, oldData) => {
|
||||
if (err || !oldData) return res404(res, err)
|
||||
else {
|
||||
|
||||
if (oldData.cover) {
|
||||
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: _sr['fa'].response.success_remove})
|
||||
}
|
||||
})
|
||||
} catch (e) {
|
||||
return res500(res, e)
|
||||
}
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user