design temlate done

This commit is contained in:
print-signs 2023-11-16 10:50:25 +05:30
parent dee0a3a701
commit 3213493516
7 changed files with 254 additions and 50 deletions

19
app.js
View File

@ -1,11 +1,14 @@
import dotenv from "dotenv";
import express from "express";
const app = express();
import path, { dirname, join } from "path";
import { fileURLToPath } from "url";
import bodyParser from "body-parser";
import fileUpload from "express-fileupload"; // important pkg for file upload
import cors from "cors";
import cookieParser from "cookie-parser";
// Design Router
import designRoute from "./resources/Design/designRouter.js";
// app.use(express.json({ limit: "50mb" }));
// app.use(express.urlencoded({ extended: true, limit: "50mb" }));
app.use(cookieParser());
@ -14,6 +17,15 @@ app.use(cookieParser());
app.use(cors());
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use("/api/design", designRoute);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Define the path to the public folder where static files are located
const publicPath = join(__dirname, "public");
// Serve static files from the 'public' directory
app.use(express.static(publicPath));
app.use(
fileUpload({
useTempFiles: true,
@ -37,8 +49,7 @@ import StateRouter from "./resources/setting/state/state_routes.js";
import LanguageRoute from "./resources/setting/Language/language_routes.js";
//purpose
import PurposeRoute from "./resources/setting/Purpose/Purpose_routes.js";
// Design Router
import designRoute from "./resources/Design/designRouter.js";
// category Route
import categoryRoute from "./resources/Category/categoryRoutes.js";
import ContentRoute from "./resources/Content/ContentRoutes.js";
@ -62,7 +73,7 @@ app.use("/api", ProductRouter);
//businesses
// app.use("/api/businesses", BusinessRoute);
// Design
app.use("/api/design", designRoute);
// Category
app.use("/api/category", categoryRoute);
// Content

Binary file not shown.

After

Width:  |  Height:  |  Size: 202 KiB

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 865 KiB

File diff suppressed because one or more lines are too long

View File

@ -2,47 +2,128 @@ import mongoose from "mongoose";
import cloudinary from "../../Utils/cloudinary.js";
import { DesignModel } from "./designModel.js";
import multer from "multer";
import path from "path";
import fs from "fs/promises";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import { fileURLToPath } from "url";
const imageStorage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "public/uploades");
},
filename: function (req, file, cb) {
cb(null, file.originalname);
},
});
const imageUpload = multer({ storage: imageStorage }).any();
// const jsonUpload = multer({ storage: jsonStorage }).fields([
// { name: "designImageJson", maxCount: 1 },
// ]);
// Add new Category
export const addDesign = async (req, res) => {
const { designName, categoryName } = req.body;
const { designImage } = req.files;
const { designImageJson } = req.body;
// export const addDesign = async (req, res) => {
// const { designName, categoryName } = req.body;
// const { designImage } = req.files;
// const { designImageJson } = req.body;
// console.log(categoryName, categoryImage);
// // 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,
// categoryName,
// designImage: result,
// designImageJson: JSON.parse(designImageJson),
// 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 addDesign = (req, res) => {
// const { designName, categoryName } = req.body;
// const { designImage, designImageJson } = req.files;
// const { designImageJson } = req.body;
// console.log(designImage, designImageJson);
// console.log(categoryName, designImage);
imageUpload(req, res, async (imageErr) => {
// jsonUpload(req, res, async (jsonErr) => {
if (imageErr) {
console.log(imageErr);
return res.status(500).json({
success: false,
message: "Error uploading files",
});
}
const { designName, categoryName } = req.body;
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",
}
);
// Retrieve the uploaded image and JSON file information
const designImage = req.files[0];
const designImageJson = req.files[1];
console.log("designImage", designImage, "json", designImageJson);
if (result) {
// Create a new design in the database
console.log("came here");
const design = await DesignModel.create({
designName,
categoryName,
designImage: result,
designImageJson: JSON.parse(designImageJson),
designImage: {
filename: designImage.filename,
path: designImage.path,
},
designImageJson: {
filename: designImageJson.filename,
path: designImageJson.path,
// Add any other necessary information from the JSON file
},
addedBy: req.user._id,
});
console.log("design", design);
if (design) {
return res
.status(201)
.json({ success: true, design, message: "design Added" });
}
return res.status(201).json({
success: true,
design,
message: "Design added successfully",
});
}
} catch (error) {
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went Wrong",
message: error.message ? error.message : "Something went wrong",
});
}
});
// });
};
export const getDesign = async (req, res) => {
@ -133,31 +214,91 @@ export const updateDesign = async (req, res) => {
}
};
// 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",
// });
// }
// };
// import imagepath from "../../public/uploades";
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 {
designImageFilename,
designImagePath,
designImageJsonFilename,
designImageJsonPath,
} = req.body;
try {
// Construct absolute paths for the files
const imagePath = path.join(__dirname, "../../", designImagePath);
const jsonPath = path.join(
__dirname,
"../../",
designImageJsonPath
// designImageJsonFilename
);
// Check if the files exist before attempting deletion
const imageExists = await fs
.access(imagePath)
.then(() => true)
.catch(() => false);
const jsonExists = await fs
.access(jsonPath)
.then(() => true)
.catch(() => false);
if (imageExists) {
await fs.unlink(imagePath);
} else {
console.error(`Image file not found at path: ${imagePath}`);
}
const deletefromCloudinary = await DesignModel.findOne({ _id: _id });
if (jsonExists) {
await fs.unlink(jsonPath);
} else {
console.error(`JSON file not found at path: ${jsonPath}`);
}
const deleteDesign = await DesignModel.findOneAndDelete({ _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 ",
error: "Cannot 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) {
console.error(error);
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went wrong",

View File

@ -1,3 +1,26 @@
// 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(getDesign);
// router
// .route("/update/:_id")
// .patch(isAuthenticatedUser, authorizeRoles("admin"), updateDesign);
// router
// .route("/delete/:_id")
// .delete(isAuthenticatedUser, authorizeRoles("admin"), deleteDesign);
// export default router;
import express from "express";
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
@ -7,8 +30,35 @@ import {
getDesign,
updateDesign,
} from "./designController.js";
import multer from "multer";
import path from "path";
const router = express.Router();
const imageStorage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "public/uploades");
},
filename: function (req, file, cb) {
cb(null, file.originalname);
},
});
// const jsonStorage = multer.diskStorage({
// destination: function (req, file, cb) {
// cb(null, "public/uploades");
// },
// filename: function (req, file, cb) {
// cb(
// null,
// file.fieldname + "-" + Date.now() + path.extname(file.originalname)
// );
// },
// });
// const imageUpload = multer({ storage: imageStorage }).any();
// const jsonUpload = multer({ storage: jsonStorage }).fields([
// { name: "designImageJson", maxCount: 1 },
// ]);
router
.route("/add")
.post(isAuthenticatedUser, authorizeRoles("admin"), addDesign);