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