diff --git a/controllers/NewsController.js b/controllers/NewsController.js index 140a84d..8f40925 100644 --- a/controllers/NewsController.js +++ b/controllers/NewsController.js @@ -8,7 +8,7 @@ export const createNews = async (req, res) => { try { const files = req.files.image; - console.log(files.tempFilePath) + // console.log(files.tempFilePath) const myCloud = await cloudinary.uploader.upload(files.tempFilePath, { folder: "cmp/News", }, diff --git a/controllers/cmp-restriction-Controller.js b/controllers/cmp-restriction-Controller.js index db384f6..12e80db 100644 --- a/controllers/cmp-restriction-Controller.js +++ b/controllers/cmp-restriction-Controller.js @@ -1,11 +1,31 @@ import cmpRestrictionModel from "../models/cmp-restriction-model.js" - +import cloudinary from "cloudinary"; export const createRestriction = async (req, res) => { try { - // console.log(req.body) - const data = await cmpRestrictionModel.create(req.body); + const CMSData = { + title: req.body.title, + page_data: req.body.page_data, + }; + + if (req.files) { + const files = req.files.image; + + const myCloud = await cloudinary.uploader.upload(files.tempFilePath, { + folder: "cmp/image", + }, + function (error, result) { (result, error) }); + CMSData.image = { + public_id: myCloud.public_id, + url: myCloud.url, + } + + } + + const data = await cmpRestrictionModel.create( + CMSData + ); res.status(201).json({ success: true, msg: " create Restriction Successfully!!", @@ -63,36 +83,84 @@ export const getOneRestriction = async (req, res) => { // 3.update export const updateRestriction = async (req, res) => { try { - // console.log(req.body) - const newResData = { - About_Us: req.body.About_Us, - Terms_and_Conditions: req.body.Terms_and_Conditions, - Privacy_Policy: req.body.Privacy_Policy, + if (!req.params.id) { + return res.status(404).json({ + msg: "CMS Id Not Found!" + }); + } + const CMSData = { + title: req.body.title, + page_data: req.body.page_data, }; + if (req.files) { + const getCms = await cmpRestrictionModel.findById(req.params.id); + //delete from cloudinary + if (getCms.image.public_id) { + const imageId = getCms.image.public_id; + await cloudinary.uploader.destroy(imageId) + } + const files = req.files.image; + + const myCloud = await cloudinary.uploader.upload(files.tempFilePath, { + folder: "cmp/image", + }, + function (error, result) { (result, error) }); + CMSData.image = { + public_id: myCloud.public_id, + url: myCloud.url, + } + + } - - //req.user.id, - const ModifyCmpRes = await cmpRestrictionModel.findByIdAndUpdate(req.params.id, newResData, - + const ModifyCms = await cmpRestrictionModel.findByIdAndUpdate(req.params.id, CMSData, { new: true } - // runValidators: true, - // useFindAndModify: false, ); - res.status(200).json({ success: true, - ModifyCmpRes + ModifyCms }); } catch (error) { - console.log(error) + // console.log(error) res.status(500).json({ success: false, - msg: "Failled to UpDate Restriction!!" + msg: "Failled to UpDate !!" }); } }; +//delete +export const deleteCms = async (req, res) => { + + try { + //delete image from cloudinary + const getCms = await cmpRestrictionModel.findById(req.params.id); + + if (getCms.image.public_id) { + const imageId = getCms.image.public_id; + await cloudinary.uploader.destroy(imageId) + } + + //-------------------------// + const Cms = await cmpRestrictionModel.findByIdAndDelete(req.params.id) + if (!Cms) { + return res.status(400).json({ message: 'CMS Not Found' }); + } + await Cms.remove(); + res.status(200).json({ + success: true, + msg: "CMS Deleted Successfully!!", + + }); + } catch (error) { + res.status(500).json({ + success: false, + msg: "Failled to Delete !!" + }); + } + +}; + diff --git a/models/cmp-restriction-model.js b/models/cmp-restriction-model.js index 5455e84..9259d55 100644 --- a/models/cmp-restriction-model.js +++ b/models/cmp-restriction-model.js @@ -1,19 +1,26 @@ import mongoose from "mongoose" const cmpRisSchema = new mongoose.Schema( { - About_Us: { + title: { type: String, required: true }, + page_data: { + type: String, + required: true + }, + image: + { + public_id: { + type: String, + + }, + url: { + type: String, + + }, + }, - Terms_and_Conditions: { - type: String, - required: true - }, - Privacy_Policy: { - type: String, - required: true - }, }, { timestamps: true } diff --git a/routes/cmp-restriction-Route.js b/routes/cmp-restriction-Route.js index 39e3c8b..4658cd5 100644 --- a/routes/cmp-restriction-Route.js +++ b/routes/cmp-restriction-Route.js @@ -3,16 +3,19 @@ import { createRestriction, getAllRestriction, updateRestriction, - getOneRestriction + getOneRestriction, + deleteCms } from "../controllers/cmp-restriction-Controller.js" const router = express.Router(); -router.route("/restriction/create/").post(createRestriction) import { isAuthenticatedUser, authorizeRoles } from "../middlewares/auth.js" +router.route("/restriction/cms/create/").post(isAuthenticatedUser, authorizeRoles('admin'), createRestriction) router.route("/restriction/getAll").get(getAllRestriction) router.route("/restriction/getOne/:id").get(getOneRestriction) -router.route("/restriction/update/:id").put(isAuthenticatedUser, authorizeRoles('admin'), updateRestriction); +router.route("/restriction/cms/update/:id").put(isAuthenticatedUser, authorizeRoles('admin'), updateRestriction); +router.route("/restriction/cms/delete/:id").delete(isAuthenticatedUser, authorizeRoles('admin'), deleteCms); + export default router; getAllRestriction \ No newline at end of file