Update brandController.js
This commit is contained in:
@@ -16,6 +16,10 @@ const createbrand = [
|
|||||||
],
|
],
|
||||||
checkValidations(validationResult),
|
checkValidations(validationResult),
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
|
const { title, link, description } = req.body;
|
||||||
|
const data = {
|
||||||
|
title, link, description,
|
||||||
|
}
|
||||||
|
|
||||||
if (req.files?.image) {
|
if (req.files?.image) {
|
||||||
const image = req.files.image
|
const image = req.files.image
|
||||||
@@ -31,21 +35,15 @@ const createbrand = [
|
|||||||
await image.mv(`./static/uploads/images/brands/${fileName}`)
|
await image.mv(`./static/uploads/images/brands/${fileName}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const { title, link, description } = req.body;
|
|
||||||
|
|
||||||
|
|
||||||
const linkAvailable = await Brand.findOne({ link });
|
const linkAvailable = await Brand.findOne({ link });
|
||||||
if (linkAvailable) {
|
if (linkAvailable) {
|
||||||
res.status(400);
|
res.status(400).json(_sr.fa.duplicated.link);
|
||||||
throw new Error(_sr.fa.duplicated.link);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const brand = await Brand.create({
|
const brand = await Brand.create(data);
|
||||||
title,
|
|
||||||
link,
|
|
||||||
description,
|
|
||||||
logo,
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(201).json(brand);
|
res.status(201).json(brand);
|
||||||
|
|
||||||
@@ -70,8 +68,7 @@ const getbrands = async (req, res) => {
|
|||||||
const getbrand = async (req, res) => {
|
const getbrand = async (req, res) => {
|
||||||
const brand = await Brand.findById(req.params.id);
|
const brand = await Brand.findById(req.params.id);
|
||||||
if (!brand) {
|
if (!brand) {
|
||||||
res.status(404)
|
res.status(404).json(_sr.fa.not_found.item_id)
|
||||||
throw new Error(_sr.fa.not_found.item_id)
|
|
||||||
}
|
}
|
||||||
res.status(200).json(brand);
|
res.status(200).json(brand);
|
||||||
};
|
};
|
||||||
@@ -85,35 +82,42 @@ const updatebrand = [
|
|||||||
body('title').notEmpty().withMessage(_sr.fa.required.title),
|
body('title').notEmpty().withMessage(_sr.fa.required.title),
|
||||||
body('description').notEmpty().withMessage(_sr.fa.required.description),
|
body('description').notEmpty().withMessage(_sr.fa.required.description),
|
||||||
body('link').notEmpty().trim().isLowercase().withMessage(_sr.fa.required.field),
|
body('link').notEmpty().trim().isLowercase().withMessage(_sr.fa.required.field),
|
||||||
body('logo').notEmpty().withMessage(_sr.fa.required.image),
|
|
||||||
],
|
],
|
||||||
checkValidations(validationResult),
|
checkValidations(validationResult),
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
const { title, link, description, logo, native, ordering } = req.body;
|
const { title, link, description } = req.body;
|
||||||
|
const data = {
|
||||||
|
title, link, description,
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.files?.image) {
|
||||||
|
const image = req.files.image
|
||||||
|
const fileName = 'brands' + Date.now() + '.' + image.name
|
||||||
|
const acceptableFormats = ['png', 'jpg', 'jpeg', 'webp']
|
||||||
|
if (!acceptableFormats.includes(image.mimetype.split('/')[1]))
|
||||||
|
return res.status(422).json({ validation: { image: { msg: _faSr.format.image } } })
|
||||||
|
if (image.size / 1024 > maxAttachmentSize)
|
||||||
|
return res
|
||||||
|
.status(422)
|
||||||
|
.json({ validation: { image: { msg: `حجم تصویر نباید بیشتر از ${maxAttachmentSize} KB باشد.` } } })
|
||||||
|
data.image = fileName
|
||||||
|
await image.mv(`./static/uploads/images/brands/${fileName}`)
|
||||||
|
}
|
||||||
|
|
||||||
const brand = await Brand.findById(req.params.id);
|
const brand = await Brand.findById(req.params.id);
|
||||||
if (!brand) {
|
if (!brand) {
|
||||||
res.status(404);
|
res.status(404).json(_sr.fa.not_found.item_id);
|
||||||
throw new Error(_sr.fa.not_found.item_id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const linkAvailable = await Brand.findOne({ link });
|
const linkAvailable = await Brand.findOne({ link });
|
||||||
if (linkAvailable && linkAvailable.id != req.params.id) {
|
if (linkAvailable && linkAvailable.id != req.params.id) {
|
||||||
res.status(400);
|
res.status(400).json(_sr.fa.duplicated.link);
|
||||||
throw new Error(_sr.fa.duplicated.link);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateBrand = await Brand.findByIdAndUpdate(
|
const updateBrand = await Brand.findByIdAndUpdate(
|
||||||
req.params.id,
|
req.params.id,
|
||||||
{
|
data,
|
||||||
title,
|
|
||||||
link,
|
|
||||||
description,
|
|
||||||
logo,
|
|
||||||
native,
|
|
||||||
ordering
|
|
||||||
},
|
|
||||||
{ new: true }
|
{ new: true }
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -131,8 +135,7 @@ const deletebrand = async (req, res) => {
|
|||||||
|
|
||||||
|
|
||||||
if (!brand) {
|
if (!brand) {
|
||||||
res.status(404);
|
res.status(404).json(_sr.fa.not_found.item_id);
|
||||||
throw new Error(_sr.fa.not_found.item_id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await brand.deleteOne();
|
await brand.deleteOne();
|
||||||
|
|||||||
Reference in New Issue
Block a user