login, register and shop page image feature implemented
This commit is contained in:
parent
7d87801120
commit
32b6fd709a
7
app.js
7
app.js
@ -53,6 +53,9 @@ import PurposeRoute from "./resources/setting/Purpose/Purpose_routes.js";
|
||||
// category Route
|
||||
import categoryRoute from "./resources/Category/categoryRoutes.js";
|
||||
import bannerRoute from "./resources/Banner/BannerRouter.js";
|
||||
import RegistrationImageRoute from './resources/RegistrationImage/RegistrationImageRoute.js';
|
||||
import loginImageRoute from './resources/LoginImage/LoginImageRoute.js'
|
||||
import shopImageRoute from './resources/ShopPageImage/ShopPageImageRoute.js'
|
||||
import ContentRoute from "./resources/Content/ContentRoutes.js";
|
||||
import UserAddressRoute from "./resources/userAddress/useAddressRoute.js";
|
||||
//business_Type
|
||||
@ -79,6 +82,10 @@ app.use("/api", ProductRouter);
|
||||
// Category
|
||||
app.use("/api/category", categoryRoute);
|
||||
app.use("/api/banner", bannerRoute);
|
||||
// registration image
|
||||
app.use('/api/registerImage', RegistrationImageRoute)
|
||||
app.use('/api/loginImage', loginImageRoute)
|
||||
app.use('/api/shopImage', shopImageRoute)
|
||||
// Content
|
||||
app.use("/api/content", ContentRoute);
|
||||
// User Address
|
||||
|
221
resources/LoginImage/LoginImageController.js
Normal file
221
resources/LoginImage/LoginImageController.js
Normal file
@ -0,0 +1,221 @@
|
||||
import mongoose from "mongoose";
|
||||
|
||||
import cloudinary from "../../Utils/cloudinary.js";
|
||||
import { LoginImageModel } from "./LoginImageModel.js";
|
||||
|
||||
// Add new Category
|
||||
export const addImage = async (req, res) => {
|
||||
// const { bannerName } = req.body;
|
||||
const { bannerImage } = req.files;
|
||||
// console.log("image", bannerImage);
|
||||
// console.log(categoryName, categoryImage);
|
||||
|
||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
try {
|
||||
if (!mongoose.Types.ObjectId.isValid(req.user._id)) {
|
||||
return res.status(400).json({ message: "please login again " });
|
||||
}
|
||||
const result = await cloudinary.v2.uploader.upload(
|
||||
bannerImage.tempFilePath,
|
||||
{
|
||||
folder: "jatinMor/loginImage",
|
||||
}
|
||||
);
|
||||
|
||||
if (result) {
|
||||
const regsiterImage = await LoginImageModel.create({
|
||||
image: result,
|
||||
addedBy: req.user._id,
|
||||
});
|
||||
if (regsiterImage) {
|
||||
return res
|
||||
.status(201)
|
||||
.json({ success: true, regsiterImage, message: "Image Added" });
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message ? error.message : "Something went Wrong",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getImage = async (req, res) => {
|
||||
try {
|
||||
// if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
const image = await LoginImageModel.find().sort({
|
||||
createdAt: -1,
|
||||
});
|
||||
|
||||
if (!image) {
|
||||
return res.status(404).json({ message: "No categories found" });
|
||||
}
|
||||
// console.log("image", image);
|
||||
res.status(200).json({ success: true, image });
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message ? error.message : "Something went wrong",
|
||||
});
|
||||
}
|
||||
};
|
||||
// export const updateImage = async (req, res) => {
|
||||
// try {
|
||||
// if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
// const { _id } = req.params;
|
||||
// const { bannerName } = req.body;
|
||||
// const olderImage = req.body?.olderImage;
|
||||
// const bannerImag = req.files?.bannerImage;
|
||||
|
||||
// if (!mongoose.Types.ObjectId.isValid(_id)) {
|
||||
// return res.status(404).json({ error: "Can not find the document " });
|
||||
// }
|
||||
|
||||
// if (olderImage) {
|
||||
// // If there's an older image, delete it from Cloudinary
|
||||
// const deletefromCloudinary = await LoginImageModel.findOne({ _id: _id });
|
||||
// const deleteresponse = await cloudinary.v2.uploader.destroy(
|
||||
// deletefromCloudinary.image.public_id
|
||||
// );
|
||||
|
||||
// if (deleteresponse) {
|
||||
// // Upload the new image to Cloudinary
|
||||
// const result = await cloudinary.v2.uploader.upload(
|
||||
// bannerImag.tempFilePath,
|
||||
// {
|
||||
// folder: "jatinMor/loginImage",
|
||||
// }
|
||||
// );
|
||||
|
||||
// // Update the document with the new image
|
||||
// const update = await LoginImageModel.findOneAndUpdate(
|
||||
// { _id: _id },
|
||||
// { bannerImage: result }, // Provide the updated bannerImage
|
||||
// { new: true } // To return the updated document
|
||||
// );
|
||||
|
||||
// if (!update) {
|
||||
// return res
|
||||
// .status(404)
|
||||
// .json({ message: "Can not update document, something went wrong" });
|
||||
// } else {
|
||||
// return res.status(200).json({ success: true, update });
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// // If there's no older image, update the document with the existing bannerImage
|
||||
// const update = await LoginImageModel.findOneAndUpdate(
|
||||
// { _id: _id },
|
||||
// { bannerImage: JSON.parse(bannerImag) }, // Provide the updated bannerImage
|
||||
// { new: true } // To return the updated document
|
||||
// );
|
||||
|
||||
// if (update) {
|
||||
// return res.status(200).json({ success: true, update });
|
||||
// }
|
||||
// }
|
||||
// } catch (error) {
|
||||
// res.status(500).json({
|
||||
// success: false,
|
||||
// message: error.message ? error.message : "Something went wrong",
|
||||
// });
|
||||
// }
|
||||
// };
|
||||
|
||||
export const updateImage = async (req, res) => {
|
||||
try {
|
||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
const { _id } = req.params;
|
||||
const { bannerName } = req.body;
|
||||
const olderImage = req.body?.olderImage;
|
||||
const bannerImag = req.files?.bannerImage;
|
||||
// console.log("bannerImag", bannerImag);
|
||||
|
||||
if (!mongoose.Types.ObjectId.isValid(_id)) {
|
||||
return res.status(404).json({ error: "Can not find the document " });
|
||||
}
|
||||
// console.log(JSON.parse(olderImage).length);
|
||||
// find the document with the id to delete the image from cloudinary
|
||||
|
||||
if (olderImage) {
|
||||
|
||||
const deletefromCloudinary = await LoginImageModel.findOne({ _id: _id });
|
||||
|
||||
// console.log("deletefromCloudinary", deletefromCloudinary)
|
||||
const deleteresponse = await cloudinary.v2.uploader.destroy(
|
||||
deletefromCloudinary.image.public_id
|
||||
);
|
||||
|
||||
|
||||
if (deleteresponse) {
|
||||
|
||||
const result = await cloudinary.v2.uploader.upload(
|
||||
bannerImag.tempFilePath,
|
||||
{
|
||||
folder: "jatinMor/loginImage",
|
||||
}
|
||||
);
|
||||
console.log("result", result);
|
||||
const update = await LoginImageModel.findOneAndUpdate(
|
||||
{ _id: _id },
|
||||
{ image: result }, // Provide the updated categoryName
|
||||
{ new: true } // To return the updated document
|
||||
);
|
||||
if (!update) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ message: "Can not update document, something went wrong" });
|
||||
} else {
|
||||
return res.status(200).json({ success: true, update });
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const update = await LoginImageModel.findOneAndUpdate(
|
||||
{ _id: _id },
|
||||
{ image: JSON.parse(olderImage) }, // Provide the updated categoryName
|
||||
{ new: true } // To return the updated document
|
||||
);
|
||||
if (update) {
|
||||
return res.status(200).json({ success: true, update });
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message ? error.message : "Something went wrong",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteImage = async (req, res) => {
|
||||
try {
|
||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
const { _id } = req.params;
|
||||
if (!mongoose.Types.ObjectId.isValid(_id)) {
|
||||
return res.status(404).json({ error: "Can not find the document " });
|
||||
}
|
||||
|
||||
const deletefromCloudinary = await LoginImageModel.findOne({ _id: _id });
|
||||
// console.log(deletefromCloudinary);
|
||||
const deleteresponse = await cloudinary.v2.uploader.destroy(
|
||||
deletefromCloudinary.image.public_id
|
||||
);
|
||||
if (deleteresponse) {
|
||||
const deleteBanner = await LoginImageModel.findOneAndDelete({ _id: _id });
|
||||
if (!deleteBanner) {
|
||||
return res.status(404).json({
|
||||
error: "Can not find the document with the provided id to delete ",
|
||||
});
|
||||
}
|
||||
res.status(200).json({ success: true, deleteBanner });
|
||||
} else {
|
||||
return res.status(404).json({ error: "can not delete the banner " });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message ? error.message : "Something went wrong",
|
||||
});
|
||||
}
|
||||
};
|
19
resources/LoginImage/LoginImageModel.js
Normal file
19
resources/LoginImage/LoginImageModel.js
Normal file
@ -0,0 +1,19 @@
|
||||
import mongoose from "mongoose";
|
||||
|
||||
const LoginImageSchema = new mongoose.Schema(
|
||||
{
|
||||
|
||||
image: {
|
||||
type: Object,
|
||||
required: true
|
||||
},
|
||||
addedBy: {
|
||||
type: mongoose.Schema.ObjectId,
|
||||
ref: "User",
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const LoginImageModel = mongoose.model("LoginImageModel", LoginImageSchema);
|
19
resources/LoginImage/LoginImageRoute.js
Normal file
19
resources/LoginImage/LoginImageRoute.js
Normal file
@ -0,0 +1,19 @@
|
||||
import express from "express";
|
||||
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
||||
|
||||
// import { addImage, deleteImage, getImage, updateImage } from "./RegistrationImageController.js";
|
||||
import { addImage, deleteImage, getImage, updateImage } from "./LoginImageController.js";
|
||||
const router = express.Router();
|
||||
|
||||
router
|
||||
.route("/add")
|
||||
.post(isAuthenticatedUser, authorizeRoles("admin"), addImage);
|
||||
router.route("/getImage").get(getImage);
|
||||
router
|
||||
.route("/update/:_id")
|
||||
.patch(isAuthenticatedUser, authorizeRoles("admin"), updateImage);
|
||||
router
|
||||
.route("/delete/:_id")
|
||||
.delete(isAuthenticatedUser, authorizeRoles("admin"), deleteImage);
|
||||
|
||||
export default router;
|
221
resources/RegistrationImage/RegistrationImageController.js
Normal file
221
resources/RegistrationImage/RegistrationImageController.js
Normal file
@ -0,0 +1,221 @@
|
||||
import mongoose from "mongoose";
|
||||
|
||||
import cloudinary from "../../Utils/cloudinary.js";
|
||||
import { RegistrationImageModel } from "./RegistrationImageModel.js";
|
||||
|
||||
// Add new Category
|
||||
export const addImage = async (req, res) => {
|
||||
// const { bannerName } = req.body;
|
||||
const { bannerImage } = req.files;
|
||||
// console.log("image", bannerImage);
|
||||
// console.log(categoryName, categoryImage);
|
||||
|
||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
try {
|
||||
if (!mongoose.Types.ObjectId.isValid(req.user._id)) {
|
||||
return res.status(400).json({ message: "please login again " });
|
||||
}
|
||||
const result = await cloudinary.v2.uploader.upload(
|
||||
bannerImage.tempFilePath,
|
||||
{
|
||||
folder: "jatinMor/registrationImage",
|
||||
}
|
||||
);
|
||||
|
||||
if (result) {
|
||||
const regsiterImage = await RegistrationImageModel.create({
|
||||
image: result,
|
||||
addedBy: req.user._id,
|
||||
});
|
||||
if (regsiterImage) {
|
||||
return res
|
||||
.status(201)
|
||||
.json({ success: true, regsiterImage, message: "Image Added" });
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message ? error.message : "Something went Wrong",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getImage = async (req, res) => {
|
||||
try {
|
||||
// if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
const image = await RegistrationImageModel.find().sort({
|
||||
createdAt: -1,
|
||||
});
|
||||
|
||||
if (!image) {
|
||||
return res.status(404).json({ message: "No categories found" });
|
||||
}
|
||||
// console.log("image", image);
|
||||
res.status(200).json({ success: true, image });
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message ? error.message : "Something went wrong",
|
||||
});
|
||||
}
|
||||
};
|
||||
// export const updateImage = async (req, res) => {
|
||||
// try {
|
||||
// if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
// const { _id } = req.params;
|
||||
// const { bannerName } = req.body;
|
||||
// const olderImage = req.body?.olderImage;
|
||||
// const bannerImag = req.files?.bannerImage;
|
||||
|
||||
// if (!mongoose.Types.ObjectId.isValid(_id)) {
|
||||
// return res.status(404).json({ error: "Can not find the document " });
|
||||
// }
|
||||
|
||||
// if (olderImage) {
|
||||
// // If there's an older image, delete it from Cloudinary
|
||||
// const deletefromCloudinary = await RegistrationImageModel.findOne({ _id: _id });
|
||||
// const deleteresponse = await cloudinary.v2.uploader.destroy(
|
||||
// deletefromCloudinary.image.public_id
|
||||
// );
|
||||
|
||||
// if (deleteresponse) {
|
||||
// // Upload the new image to Cloudinary
|
||||
// const result = await cloudinary.v2.uploader.upload(
|
||||
// bannerImag.tempFilePath,
|
||||
// {
|
||||
// folder: "jatinMor/registrationImage",
|
||||
// }
|
||||
// );
|
||||
|
||||
// // Update the document with the new image
|
||||
// const update = await RegistrationImageModel.findOneAndUpdate(
|
||||
// { _id: _id },
|
||||
// { bannerImage: result }, // Provide the updated bannerImage
|
||||
// { new: true } // To return the updated document
|
||||
// );
|
||||
|
||||
// if (!update) {
|
||||
// return res
|
||||
// .status(404)
|
||||
// .json({ message: "Can not update document, something went wrong" });
|
||||
// } else {
|
||||
// return res.status(200).json({ success: true, update });
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// // If there's no older image, update the document with the existing bannerImage
|
||||
// const update = await RegistrationImageModel.findOneAndUpdate(
|
||||
// { _id: _id },
|
||||
// { bannerImage: JSON.parse(bannerImag) }, // Provide the updated bannerImage
|
||||
// { new: true } // To return the updated document
|
||||
// );
|
||||
|
||||
// if (update) {
|
||||
// return res.status(200).json({ success: true, update });
|
||||
// }
|
||||
// }
|
||||
// } catch (error) {
|
||||
// res.status(500).json({
|
||||
// success: false,
|
||||
// message: error.message ? error.message : "Something went wrong",
|
||||
// });
|
||||
// }
|
||||
// };
|
||||
|
||||
export const updateImage = async (req, res) => {
|
||||
try {
|
||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
const { _id } = req.params;
|
||||
const { bannerName } = req.body;
|
||||
const olderImage = req.body?.olderImage;
|
||||
const bannerImag = req.files?.bannerImage;
|
||||
// console.log("bannerImag", bannerImag);
|
||||
|
||||
if (!mongoose.Types.ObjectId.isValid(_id)) {
|
||||
return res.status(404).json({ error: "Can not find the document " });
|
||||
}
|
||||
// console.log(JSON.parse(olderImage).length);
|
||||
// find the document with the id to delete the image from cloudinary
|
||||
|
||||
if (olderImage) {
|
||||
|
||||
const deletefromCloudinary = await RegistrationImageModel.findOne({ _id: _id });
|
||||
|
||||
// console.log("deletefromCloudinary", deletefromCloudinary)
|
||||
const deleteresponse = await cloudinary.v2.uploader.destroy(
|
||||
deletefromCloudinary.image.public_id
|
||||
);
|
||||
|
||||
|
||||
if (deleteresponse) {
|
||||
|
||||
const result = await cloudinary.v2.uploader.upload(
|
||||
bannerImag.tempFilePath,
|
||||
{
|
||||
folder: "jatinMor/registrationImage",
|
||||
}
|
||||
);
|
||||
console.log("result", result);
|
||||
const update = await RegistrationImageModel.findOneAndUpdate(
|
||||
{ _id: _id },
|
||||
{ image: result }, // Provide the updated categoryName
|
||||
{ new: true } // To return the updated document
|
||||
);
|
||||
if (!update) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ message: "Can not update document, something went wrong" });
|
||||
} else {
|
||||
return res.status(200).json({ success: true, update });
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const update = await RegistrationImageModel.findOneAndUpdate(
|
||||
{ _id: _id },
|
||||
{ image: JSON.parse(olderImage) }, // Provide the updated categoryName
|
||||
{ new: true } // To return the updated document
|
||||
);
|
||||
if (update) {
|
||||
return res.status(200).json({ success: true, update });
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message ? error.message : "Something went wrong",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteImage = async (req, res) => {
|
||||
try {
|
||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
const { _id } = req.params;
|
||||
if (!mongoose.Types.ObjectId.isValid(_id)) {
|
||||
return res.status(404).json({ error: "Can not find the document " });
|
||||
}
|
||||
|
||||
const deletefromCloudinary = await RegistrationImageModel.findOne({ _id: _id });
|
||||
// console.log(deletefromCloudinary);
|
||||
const deleteresponse = await cloudinary.v2.uploader.destroy(
|
||||
deletefromCloudinary.image.public_id
|
||||
);
|
||||
if (deleteresponse) {
|
||||
const deleteBanner = await RegistrationImageModel.findOneAndDelete({ _id: _id });
|
||||
if (!deleteBanner) {
|
||||
return res.status(404).json({
|
||||
error: "Can not find the document with the provided id to delete ",
|
||||
});
|
||||
}
|
||||
res.status(200).json({ success: true, deleteBanner });
|
||||
} else {
|
||||
return res.status(404).json({ error: "can not delete the banner " });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message ? error.message : "Something went wrong",
|
||||
});
|
||||
}
|
||||
};
|
19
resources/RegistrationImage/RegistrationImageModel.js
Normal file
19
resources/RegistrationImage/RegistrationImageModel.js
Normal file
@ -0,0 +1,19 @@
|
||||
import mongoose from "mongoose";
|
||||
|
||||
const RegistrationImageSchema = new mongoose.Schema(
|
||||
{
|
||||
|
||||
image: {
|
||||
type: Object,
|
||||
required: true
|
||||
},
|
||||
addedBy: {
|
||||
type: mongoose.Schema.ObjectId,
|
||||
ref: "User",
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const RegistrationImageModel = mongoose.model("RegistrationImageModel", RegistrationImageSchema);
|
18
resources/RegistrationImage/RegistrationImageRoute.js
Normal file
18
resources/RegistrationImage/RegistrationImageRoute.js
Normal file
@ -0,0 +1,18 @@
|
||||
import express from "express";
|
||||
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
||||
|
||||
import { addImage, deleteImage, getImage, updateImage } from "./RegistrationImageController.js";
|
||||
const router = express.Router();
|
||||
|
||||
router
|
||||
.route("/add")
|
||||
.post(isAuthenticatedUser, authorizeRoles("admin"), addImage);
|
||||
router.route("/getImage").get(getImage);
|
||||
router
|
||||
.route("/update/:_id")
|
||||
.patch(isAuthenticatedUser, authorizeRoles("admin"), updateImage);
|
||||
router
|
||||
.route("/delete/:_id")
|
||||
.delete(isAuthenticatedUser, authorizeRoles("admin"), deleteImage);
|
||||
|
||||
export default router;
|
221
resources/ShopPageImage/ShopPageImageController.js
Normal file
221
resources/ShopPageImage/ShopPageImageController.js
Normal file
@ -0,0 +1,221 @@
|
||||
import mongoose from "mongoose";
|
||||
|
||||
import cloudinary from "../../Utils/cloudinary.js";
|
||||
import { ShopPageImageModel } from "./ShopPageImageModel.js";
|
||||
|
||||
// Add new Category
|
||||
export const addImage = async (req, res) => {
|
||||
// const { bannerName } = req.body;
|
||||
const { bannerImage } = req.files;
|
||||
// console.log("image", bannerImage);
|
||||
// console.log(categoryName, categoryImage);
|
||||
|
||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
try {
|
||||
if (!mongoose.Types.ObjectId.isValid(req.user._id)) {
|
||||
return res.status(400).json({ message: "please login again " });
|
||||
}
|
||||
const result = await cloudinary.v2.uploader.upload(
|
||||
bannerImage.tempFilePath,
|
||||
{
|
||||
folder: "jatinMor/shopImage",
|
||||
}
|
||||
);
|
||||
|
||||
if (result) {
|
||||
const regsiterImage = await ShopPageImageModel.create({
|
||||
image: result,
|
||||
addedBy: req.user._id,
|
||||
});
|
||||
if (regsiterImage) {
|
||||
return res
|
||||
.status(201)
|
||||
.json({ success: true, regsiterImage, message: "Image Added" });
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message ? error.message : "Something went Wrong",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getImage = async (req, res) => {
|
||||
try {
|
||||
// if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
const image = await ShopPageImageModel.find().sort({
|
||||
createdAt: -1,
|
||||
});
|
||||
|
||||
if (!image) {
|
||||
return res.status(404).json({ message: "No categories found" });
|
||||
}
|
||||
// console.log("image", image);
|
||||
res.status(200).json({ success: true, image });
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message ? error.message : "Something went wrong",
|
||||
});
|
||||
}
|
||||
};
|
||||
// export const updateImage = async (req, res) => {
|
||||
// try {
|
||||
// if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
// const { _id } = req.params;
|
||||
// const { bannerName } = req.body;
|
||||
// const olderImage = req.body?.olderImage;
|
||||
// const bannerImag = req.files?.bannerImage;
|
||||
|
||||
// if (!mongoose.Types.ObjectId.isValid(_id)) {
|
||||
// return res.status(404).json({ error: "Can not find the document " });
|
||||
// }
|
||||
|
||||
// if (olderImage) {
|
||||
// // If there's an older image, delete it from Cloudinary
|
||||
// const deletefromCloudinary = await ShopPageImageModel.findOne({ _id: _id });
|
||||
// const deleteresponse = await cloudinary.v2.uploader.destroy(
|
||||
// deletefromCloudinary.image.public_id
|
||||
// );
|
||||
|
||||
// if (deleteresponse) {
|
||||
// // Upload the new image to Cloudinary
|
||||
// const result = await cloudinary.v2.uploader.upload(
|
||||
// bannerImag.tempFilePath,
|
||||
// {
|
||||
// folder: "jatinMor/shopImage",
|
||||
// }
|
||||
// );
|
||||
|
||||
// // Update the document with the new image
|
||||
// const update = await ShopPageImageModel.findOneAndUpdate(
|
||||
// { _id: _id },
|
||||
// { bannerImage: result }, // Provide the updated bannerImage
|
||||
// { new: true } // To return the updated document
|
||||
// );
|
||||
|
||||
// if (!update) {
|
||||
// return res
|
||||
// .status(404)
|
||||
// .json({ message: "Can not update document, something went wrong" });
|
||||
// } else {
|
||||
// return res.status(200).json({ success: true, update });
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// // If there's no older image, update the document with the existing bannerImage
|
||||
// const update = await ShopPageImageModel.findOneAndUpdate(
|
||||
// { _id: _id },
|
||||
// { bannerImage: JSON.parse(bannerImag) }, // Provide the updated bannerImage
|
||||
// { new: true } // To return the updated document
|
||||
// );
|
||||
|
||||
// if (update) {
|
||||
// return res.status(200).json({ success: true, update });
|
||||
// }
|
||||
// }
|
||||
// } catch (error) {
|
||||
// res.status(500).json({
|
||||
// success: false,
|
||||
// message: error.message ? error.message : "Something went wrong",
|
||||
// });
|
||||
// }
|
||||
// };
|
||||
|
||||
export const updateImage = async (req, res) => {
|
||||
try {
|
||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
const { _id } = req.params;
|
||||
const { bannerName } = req.body;
|
||||
const olderImage = req.body?.olderImage;
|
||||
const bannerImag = req.files?.bannerImage;
|
||||
// console.log("bannerImag", bannerImag);
|
||||
|
||||
if (!mongoose.Types.ObjectId.isValid(_id)) {
|
||||
return res.status(404).json({ error: "Can not find the document " });
|
||||
}
|
||||
// console.log(JSON.parse(olderImage).length);
|
||||
// find the document with the id to delete the image from cloudinary
|
||||
|
||||
if (olderImage) {
|
||||
|
||||
const deletefromCloudinary = await ShopPageImageModel.findOne({ _id: _id });
|
||||
|
||||
// console.log("deletefromCloudinary", deletefromCloudinary)
|
||||
const deleteresponse = await cloudinary.v2.uploader.destroy(
|
||||
deletefromCloudinary.image.public_id
|
||||
);
|
||||
|
||||
|
||||
if (deleteresponse) {
|
||||
|
||||
const result = await cloudinary.v2.uploader.upload(
|
||||
bannerImag.tempFilePath,
|
||||
{
|
||||
folder: "jatinMor/shopImage",
|
||||
}
|
||||
);
|
||||
console.log("result", result);
|
||||
const update = await ShopPageImageModel.findOneAndUpdate(
|
||||
{ _id: _id },
|
||||
{ image: result }, // Provide the updated categoryName
|
||||
{ new: true } // To return the updated document
|
||||
);
|
||||
if (!update) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ message: "Can not update document, something went wrong" });
|
||||
} else {
|
||||
return res.status(200).json({ success: true, update });
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const update = await ShopPageImageModel.findOneAndUpdate(
|
||||
{ _id: _id },
|
||||
{ image: JSON.parse(olderImage) }, // Provide the updated categoryName
|
||||
{ new: true } // To return the updated document
|
||||
);
|
||||
if (update) {
|
||||
return res.status(200).json({ success: true, update });
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message ? error.message : "Something went wrong",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteImage = async (req, res) => {
|
||||
try {
|
||||
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||
const { _id } = req.params;
|
||||
if (!mongoose.Types.ObjectId.isValid(_id)) {
|
||||
return res.status(404).json({ error: "Can not find the document " });
|
||||
}
|
||||
|
||||
const deletefromCloudinary = await ShopPageImageModel.findOne({ _id: _id });
|
||||
// console.log(deletefromCloudinary);
|
||||
const deleteresponse = await cloudinary.v2.uploader.destroy(
|
||||
deletefromCloudinary.image.public_id
|
||||
);
|
||||
if (deleteresponse) {
|
||||
const deleteBanner = await ShopPageImageModel.findOneAndDelete({ _id: _id });
|
||||
if (!deleteBanner) {
|
||||
return res.status(404).json({
|
||||
error: "Can not find the document with the provided id to delete ",
|
||||
});
|
||||
}
|
||||
res.status(200).json({ success: true, deleteBanner });
|
||||
} else {
|
||||
return res.status(404).json({ error: "can not delete the banner " });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message ? error.message : "Something went wrong",
|
||||
});
|
||||
}
|
||||
};
|
19
resources/ShopPageImage/ShopPageImageModel.js
Normal file
19
resources/ShopPageImage/ShopPageImageModel.js
Normal file
@ -0,0 +1,19 @@
|
||||
import mongoose from "mongoose";
|
||||
|
||||
const ShopImageSchema = new mongoose.Schema(
|
||||
{
|
||||
|
||||
image: {
|
||||
type: Object,
|
||||
required: true
|
||||
},
|
||||
addedBy: {
|
||||
type: mongoose.Schema.ObjectId,
|
||||
ref: "User",
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const ShopPageImageModel = mongoose.model("ShopPageImageModel", ShopImageSchema);
|
20
resources/ShopPageImage/ShopPageImageRoute.js
Normal file
20
resources/ShopPageImage/ShopPageImageRoute.js
Normal file
@ -0,0 +1,20 @@
|
||||
import express from "express";
|
||||
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
||||
import { addImage, deleteImage, getImage, updateImage } from "./ShopPageImageController.js";
|
||||
|
||||
// import { addImage, deleteImage, getImage, updateImage } from "./RegistrationImageController.js";
|
||||
// import { addImage, deleteImage, getImage, updateImage } from "./LoginImageController.js";
|
||||
const router = express.Router();
|
||||
|
||||
router
|
||||
.route("/add")
|
||||
.post(isAuthenticatedUser, authorizeRoles("admin"), addImage);
|
||||
router.route("/getImage").get(getImage);
|
||||
router
|
||||
.route("/update/:_id")
|
||||
.patch(isAuthenticatedUser, authorizeRoles("admin"), updateImage);
|
||||
router
|
||||
.route("/delete/:_id")
|
||||
.delete(isAuthenticatedUser, authorizeRoles("admin"), deleteImage);
|
||||
|
||||
export default router;
|
Loading…
Reference in New Issue
Block a user