Update brandController.js
This commit is contained in:
@@ -16,6 +16,10 @@ const createbrand = [
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
const { title, link, description } = req.body;
|
||||
const data = {
|
||||
title, link, description,
|
||||
}
|
||||
|
||||
if (req.files?.image) {
|
||||
const image = req.files.image
|
||||
@@ -31,21 +35,15 @@ const createbrand = [
|
||||
await image.mv(`./static/uploads/images/brands/${fileName}`)
|
||||
}
|
||||
|
||||
const { title, link, description } = req.body;
|
||||
|
||||
|
||||
|
||||
const linkAvailable = await Brand.findOne({ link });
|
||||
if (linkAvailable) {
|
||||
res.status(400);
|
||||
throw new Error(_sr.fa.duplicated.link);
|
||||
res.status(400).json(_sr.fa.duplicated.link);
|
||||
}
|
||||
|
||||
const brand = await Brand.create({
|
||||
title,
|
||||
link,
|
||||
description,
|
||||
logo,
|
||||
});
|
||||
const brand = await Brand.create(data);
|
||||
|
||||
res.status(201).json(brand);
|
||||
|
||||
@@ -70,8 +68,7 @@ const getbrands = async (req, res) => {
|
||||
const getbrand = async (req, res) => {
|
||||
const brand = await Brand.findById(req.params.id);
|
||||
if (!brand) {
|
||||
res.status(404)
|
||||
throw new Error(_sr.fa.not_found.item_id)
|
||||
res.status(404).json(_sr.fa.not_found.item_id)
|
||||
}
|
||||
res.status(200).json(brand);
|
||||
};
|
||||
@@ -85,35 +82,42 @@ const updatebrand = [
|
||||
body('title').notEmpty().withMessage(_sr.fa.required.title),
|
||||
body('description').notEmpty().withMessage(_sr.fa.required.description),
|
||||
body('link').notEmpty().trim().isLowercase().withMessage(_sr.fa.required.field),
|
||||
body('logo').notEmpty().withMessage(_sr.fa.required.image),
|
||||
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
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);
|
||||
if (!brand) {
|
||||
res.status(404);
|
||||
throw new Error(_sr.fa.not_found.item_id);
|
||||
res.status(404).json(_sr.fa.not_found.item_id);
|
||||
}
|
||||
|
||||
const linkAvailable = await Brand.findOne({ link });
|
||||
if (linkAvailable && linkAvailable.id != req.params.id) {
|
||||
res.status(400);
|
||||
throw new Error(_sr.fa.duplicated.link);
|
||||
res.status(400).json(_sr.fa.duplicated.link);
|
||||
}
|
||||
|
||||
const updateBrand = await Brand.findByIdAndUpdate(
|
||||
req.params.id,
|
||||
{
|
||||
title,
|
||||
link,
|
||||
description,
|
||||
logo,
|
||||
native,
|
||||
ordering
|
||||
},
|
||||
data,
|
||||
{ new: true }
|
||||
);
|
||||
|
||||
@@ -131,8 +135,7 @@ const deletebrand = async (req, res) => {
|
||||
|
||||
|
||||
if (!brand) {
|
||||
res.status(404);
|
||||
throw new Error(_sr.fa.not_found.item_id);
|
||||
res.status(404).json(_sr.fa.not_found.item_id);
|
||||
}
|
||||
|
||||
await brand.deleteOne();
|
||||
|
||||
Reference in New Issue
Block a user