desing is done
This commit is contained in:
parent
a7a17e471c
commit
39a565cb0e
4
app.js
4
app.js
@ -37,6 +37,8 @@ import StateRouter from "./resources/setting/state/state_routes.js";
|
|||||||
import LanguageRoute from "./resources/setting/Language/language_routes.js";
|
import LanguageRoute from "./resources/setting/Language/language_routes.js";
|
||||||
//purpose
|
//purpose
|
||||||
import PurposeRoute from "./resources/setting/Purpose/Purpose_routes.js";
|
import PurposeRoute from "./resources/setting/Purpose/Purpose_routes.js";
|
||||||
|
// Design Router
|
||||||
|
import designRoute from "./resources/Design/designRouter.js";
|
||||||
// category Route
|
// category Route
|
||||||
import categoryRoute from "./resources/Category/categoryRoutes.js";
|
import categoryRoute from "./resources/Category/categoryRoutes.js";
|
||||||
import ContentRoute from "./resources/Content/ContentRoutes.js";
|
import ContentRoute from "./resources/Content/ContentRoutes.js";
|
||||||
@ -59,6 +61,8 @@ app.use("/api/v1/", user);
|
|||||||
app.use("/api", ProductRouter);
|
app.use("/api", ProductRouter);
|
||||||
//businesses
|
//businesses
|
||||||
// app.use("/api/businesses", BusinessRoute);
|
// app.use("/api/businesses", BusinessRoute);
|
||||||
|
// Design
|
||||||
|
app.use("/api/design", designRoute);
|
||||||
// Category
|
// Category
|
||||||
app.use("/api/category", categoryRoute);
|
app.use("/api/category", categoryRoute);
|
||||||
// Content
|
// Content
|
||||||
|
151
resources/Design/designController.js
Normal file
151
resources/Design/designController.js
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
|
import cloudinary from "../../Utils/cloudinary.js";
|
||||||
|
import { DesignModel } from "./designModel.js";
|
||||||
|
|
||||||
|
// Add new Category
|
||||||
|
export const addDesign = async (req, res) => {
|
||||||
|
const { designName } = req.body;
|
||||||
|
const { designImage } = req.files;
|
||||||
|
// 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(
|
||||||
|
designImage.tempFilePath,
|
||||||
|
{
|
||||||
|
folder: "jatinMor/design",
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
const design = await DesignModel.create({
|
||||||
|
designName,
|
||||||
|
designImage: result,
|
||||||
|
addedBy: req.user._id,
|
||||||
|
});
|
||||||
|
if (design) {
|
||||||
|
return res
|
||||||
|
.status(201)
|
||||||
|
.json({ success: true, design, message: "design Added" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: error.message ? error.message : "Something went Wrong",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getDesign = async (req, res) => {
|
||||||
|
try {
|
||||||
|
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||||
|
const designs = await DesignModel.find({ addedBy: req.user._id }).sort({
|
||||||
|
createdAt: -1,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!designs) {
|
||||||
|
return res.status(404).json({ message: "No design found" });
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).json({ success: true, designs });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: error.message ? error.message : "Something went wrong",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const updateDesign = async (req, res) => {
|
||||||
|
try {
|
||||||
|
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
||||||
|
const { _id } = req.params;
|
||||||
|
const { designName } = req.body;
|
||||||
|
const olderImage = req.body?.olderImage;
|
||||||
|
const designImage = req.files?.designImage;
|
||||||
|
|
||||||
|
if (!mongoose.Types.ObjectId.isValid(_id)) {
|
||||||
|
return res.status(404).json({ error: "Can not find the document " });
|
||||||
|
}
|
||||||
|
|
||||||
|
// find the document with the id to delete the image from cloudinary
|
||||||
|
if (JSON.parse(olderImage).length == 0) {
|
||||||
|
const deletefromCloudinary = await DesignModel.findOne({ _id: _id });
|
||||||
|
|
||||||
|
const deleteresponse = await cloudinary.v2.uploader.destroy(
|
||||||
|
deletefromCloudinary.designImage.public_id
|
||||||
|
);
|
||||||
|
if (deleteresponse) {
|
||||||
|
const result = await cloudinary.v2.uploader.upload(
|
||||||
|
designImage.tempFilePath,
|
||||||
|
{
|
||||||
|
folder: "jatinMor/design",
|
||||||
|
}
|
||||||
|
);
|
||||||
|
const update = await DesignModel.findOneAndUpdate(
|
||||||
|
{ _id: _id },
|
||||||
|
{ designName: designName, designImage: 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 DesignModel.findOneAndUpdate(
|
||||||
|
{ _id: _id },
|
||||||
|
{ designName: designName, designImage: 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 deleteDesign = 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 DesignModel.findOne({ _id: _id });
|
||||||
|
|
||||||
|
const deleteresponse = await cloudinary.v2.uploader.destroy(
|
||||||
|
deletefromCloudinary.designImage.public_id
|
||||||
|
);
|
||||||
|
if (deleteresponse) {
|
||||||
|
const deleteDesign = await DesignModel.findOneAndDelete({ _id: _id });
|
||||||
|
if (!deleteDesign) {
|
||||||
|
return res.status(404).json({
|
||||||
|
error: "Can not find the document with the provided id to delete ",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
res.status(200).json({ success: true, deleteDesign });
|
||||||
|
} else {
|
||||||
|
return res.status(404).json({ error: "can not delete the design " });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: error.message ? error.message : "Something went wrong",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
19
resources/Design/designModel.js
Normal file
19
resources/Design/designModel.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
|
const DesignSchema = new mongoose.Schema(
|
||||||
|
{
|
||||||
|
designName: {
|
||||||
|
type: String,
|
||||||
|
required: [true, "Name of desing required "],
|
||||||
|
},
|
||||||
|
designImage: {},
|
||||||
|
addedBy: {
|
||||||
|
type: mongoose.Schema.ObjectId,
|
||||||
|
ref: "User",
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ timestamps: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
export const DesignModel = mongoose.model("DesignModel", DesignSchema);
|
25
resources/Design/designRouter.js
Normal file
25
resources/Design/designRouter.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import express from "express";
|
||||||
|
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
||||||
|
|
||||||
|
import {
|
||||||
|
addDesign,
|
||||||
|
deleteDesign,
|
||||||
|
getDesign,
|
||||||
|
updateDesign,
|
||||||
|
} from "./designController.js";
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router
|
||||||
|
.route("/add")
|
||||||
|
.post(isAuthenticatedUser, authorizeRoles("admin"), addDesign);
|
||||||
|
router
|
||||||
|
.route("/getDesigns")
|
||||||
|
.get(isAuthenticatedUser, authorizeRoles("admin"), getDesign);
|
||||||
|
router
|
||||||
|
.route("/update/:_id")
|
||||||
|
.patch(isAuthenticatedUser, authorizeRoles("admin"), updateDesign);
|
||||||
|
router
|
||||||
|
.route("/delete/:_id")
|
||||||
|
.delete(isAuthenticatedUser, authorizeRoles("admin"), deleteDesign);
|
||||||
|
|
||||||
|
export default router;
|
Loading…
Reference in New Issue
Block a user