This commit is contained in:
pawan-dot 2022-06-08 16:24:19 +05:30
parent 5ba1a2129c
commit 7157a7ce8c
34 changed files with 251 additions and 10 deletions

View File

@ -15,7 +15,7 @@ const sendToken = (user, statusCode, res) => {
// res.status(statusCode).json({ // res.status(statusCode).json({
success: true, success: true,
user, // user,
token, token,
}); });
}; };

5
app.js
View File

@ -22,12 +22,17 @@ app.use(fileUpload({
useTempFiles: true useTempFiles: true
})); }));
//auth
import user from "./routes/userRoute.js" import user from "./routes/userRoute.js"
app.use("/api", user); app.use("/api", user);
//category
import category from "./routes/categoryRoute.js" import category from "./routes/categoryRoute.js"
app.use("/api", category); app.use("/api", category);
//directory //directory
import directory from "./routes/directoryRoute.js" import directory from "./routes/directoryRoute.js"
app.use("/api", directory); app.use("/api", directory);
//News
import news from "./routes/newsRoute.js"
app.use("/api", news);
export default app; export default app;

View File

@ -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 !!"
});
}
};

View File

@ -10,7 +10,7 @@ export const createCategory = async (req, res) => {
// console.log(files) // console.log(files)
const myCloud = await cloudinary.uploader.upload(files.tempFilePath, { const myCloud = await cloudinary.uploader.upload(files.tempFilePath, {
folder: "image", folder: "cmp/image",
}, },
function (error, result) { (result, error) }); function (error, result) { (result, error) });
const { name } = req.body; 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) => { export const updateCategory = async (req, res) => {
try { try {
const newCategoryData = { const newCategoryData = {
name: req.body.name, name: req.body.name,
// email: req.body.email,
}; };
const files = req.files.image; const files = req.files.image;

View File

@ -2,7 +2,6 @@ import directoryModel from "../models/directoryModel.js";
export const createDirectory = async (req, res) => { export const createDirectory = async (req, res) => {
try { try {
console.log("hii")
const { name, const { name,
phone, phone,
email, email,
@ -17,7 +16,7 @@ export const createDirectory = async (req, res) => {
LinkedinUrl, LinkedinUrl,
FacebookUrl, FacebookUrl,
InstagramUrl, InstagramUrl,
} = req.body.state; } = req.body;
// console.log(name) // console.log(name)
const data = await directoryModel.create({ const data = await directoryModel.create({
@ -43,7 +42,7 @@ export const createDirectory = async (req, res) => {
data, data,
}); });
} catch (error) { } catch (error) {
console.log(err) console.log(error)
res.status(500).json({ res.status(500).json({
success: false, success: false,
msg: "Failled to create !!" 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 //delete one category
export const deleteOneDirectory = async (req, res) => { export const deleteOneDirectory = async (req, res) => {

View File

@ -15,7 +15,7 @@ export const registerUser = async (req, res) => {
password password
}); });
// const token = user.getJWTToken(); // const token = user.getJWTToken();
// console.log(token) // // console.log(token)
// res.status(201).json({ // res.status(201).json({
// success: true, // success: true,
// token, // token,

31
models/newsModel.js Normal file
View File

@ -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;

View File

@ -2,7 +2,7 @@ import express from "express";
import { import {
createDirectory, createDirectory,
getAllDirectory, getAllDirectory,
// updateCategory, updateDirectory,
deleteOneDirectory, deleteOneDirectory,
getOneDirectory getOneDirectory
} from "../controllers/directoryController.js" } from "../controllers/directoryController.js"
@ -11,6 +11,6 @@ import { isAuthenticated } from "../middlewares/auth.js"
router.route("/directory/create/").post(createDirectory) router.route("/directory/create/").post(createDirectory)
router.route("/directory/getAll/").get(getAllDirectory) router.route("/directory/getAll/").get(getAllDirectory)
router.route("/directory/getOne/:id").get(getOneDirectory) 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); router.route("/directory/delete/:id").delete(deleteOneDirectory);
export default router; export default router;

16
routes/newsRoute.js Normal file
View File

@ -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;

BIN
tmp/tmp-1-1654665010798 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 KiB

BIN
tmp/tmp-1-1654665518486 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 KiB

BIN
tmp/tmp-1-1654667388170 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 KiB

BIN
tmp/tmp-1-1654667485524 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

BIN
tmp/tmp-1-1654669073057 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

BIN
tmp/tmp-1-1654669516053 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

BIN
tmp/tmp-1-1654676322204 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

BIN
tmp/tmp-1-1654676407434 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

BIN
tmp/tmp-1-1654677603934 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 KiB

BIN
tmp/tmp-1-1654678061267 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 KiB

BIN
tmp/tmp-1-1654679838534 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

BIN
tmp/tmp-1-1654680793857 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

BIN
tmp/tmp-2-1654665778551 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 KiB

BIN
tmp/tmp-2-1654667734153 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

BIN
tmp/tmp-2-1654677276716 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

BIN
tmp/tmp-2-1654677785820 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 KiB

BIN
tmp/tmp-2-1654678093876 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

BIN
tmp/tmp-2-1654680852572 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

BIN
tmp/tmp-3-1654666356286 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

BIN
tmp/tmp-3-1654677498396 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

BIN
tmp/tmp-3-1654678122681 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

BIN
tmp/tmp-3-1654680986071 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 318 KiB

BIN
tmp/tmp-4-1654677539041 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 KiB

BIN
tmp/tmp-4-1654679411745 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 MiB

BIN
tmp/tmp-5-1654679688769 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB