업데이트
This commit is contained in:
parent
b613c87745
commit
2bb0a3374b
@ -1,8 +1,7 @@
|
|||||||
const SubCategories = require('../models/SubCategory');
|
const SubCategories = require("../models/SubCategory");
|
||||||
const Category = require('../models/Category');
|
const Category = require("../models/Category");
|
||||||
const getBlurDataURL = require('../config/getBlurDataURL');
|
const getBlurDataURL = require("../config/getBlurDataURL");
|
||||||
const { singleFileDelete } = require('../config/uploader');
|
const { singleFileDelete } = require("../config/uploader");
|
||||||
|
|
||||||
|
|
||||||
// const createSubCategory = async (req, res) => {
|
// const createSubCategory = async (req, res) => {
|
||||||
// try {
|
// try {
|
||||||
@ -28,15 +27,14 @@ const { singleFileDelete } = require('../config/uploader');
|
|||||||
// }
|
// }
|
||||||
// };
|
// };
|
||||||
|
|
||||||
|
|
||||||
const createSubCategory = async (req, res) => {
|
const createSubCategory = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { cover, ...others } = req.body;
|
const { cover, ...others } = req.body;
|
||||||
|
|
||||||
// 기본 cover 설정
|
// 기본 cover 설정
|
||||||
const defaultCover = {
|
const defaultCover = {
|
||||||
url: '/ui_pattern.png',
|
url: "/ui_pattern.png",
|
||||||
blurDataURL: '/ui_pattern.png',
|
blurDataURL: "/ui_pattern.png",
|
||||||
};
|
};
|
||||||
|
|
||||||
// cover가 없거나 url이 비어있으면 기본 cover 사용
|
// cover가 없거나 url이 비어있으면 기본 cover 사용
|
||||||
@ -60,25 +58,24 @@ const createSubCategory = async (req, res) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
res.status(201).json({ success: true, message: 'SubCategory Created' });
|
res.status(201).json({ success: true, message: "SubCategory Created" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(400).json({ message: error.message });
|
res.status(400).json({ message: error.message });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const getAllSubCategories = async (req, res) => {
|
const getAllSubCategories = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { limit = 10, page = 1, search = '', category } = req.query;
|
const { limit = 10, page = 1, search = "", category } = req.query;
|
||||||
const currentCategory = category
|
const currentCategory = category
|
||||||
? await Category.findOne({ slug: category })
|
? await Category.findOne({ slug: category })
|
||||||
: null;
|
: null;
|
||||||
if (category && !currentCategory) {
|
if (category && !currentCategory) {
|
||||||
res.status(404).json({ message: 'Category not found!' });
|
res.status(404).json({ message: "Category not found!" });
|
||||||
}
|
}
|
||||||
const skip = parseInt(limit) || 10;
|
const skip = parseInt(limit) || 10;
|
||||||
const query = {
|
const query = {
|
||||||
name: { $regex: search, $options: 'i' },
|
name: { $regex: search, $options: "i" },
|
||||||
...(currentCategory && { parentCategory: currentCategory._id }),
|
...(currentCategory && { parentCategory: currentCategory._id }),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -104,11 +101,11 @@ const getSubCategoriesBySlug = async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const { slug } = req.params;
|
const { slug } = req.params;
|
||||||
const subcategories = await SubCategories.findOne({ slug });
|
const subcategories = await SubCategories.findOne({ slug });
|
||||||
const categories = await Category.find().select(['name']);
|
const categories = await Category.find().select(["name"]);
|
||||||
|
|
||||||
if (!subcategories) {
|
if (!subcategories) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
message: 'Subcategory Not Found',
|
message: "Subcategory Not Found",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,12 +120,20 @@ const updateSubCategoriesBySlug = async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const { slug } = req.params;
|
const { slug } = req.params;
|
||||||
const { cover, ...others } = req.body;
|
const { cover, ...others } = req.body;
|
||||||
// Validate if the 'blurDataURL' property exists in the logo object
|
|
||||||
|
// Find the existing subcategory before updating
|
||||||
|
const existingCategory = await SubCategories.findOne({ slug });
|
||||||
|
|
||||||
|
if (!existingCategory) {
|
||||||
|
return res.status(404).json({ message: "SubCategory not found" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate if the 'blurDataURL' property exists in the cover object
|
||||||
if (!cover.blurDataURL) {
|
if (!cover.blurDataURL) {
|
||||||
// If blurDataURL is not provided, generate it using the 'getBlurDataURL' function
|
|
||||||
cover.blurDataURL = await getBlurDataURL(cover.url);
|
cover.blurDataURL = await getBlurDataURL(cover.url);
|
||||||
}
|
}
|
||||||
const currentCategory = await SubCategories.findOneAndUpdate(
|
|
||||||
|
const updatedCategory = await SubCategories.findOneAndUpdate(
|
||||||
{ slug },
|
{ slug },
|
||||||
{
|
{
|
||||||
...others,
|
...others,
|
||||||
@ -138,25 +143,26 @@ const updateSubCategoriesBySlug = async (req, res) => {
|
|||||||
},
|
},
|
||||||
{ new: true, runValidators: true }
|
{ new: true, runValidators: true }
|
||||||
);
|
);
|
||||||
// Check if parent category is updated
|
|
||||||
|
// Check if parent category has changed
|
||||||
if (
|
if (
|
||||||
String(currentCategory.parentCategory) !== String(others.parentCategory)
|
String(existingCategory.parentCategory) !== String(others.parentCategory)
|
||||||
) {
|
) {
|
||||||
// Remove subcategory from old parent category
|
// Remove subcategory from old parent category
|
||||||
await Category.findByIdAndUpdate(currentCategory.parentCategory, {
|
await Category.findByIdAndUpdate(existingCategory.parentCategory, {
|
||||||
$pull: { subCategories: currentCategory._id },
|
$pull: { subCategories: existingCategory._id },
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add subcategory to new parent category
|
// Add subcategory to new parent category
|
||||||
await Category.findByIdAndUpdate(others.parentCategory, {
|
await Category.findByIdAndUpdate(others.parentCategory, {
|
||||||
$addToSet: { subCategories: currentCategory._id },
|
$addToSet: { subCategories: updatedCategory._id },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
success: true,
|
success: true,
|
||||||
message: 'SubCategory Updated',
|
message: "SubCategory Updated",
|
||||||
currentCategory,
|
updatedCategory,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(400).json({ message: error.message });
|
res.status(400).json({ message: error.message });
|
||||||
@ -176,13 +182,13 @@ const deleteSubCategoriesBySlug = async (req, res) => {
|
|||||||
if (!subCategory) {
|
if (!subCategory) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: 'Subcategory Not Found',
|
message: "Subcategory Not Found",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
res
|
res
|
||||||
.status(201)
|
.status(201)
|
||||||
.json({ success: true, message: 'SubCategory Deleted Successfully' });
|
.json({ success: true, message: "SubCategory Deleted Successfully" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(400).json({ message: error.message });
|
res.status(400).json({ message: error.message });
|
||||||
}
|
}
|
||||||
@ -205,8 +211,8 @@ const getSubCategories = async (req, res) => {
|
|||||||
const getSubCategoryNameBySlug = async (req, res) => {
|
const getSubCategoryNameBySlug = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const subcategory = await SubCategories.findOne({ slug: req.params.slug })
|
const subcategory = await SubCategories.findOne({ slug: req.params.slug })
|
||||||
.select(['name', 'slug'])
|
.select(["name", "slug"])
|
||||||
.populate({ path: 'parentCategory', select: ['name', 'slug'] });
|
.populate({ path: "parentCategory", select: ["name", "slug"] });
|
||||||
|
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
success: true,
|
success: true,
|
||||||
|
@ -49,7 +49,7 @@ const SubCategorySchema = new mongoose.Schema(
|
|||||||
type: mongoose.Schema.Types.ObjectId,
|
type: mongoose.Schema.Types.ObjectId,
|
||||||
ref: 'Category',
|
ref: 'Category',
|
||||||
required: true,
|
required: true,
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
{ timestamps: true }
|
{ timestamps: true }
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user