requirement add
4
app.js
@ -49,4 +49,8 @@ app.use("/api", cmpRistriction);
|
||||
//feedback
|
||||
import feedback from "./routes/feedbackRoute.js"
|
||||
app.use("/api", feedback);
|
||||
|
||||
//feedback
|
||||
import requirement from "./routes/RequirementRoute.js"
|
||||
app.use("/api", requirement);
|
||||
export default app;
|
@ -8,7 +8,7 @@ export const createNews = async (req, res) => {
|
||||
try {
|
||||
const files = req.files.image;
|
||||
|
||||
// console.log(files)
|
||||
console.log(files.tempFilePath)
|
||||
const myCloud = await cloudinary.uploader.upload(files.tempFilePath, {
|
||||
folder: "cmp/News",
|
||||
},
|
||||
|
191
controllers/RequirementController.js
Normal file
@ -0,0 +1,191 @@
|
||||
import RequirementModel from "../models/RequirementModel.js"
|
||||
import cloudinary from "cloudinary";
|
||||
// import cloudinary from "../Utils/cloudinary.js"
|
||||
//import { v2 as cloudinary } from 'cloudinary'
|
||||
|
||||
export const createRequirement = async (req, res) => {
|
||||
|
||||
try {
|
||||
let images = [];
|
||||
let Allfiles = req.files.image;
|
||||
// console.log(typeof Allfiles.tempFilePath)
|
||||
if (typeof Allfiles.tempFilePath === "string") {
|
||||
let filepath = Allfiles.tempFilePath;
|
||||
// console.log(filepath)
|
||||
images.push(filepath)
|
||||
} else {
|
||||
Allfiles.map(item => {
|
||||
images.push(item.tempFilePath);
|
||||
})
|
||||
}
|
||||
// console.log(images.length)
|
||||
const imagesLinks = [];
|
||||
for (let i = 0; i < images.length; i++) {
|
||||
const result = await cloudinary.v2.uploader.upload(images[i], {
|
||||
folder: "cmp",
|
||||
});
|
||||
|
||||
imagesLinks.push({
|
||||
public_id: result.public_id,
|
||||
url: result.secure_url,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
req.body.image = imagesLinks;
|
||||
req.body.addedBy = req.user.id;
|
||||
|
||||
const Requirement = await RequirementModel.create(req.body);
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
msg: " create Requirement Successfully!!",
|
||||
Requirement,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: "Failled to create !!"
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
//get All Requirement
|
||||
export const getAllRequirement = async (req, res) => {
|
||||
|
||||
try {
|
||||
const Requirement = await RequirementModel.find();
|
||||
// console.log(news)
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
msg: " fetch Successfully!!",
|
||||
Requirement,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: "Failled to fetch !!"
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
//get One Requirement
|
||||
export const getOneRequirement = async (req, res) => {
|
||||
|
||||
try {
|
||||
const Requirement = await RequirementModel.findById(req.params.id);
|
||||
// console.log(news)
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
msg: " fetch Successfully!!",
|
||||
Requirement,
|
||||
});
|
||||
} catch (error) {
|
||||
// console.log(error)
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: "Failled to fetch !!"
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// 3.update Requirement
|
||||
export const updateRequirement = async (req, res) => {
|
||||
try {
|
||||
const Requirement = await RequirementModel.findById(req.params.id);
|
||||
if (!Requirement) {
|
||||
return res.status(400).json({ message: 'Requirement Not Found' });
|
||||
}
|
||||
//handle image------------------------------------------------------------
|
||||
let images = [];
|
||||
let Allfiles = req.files.image;
|
||||
// console.log(typeof Allfiles.tempFilePath)
|
||||
if (typeof Allfiles.tempFilePath === "string") {
|
||||
let filepath = Allfiles.tempFilePath;
|
||||
// console.log(filepath)
|
||||
images.push(filepath)
|
||||
} else {
|
||||
Allfiles.map(item => {
|
||||
images.push(item.tempFilePath);
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
if (images !== undefined) {
|
||||
// Deleting Images From Cloudinary
|
||||
for (let i = 0; i < Requirement.image.length; i++) {
|
||||
await cloudinary.v2.uploader.destroy(Requirement.image[i].public_id);
|
||||
}
|
||||
|
||||
const imagesLinks = [];
|
||||
|
||||
for (let i = 0; i < images.length; i++) {
|
||||
const result = await cloudinary.v2.uploader.upload(images[i], {
|
||||
folder: "cmp",
|
||||
});
|
||||
|
||||
imagesLinks.push({
|
||||
public_id: result.public_id,
|
||||
url: result.secure_url,
|
||||
});
|
||||
}
|
||||
|
||||
req.body.image = imagesLinks;
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
const ModifyRequirement = await RequirementModel.findByIdAndUpdate(req.params.id, req.body,
|
||||
|
||||
{
|
||||
new: true,
|
||||
runValidators: true,
|
||||
useFindAndModify: false
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
ModifyRequirement
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
// console.log(error)
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: "Failled to UpDate !!"
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//delete one Requirement
|
||||
export const deleteOneRequirement = async (req, res) => {
|
||||
|
||||
try {
|
||||
const findRequirement = await RequirementModel.findById(req.params.id);
|
||||
|
||||
if (!findRequirement) {
|
||||
return res.status(400).json({ message: 'Requirement Not Found' });
|
||||
}
|
||||
// Deleting Images From Cloudinary
|
||||
for (let i = 0; i < findRequirement.image.length; i++) {
|
||||
await cloudinary.v2.uploader.destroy(findRequirement.image[i].public_id);
|
||||
}
|
||||
|
||||
|
||||
await findRequirement.remove();
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
msg: "Requirement Deleted Successfully!!",
|
||||
// news,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: "Failled to Delete !!"
|
||||
});
|
||||
}
|
||||
|
||||
};
|
40
models/RequirementModel.js
Normal file
@ -0,0 +1,40 @@
|
||||
import mongoose from "mongoose"
|
||||
const RequirementSchema = new mongoose.Schema(
|
||||
{
|
||||
title: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
areaOfInterest: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
|
||||
image: [
|
||||
{
|
||||
public_id: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
url: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
description: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
|
||||
addedBy: {
|
||||
type: mongoose.Schema.ObjectId,
|
||||
ref: "User",
|
||||
required: true,
|
||||
},
|
||||
|
||||
}, { timestamps: true }
|
||||
);
|
||||
const RequirementModel = mongoose.model("Requirement", RequirementSchema);
|
||||
export default RequirementModel;
|
17
routes/RequirementRoute.js
Normal file
@ -0,0 +1,17 @@
|
||||
import express from "express";
|
||||
import {
|
||||
createRequirement,
|
||||
getAllRequirement,
|
||||
getOneRequirement,
|
||||
updateRequirement,
|
||||
deleteOneRequirement
|
||||
} from "../controllers/RequirementController.js"
|
||||
import { isAuthenticatedUser, authorizeRoles } from "../middlewares/auth.js"
|
||||
const router = express.Router();
|
||||
|
||||
router.route("/requirement/create/").post(isAuthenticatedUser, createRequirement)
|
||||
router.route("/requirement/getAll/").get(isAuthenticatedUser, getAllRequirement)
|
||||
router.route("/requirement/getOne/:id").get(isAuthenticatedUser, getOneRequirement)
|
||||
router.route("/requirement/update/:id").put(isAuthenticatedUser, updateRequirement);
|
||||
router.route("/requirement/delete/:id").delete(isAuthenticatedUser, deleteOneRequirement);
|
||||
export default router;
|
@ -4,7 +4,7 @@ dotenv.config()
|
||||
import app from "./app.js"
|
||||
import connectDatabase from "./database/db.js";
|
||||
import cloudinary from "cloudinary"
|
||||
// const PORT = 5000;
|
||||
|
||||
// Connecting to database
|
||||
connectDatabase();
|
||||
|
||||
|
BIN
tmp/tmp-1-1657691541934
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657691823983
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657691932400
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657691961961
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657692044911
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657692073291
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657692156393
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657692270744
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657692521378
Normal file
After Width: | Height: | Size: 1.7 MiB |
BIN
tmp/tmp-1-1657693506068
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657693804951
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657693969464
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657693993416
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657694204918
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657694244500
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657694320050
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657694351787
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657694406205
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657694710102
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657694808176
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657694874791
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657694912620
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657694941061
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657694991436
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657695033209
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657695228328
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657695348961
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657695478465
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
tmp/tmp-1-1657695634319
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
tmp/tmp-1-1657695672740
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
tmp/tmp-1-1657695842122
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
tmp/tmp-1-1657696047964
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657696083335
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657696141057
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657696164150
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657696330842
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657696420907
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657696477532
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657696567793
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657696610955
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657696751615
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657696838738
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657696859057
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657696892113
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657696962305
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657696998330
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697040619
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697066325
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697089774
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697108436
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697151150
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697202911
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697285947
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697368226
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697388226
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697426168
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697467436
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697504819
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697560611
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697838044
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697897004
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697940618
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657697985619
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657698155777
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657698193872
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657698618005
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657698675708
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657698708070
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657698860687
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657698924545
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657699013432
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657699115862
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657699252595
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1657699605267
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657699660055
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657699689974
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657699773929
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657700999405
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1657702223617
Normal file
After Width: | Height: | Size: 46 KiB |
BIN
tmp/tmp-10-1657691629579
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-11-1657691629586
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
tmp/tmp-2-1657691541944
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
tmp/tmp-2-1657691823999
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
tmp/tmp-2-1657691932416
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
tmp/tmp-2-1657691961973
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
tmp/tmp-2-1657692044925
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
tmp/tmp-2-1657692073317
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
tmp/tmp-2-1657692156426
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
tmp/tmp-2-1657692270759
Normal file
After Width: | Height: | Size: 1.4 MiB |
BIN
tmp/tmp-2-1657692577479
Normal file
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-2-1657693506077
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-2-1657693804954
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
tmp/tmp-2-1657693969470
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
tmp/tmp-2-1657693993419
Normal file
After Width: | Height: | Size: 14 KiB |