Skip to content

Commit

Permalink
Add delete a pruoduct route
Browse files Browse the repository at this point in the history
  • Loading branch information
bewake24 committed Nov 19, 2024
1 parent b003661 commit 136c178
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
6 changes: 5 additions & 1 deletion controllers/attribute.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
107 changes: 107 additions & 0 deletions controllers/product.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
};
5 changes: 5 additions & 0 deletions routes/product.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -31,4 +32,8 @@ router
updateAProduct
);

router
.route("/delete-a-product/:id")
.delete(verifyJWT, verifyRoles(ROLES_LIST.ADMIN), deleteAProduct);

module.exports = router;

0 comments on commit 136c178

Please sign in to comment.