make it betterl

This commit is contained in:
pawan-dot 2022-06-20 12:16:50 +05:30
parent 1b91e4bf4c
commit bb0e9f4213
16 changed files with 227 additions and 4 deletions

3
app.js
View File

@ -40,4 +40,7 @@ app.use("/api", Event);
//Offers
import Offer from "./routes/OffersRoute.js"
app.use("/api", Offer);
//Offers
import banner from "./routes/bannerRoute.js"
app.use("/api", banner);
export default app;

View File

@ -0,0 +1,165 @@
import Banners from "../models/bannerModel.js"
import cloudinary from "cloudinary";
// import cloudinary from "../Utils/cloudinary.js"
//import { v2 as cloudinary } from 'cloudinary'
export const createBanner = async (req, res) => {
try {
const files = req.files.image;
// console.log(files)
const myCloud = await cloudinary.uploader.upload(files.tempFilePath, {
folder: "cmp/image",
},
function (error, result) { (result, error) });
const { title, section, startDate, endDate } = req.body;
const data = await Banners.create({
title,
image: {
public_id: myCloud.public_id,
url: myCloud.secure_url,
},
section,
startDate,
endDate
});
res.status(201).json({
success: true,
msg: " create Banner Successfully!!",
data,
});
} catch (error) {
res.status(500).json({
success: false,
msg: "Failled to create Banner !!"
});
}
};
//get All Banners
export const getAllBanner = async (req, res) => {
try {
const banner = await Banners.find();
// console.log(category)
res.status(200).json({
success: true,
msg: " fetch Successfully!!",
banner,
});
} catch (error) {
res.status(500).json({
success: false,
msg: "Failled to fetch !!"
});
}
};
//get One Banner
export const getOneBanner = async (req, res) => {
try {
const banner = await Banners.findById(req.params.id);
// console.log(category)
res.status(200).json({
success: true,
msg: " fetch Successfully!!",
banner,
});
} catch (error) {
// console.log(error)
res.status(500).json({
success: false,
msg: "Failled to fetch !!"
});
}
};
// 3.update Event
export const updateBanner = async (req, res) => {
try {
const newBannerData = {
title: req.body.title,
section: req.body.section,
startDate: req.body.startDate,
endDate: req.body.endDate,
};
if (req.files) {
const files = req.files.image;
const getBanner = await Banners.findById(req.params.id);
const imageId = getBanner.image.public_id;
// console.log(imageId)
//delete image from claudinary
await cloudinary.uploader.destroy(imageId)
// await cloudinary.uploader.destroy(imageId, function (result) { console.log(result) });
const myCloud = await cloudinary.uploader.upload(files.tempFilePath, {
folder: "image",
},
function (error, result) { (result, error) });
// console.log(myCloud)
newBannerData.image = {
public_id: myCloud.public_id,
url: myCloud.secure_url,
};
}
//req.user.id,
const ModifyBanner = await Banners.findByIdAndUpdate(req.params.id, newBannerData,
{ new: true }
// runValidators: true,
// useFindAndModify: false,
);
res.status(200).json({
success: true,
ModifyBanner
});
} catch (error) {
// console.log(error)
res.status(500).json({
success: false,
msg: "Failled to UpDate Banner!!"
});
}
};
//delete one Banner
export const deleteBanner = async (req, res) => {
try {
//delete image from cloudinary
const getBanner = await Banners.findById(req.params.id);
// console.log(categ)
const imageId = getBanner.image.public_id;
await cloudinary.uploader.destroy(imageId)
//-------------------------//
const banner = await Banners.findByIdAndDelete(req.params.id)
if (!banner) {
return res.status(400).json({ message: 'banner Not Found' });
}
await banner.remove();
res.status(200).json({
success: true,
msg: "banner Deleted Successfully!!",
// category,
});
} catch (error) {
res.status(500).json({
success: false,
msg: "Failled to Delete banner !!"
});
}
};

View File

@ -4,11 +4,11 @@ export const createDirectory = async (req, res) => {
try {
let images;
console.log(req.body)
console.log(req.files)
// console.log(req.body)
// console.log(req.body.image)
if (req.files) {
const files = req.files.image;
console.log(files)
// console.log(files)
const myCloud = await cloudinary.uploader.upload(files.tempFilePath, {
folder: "cmp/image",
},

39
models/bannerModel.js Normal file
View File

@ -0,0 +1,39 @@
import mongoose from "mongoose"
const bannerSchema = new mongoose.Schema(
{
title: {
type: String,
required: true
},
image:
{
public_id: {
type: String,
required: true,
},
url: {
type: String,
required: true,
},
},
section: {
type: String,
required: true
},
startDate: {
type: Date,
required: true
},
endDate: {
type: Date,
required: true
},
addedOn: {
type: Date,
default: Date.now
},
}, { timestamps: true }
);
const bannerModel = mongoose.model("banner", bannerSchema);
export default bannerModel

16
routes/bannerRoute.js Normal file
View File

@ -0,0 +1,16 @@
import express from "express";
import {
createBanner,
getAllBanner,
updateBanner,
deleteBanner,
getOneBanner
} from "../controllers/bannerController.js"
const router = express.Router();
router.route("/banner/create/").post(createBanner)
router.route("/banner/getAll/").get(getAllBanner)
router.route("/banner/getOne/:id").get(getOneBanner)
router.route("/banner/update/:id").put(updateBanner);
router.route("/banner/delete/:id").delete(deleteBanner);
export default router;

BIN
tmp/tmp-1-1655704343318 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
tmp/tmp-1-1655704714841 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
tmp/tmp-1-1655707290465 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 318 KiB

BIN
tmp/tmp-2-1655704484188 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
tmp/tmp-2-1655704962606 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
tmp/tmp-3-1655705709722 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

BIN
tmp/tmp-4-1655705797633 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

BIN
tmp/tmp-5-1655705927779 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

BIN
tmp/tmp-6-1655706570178 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

BIN
tmp/tmp-7-1655706620303 Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB