From 2bb0a3374b1542d2c72fa6d50cba195432453afb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B5=ED=9D=AC=20=EA=B9=80?= Date: Sun, 19 Jan 2025 21:14:26 +0900 Subject: [PATCH] =?UTF-8?q?=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/subcategory.js | 64 +++++++++++++++++++--------------- src/models/SubCategory.js | 2 +- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/controllers/subcategory.js b/src/controllers/subcategory.js index d0e7bd6..aa382a1 100644 --- a/src/controllers/subcategory.js +++ b/src/controllers/subcategory.js @@ -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, diff --git a/src/models/SubCategory.js b/src/models/SubCategory.js index 2eb8e37..5000790 100644 --- a/src/models/SubCategory.js +++ b/src/models/SubCategory.js @@ -49,7 +49,7 @@ const SubCategorySchema = new mongoose.Schema( type: mongoose.Schema.Types.ObjectId, ref: 'Category', required: true, - }, + } }, { timestamps: true } );