From 136c178e9157c8cf5c2dc5b63093544f917452ed Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Tue, 19 Nov 2024 10:41:31 +0530 Subject: [PATCH] Add delete a pruoduct route --- controllers/attribute.controller.js | 6 +- controllers/product.controller.js | 107 ++++++++++++++++++++++++++++ routes/product.routes.js | 5 ++ 3 files changed, 117 insertions(+), 1 deletion(-) diff --git a/controllers/attribute.controller.js b/controllers/attribute.controller.js index dc58f22..b2e9fda 100644 --- a/controllers/attribute.controller.js +++ b/controllers/attribute.controller.js @@ -142,7 +142,11 @@ const deleteAnAttribute = asyncHandler(async (req, res) => { ) { return ApiResponse.validationError( res, - "Invalid attribute Fromat provided" + "Attribute deletion failed", + { + id: "Invalid attribute ID provided", + }, + 400 ); } ApiResponse.error(res, "Error while deleting attribute", 500, error); diff --git a/controllers/product.controller.js b/controllers/product.controller.js index b1dfc63..ff892ef 100644 --- a/controllers/product.controller.js +++ b/controllers/product.controller.js @@ -4,6 +4,7 @@ const { MONGOOSE_VALIDATION_ERROR, MONGOOSE_CAST_ERROR, VARIABLE, + MONGOOSE_OBJECT_ID, } = require("../constants/models.constants"); const Product = require("../model/product.model"); const invalidFieldMessage = require("../utils/invalidFieldMessage"); @@ -196,9 +197,115 @@ const updateAProduct = asyncHandler(async (req, res) => { } }); +const updateProductThumbnail = asyncHandler(async (req, res) => { + try { + const { id } = req.params; + + const thumbnail = req.file.filenane; + + const product = Product.findByIdAndUpdate( + id, + { thumbnail }, + { new: true, runValidators: true } + ); + + if (!product) { + return ApiResponse.notFound(res, "Product not found"); + } + + ApiResponse.success(res, "Thumbnail Updated SUccessfully", product, 201); + } catch (error) { + if ( + error.name === MONGOOSE_CAST_ERROR && + error.kind === MONGOOSE_OBJECT_ID + ) { + ApiResponse.validationError( + res, + "Thummbnail Update Failed", + { id: "Invalid productId provided" }, + 400 + ); + } + ApiResponse.error(res, "Thumbnail update failed", 500, error); + } +}); + +const updateProductGallery = asyncHandler(async (req, res) => { + try { + const { id } = req.params; + + const gallery = req.files.gallery.map((file) => file.filename); + + const product = Product.findByIdAndUpdate( + id, + { gallery }, + { new: true, runValidators: true } + ); + + if (!product) { + return ApiResponse.notFound(res, "Product not found"); + } + + ApiResponse.success(res, "Gallery. Updated SUccessfully", product, 201); + } catch (error) { + if ( + error.name === MONGOOSE_CAST_ERROR && + error.kind === MONGOOSE_OBJECT_ID + ) { + ApiResponse.validationError( + res, + "Gallery Update Failed", + { id: "Invalid productId provided" }, + 400 + ); + } + ApiResponse.error(res, "Gallery update failed", 500, error); + } +}); + +const deleteAProduct = asyncHandler(async (req, res) => { + try { + const { id } = req.params; + + const product = await Product.findByIdAndDelete(id); + + if (!product) { + ApiResponse.notFound(res, "Product not found"); + } + + ApiResponse.success(res, "Product deleted successfully", {}, 201); + } catch (error) { + if ( + error.name === MONGOOSE_CAST_ERROR && + error.kind === MONGOOSE_OBJECT_ID + ) { + ApiResponse.validationError( + res, + "Product deletion failed", + { id: "Invalid productId provided" }, + 400 + ); + } + ApiResponse.error(res, "Product deletion failed", 500, error); + } +}); + module.exports = { addAProduct, getAllProducts, getAProduct, updateAProduct, + updateProductThumbnail, + updateProductGallery, + deleteAProduct, + // updateManyProducts + // deleteManyProducts + // getProductByCategory + // getProductByTag + // getProductByAttribute + // getProductByslug + // getProductByStatus + // getProductByStock + // getProductInDateRange + // getProductInPriceRange }; diff --git a/routes/product.routes.js b/routes/product.routes.js index 91195fd..387c6e2 100644 --- a/routes/product.routes.js +++ b/routes/product.routes.js @@ -4,6 +4,7 @@ const { getAllProducts, getAProduct, updateAProduct, + deleteAProduct, } = require("../controllers/product.controller"); const upload = require("../middleware/multer.middleware"); const verifyJWT = require("../middleware/verifyJWT.middleware"); @@ -31,4 +32,8 @@ router updateAProduct ); +router + .route("/delete-a-product/:id") + .delete(verifyJWT, verifyRoles(ROLES_LIST.ADMIN), deleteAProduct); + module.exports = router;