diff --git a/Utils/jwtToken.js b/Utils/jwtToken.js index 28576dd..a6f85c3 100644 --- a/Utils/jwtToken.js +++ b/Utils/jwtToken.js @@ -15,7 +15,7 @@ const sendToken = (user, statusCode, res) => { // res.status(statusCode).json({ success: true, - user, + // user, token, }); }; diff --git a/app.js b/app.js index 8fa5f9b..0e7384c 100644 --- a/app.js +++ b/app.js @@ -22,12 +22,17 @@ app.use(fileUpload({ useTempFiles: true })); +//auth import user from "./routes/userRoute.js" app.use("/api", user); +//category import category from "./routes/categoryRoute.js" app.use("/api", category); //directory import directory from "./routes/directoryRoute.js" app.use("/api", directory); +//News +import news from "./routes/newsRoute.js" +app.use("/api", news); export default app; \ No newline at end of file diff --git a/controllers/NewsController.js b/controllers/NewsController.js new file mode 100644 index 0000000..bd34af0 --- /dev/null +++ b/controllers/NewsController.js @@ -0,0 +1,160 @@ +import News from "../models/newsModel.js" +import cloudinary from "cloudinary"; +// import cloudinary from "../Utils/cloudinary.js" +//import { v2 as cloudinary } from 'cloudinary' + +export const createNews = async (req, res) => { + + try { + const files = req.files.image; + + // console.log(files) + const myCloud = await cloudinary.uploader.upload(files.tempFilePath, { + folder: "cmp/News", + }, + function (error, result) { (result, error) }); + const { title, description } = req.body; + + const news = await News.create({ + title, + image: { + public_id: myCloud.public_id, + url: myCloud.secure_url, + }, + description, + + }); + res.status(201).json({ + success: true, + msg: " create News Successfully!!", + news, + }); + } catch (error) { + res.status(500).json({ + success: false, + msg: "Failled to create !!" + }); + } + +}; +//get All news +export const getAllNews = async (req, res) => { + + try { + const news = await News.find(); + // console.log(news) + res.status(200).json({ + success: true, + msg: " fetch Successfully!!", + news, + }); + } catch (error) { + res.status(500).json({ + success: false, + msg: "Failled to fetch !!" + }); + } + +}; +//get One news +export const getOneNews = async (req, res) => { + + try { + const news = await News.findById(req.params.id); + // console.log(news) + res.status(200).json({ + success: true, + msg: " fetch Successfully!!", + news, + }); + } catch (error) { + // console.log(error) + res.status(500).json({ + success: false, + msg: "Failled to fetch !!" + }); + } + +}; + +// 3.update news +export const updateNews = async (req, res) => { + try { + const newNewsData = { + title: req.body.title, + description: req.body.description, + }; + console.log() + const files = req.files.image; + + if (req.files.image !== "") { + const newsImage = await News.findById(req.params.id); + + const imgId = newsImage.image.public_id; + + //delete image from claudinary + await cloudinary.uploader.destroy(imgId) + // await cloudinary.uploader.destroy(imageId, function (result) { console.log(result) }); + const myCloud = await cloudinary.uploader.upload(files.tempFilePath, { + folder: "cmp/image", + }, + function (error, result) { (result, error) }); + // console.log(myCloud) + newNewsData.image = { + public_id: myCloud.public_id, + url: myCloud.secure_url, + }; + } + //req.user.id, + const ModifyNews = await News.findByIdAndUpdate(req.params.id, newNewsData, + + { new: true } + // runValidators: true, + // useFindAndModify: false, + ); + + res.status(200).json({ + success: true, + ModifyNews + }); + + } catch (error) { + // console.log(error) + res.status(500).json({ + success: false, + msg: "Failled to UpDate !!" + + }); + } + +}; + +//delete one news +export const deleteOneNews = async (req, res) => { + + try { + //delete image from cloudinary + const findNews = await News.findById(req.params.id); + // console.log(newsImageId) + const imgId = findNews.image.public_id; + await cloudinary.uploader.destroy(imgId) + + //-------------------------// + const news = await News.findByIdAndDelete(req.params.id) + if (!news) { + return res.status(400).json({ message: 'News Not Found' }); + } + await news.remove(); + res.status(200).json({ + success: true, + msg: "News Deleted Successfully!!", + // news, + }); + } catch (error) { + res.status(500).json({ + success: false, + msg: "Failled to Delete !!" + }); + } + +}; diff --git a/controllers/categoryController.js b/controllers/categoryController.js index 51b7eea..19d0e65 100644 --- a/controllers/categoryController.js +++ b/controllers/categoryController.js @@ -10,7 +10,7 @@ export const createCategory = async (req, res) => { // console.log(files) const myCloud = await cloudinary.uploader.upload(files.tempFilePath, { - folder: "image", + folder: "cmp/image", }, function (error, result) { (result, error) }); const { name } = req.body; @@ -76,12 +76,11 @@ export const getOneCategory = async (req, res) => { }; -// 8.update Category +// 3.update Category export const updateCategory = async (req, res) => { try { const newCategoryData = { name: req.body.name, - // email: req.body.email, }; const files = req.files.image; diff --git a/controllers/directoryController.js b/controllers/directoryController.js index 001579e..a5e9ac6 100644 --- a/controllers/directoryController.js +++ b/controllers/directoryController.js @@ -2,7 +2,6 @@ import directoryModel from "../models/directoryModel.js"; export const createDirectory = async (req, res) => { try { - console.log("hii") const { name, phone, email, @@ -17,7 +16,7 @@ export const createDirectory = async (req, res) => { LinkedinUrl, FacebookUrl, InstagramUrl, - } = req.body.state; + } = req.body; // console.log(name) const data = await directoryModel.create({ @@ -43,7 +42,7 @@ export const createDirectory = async (req, res) => { data, }); } catch (error) { - console.log(err) + console.log(error) res.status(500).json({ success: false, msg: "Failled to create !!" @@ -90,6 +89,37 @@ export const getOneDirectory = async (req, res) => { }); } +}; + +export const updateDirectory = async (req, res) => { + try { + // const newDirectoryData = { + // name: req.body.name, + // // email: req.body.email, + // }; + // console.log(newCategoryData) + //req.user.id, + const ModifyDirectory = await directoryModel.findByIdAndUpdate(req.params.id, req.body.state, + + { new: true } + // runValidators: true, + // useFindAndModify: false, + ); + + res.status(200).json({ + success: true, + ModifyDirectory + }); + + } catch (error) { + // console.log(error) + res.status(500).json({ + success: false, + msg: "Failled to UpDate !!" + + }); + } + }; //delete one category export const deleteOneDirectory = async (req, res) => { diff --git a/controllers/userController.js b/controllers/userController.js index d79820b..5a50c55 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -15,7 +15,7 @@ export const registerUser = async (req, res) => { password }); // const token = user.getJWTToken(); - // console.log(token) + // // console.log(token) // res.status(201).json({ // success: true, // token, diff --git a/models/newsModel.js b/models/newsModel.js new file mode 100644 index 0000000..8d02c39 --- /dev/null +++ b/models/newsModel.js @@ -0,0 +1,31 @@ +import mongoose from "mongoose" +const newsSchema = new mongoose.Schema( + { + title: { + type: String, + required: true + }, + image: + { + public_id: { + type: String, + required: true, + }, + url: { + type: String, + required: true, + }, + }, + description: { + type: String, + required: true + }, + addedOn: { + type: Date, + default: Date.now + }, + + }, { timestamps: true } +); +const newsUpload = mongoose.model("news", newsSchema); +export default newsUpload; \ No newline at end of file diff --git a/routes/directoryRoute.js b/routes/directoryRoute.js index f124687..de7ef49 100644 --- a/routes/directoryRoute.js +++ b/routes/directoryRoute.js @@ -2,7 +2,7 @@ import express from "express"; import { createDirectory, getAllDirectory, - // updateCategory, + updateDirectory, deleteOneDirectory, getOneDirectory } from "../controllers/directoryController.js" @@ -11,6 +11,6 @@ import { isAuthenticated } from "../middlewares/auth.js" router.route("/directory/create/").post(createDirectory) router.route("/directory/getAll/").get(getAllDirectory) router.route("/directory/getOne/:id").get(getOneDirectory) -// router.route("/category/update/:id").put(updateCategory); +router.route("/directory/update/:id").put(updateDirectory); router.route("/directory/delete/:id").delete(deleteOneDirectory); export default router; \ No newline at end of file diff --git a/routes/newsRoute.js b/routes/newsRoute.js new file mode 100644 index 0000000..87a370e --- /dev/null +++ b/routes/newsRoute.js @@ -0,0 +1,16 @@ +import express from "express"; +import { + createNews, + getAllNews, + getOneNews, + updateNews, + deleteOneNews +} from "../controllers/NewsController.js" +const router = express.Router(); + +router.route("/news/create/").post(createNews) +router.route("/news/getAll/").get(getAllNews) +router.route("/news/getOne/:id").get(getOneNews) +router.route("/news/update/:id").put(updateNews); +router.route("/news/delete/:id").delete(deleteOneNews); +export default router; \ No newline at end of file diff --git a/tmp/tmp-1-1654665010798 b/tmp/tmp-1-1654665010798 new file mode 100644 index 0000000..f61ad05 Binary files /dev/null and b/tmp/tmp-1-1654665010798 differ diff --git a/tmp/tmp-1-1654665518486 b/tmp/tmp-1-1654665518486 new file mode 100644 index 0000000..f61ad05 Binary files /dev/null and b/tmp/tmp-1-1654665518486 differ diff --git a/tmp/tmp-1-1654667388170 b/tmp/tmp-1-1654667388170 new file mode 100644 index 0000000..f61ad05 Binary files /dev/null and b/tmp/tmp-1-1654667388170 differ diff --git a/tmp/tmp-1-1654667485524 b/tmp/tmp-1-1654667485524 new file mode 100644 index 0000000..62743be Binary files /dev/null and b/tmp/tmp-1-1654667485524 differ diff --git a/tmp/tmp-1-1654669073057 b/tmp/tmp-1-1654669073057 new file mode 100644 index 0000000..6fb99c9 Binary files /dev/null and b/tmp/tmp-1-1654669073057 differ diff --git a/tmp/tmp-1-1654669516053 b/tmp/tmp-1-1654669516053 new file mode 100644 index 0000000..25c7510 Binary files /dev/null and b/tmp/tmp-1-1654669516053 differ diff --git a/tmp/tmp-1-1654676322204 b/tmp/tmp-1-1654676322204 new file mode 100644 index 0000000..b9a863b Binary files /dev/null and b/tmp/tmp-1-1654676322204 differ diff --git a/tmp/tmp-1-1654676407434 b/tmp/tmp-1-1654676407434 new file mode 100644 index 0000000..b9a863b Binary files /dev/null and b/tmp/tmp-1-1654676407434 differ diff --git a/tmp/tmp-1-1654677603934 b/tmp/tmp-1-1654677603934 new file mode 100644 index 0000000..f61ad05 Binary files /dev/null and b/tmp/tmp-1-1654677603934 differ diff --git a/tmp/tmp-1-1654678061267 b/tmp/tmp-1-1654678061267 new file mode 100644 index 0000000..f61ad05 Binary files /dev/null and b/tmp/tmp-1-1654678061267 differ diff --git a/tmp/tmp-1-1654679838534 b/tmp/tmp-1-1654679838534 new file mode 100644 index 0000000..6fb99c9 Binary files /dev/null and b/tmp/tmp-1-1654679838534 differ diff --git a/tmp/tmp-1-1654680793857 b/tmp/tmp-1-1654680793857 new file mode 100644 index 0000000..62743be Binary files /dev/null and b/tmp/tmp-1-1654680793857 differ diff --git a/tmp/tmp-2-1654665778551 b/tmp/tmp-2-1654665778551 new file mode 100644 index 0000000..f61ad05 Binary files /dev/null and b/tmp/tmp-2-1654665778551 differ diff --git a/tmp/tmp-2-1654667734153 b/tmp/tmp-2-1654667734153 new file mode 100644 index 0000000..b9a863b Binary files /dev/null and b/tmp/tmp-2-1654667734153 differ diff --git a/tmp/tmp-2-1654677276716 b/tmp/tmp-2-1654677276716 new file mode 100644 index 0000000..62743be Binary files /dev/null and b/tmp/tmp-2-1654677276716 differ diff --git a/tmp/tmp-2-1654677785820 b/tmp/tmp-2-1654677785820 new file mode 100644 index 0000000..f61ad05 Binary files /dev/null and b/tmp/tmp-2-1654677785820 differ diff --git a/tmp/tmp-2-1654678093876 b/tmp/tmp-2-1654678093876 new file mode 100644 index 0000000..6fb99c9 Binary files /dev/null and b/tmp/tmp-2-1654678093876 differ diff --git a/tmp/tmp-2-1654680852572 b/tmp/tmp-2-1654680852572 new file mode 100644 index 0000000..62743be Binary files /dev/null and b/tmp/tmp-2-1654680852572 differ diff --git a/tmp/tmp-3-1654666356286 b/tmp/tmp-3-1654666356286 new file mode 100644 index 0000000..6fb99c9 Binary files /dev/null and b/tmp/tmp-3-1654666356286 differ diff --git a/tmp/tmp-3-1654677498396 b/tmp/tmp-3-1654677498396 new file mode 100644 index 0000000..62743be Binary files /dev/null and b/tmp/tmp-3-1654677498396 differ diff --git a/tmp/tmp-3-1654678122681 b/tmp/tmp-3-1654678122681 new file mode 100644 index 0000000..25c7510 Binary files /dev/null and b/tmp/tmp-3-1654678122681 differ diff --git a/tmp/tmp-3-1654680986071 b/tmp/tmp-3-1654680986071 new file mode 100644 index 0000000..5276b0e Binary files /dev/null and b/tmp/tmp-3-1654680986071 differ diff --git a/tmp/tmp-4-1654677539041 b/tmp/tmp-4-1654677539041 new file mode 100644 index 0000000..f61ad05 Binary files /dev/null and b/tmp/tmp-4-1654677539041 differ diff --git a/tmp/tmp-4-1654679411745 b/tmp/tmp-4-1654679411745 new file mode 100644 index 0000000..ce976ad Binary files /dev/null and b/tmp/tmp-4-1654679411745 differ diff --git a/tmp/tmp-5-1654679688769 b/tmp/tmp-5-1654679688769 new file mode 100644 index 0000000..6fb99c9 Binary files /dev/null and b/tmp/tmp-5-1654679688769 differ