make it bet
This commit is contained in:
parent
0b582e6019
commit
1b91e4bf4c
4
app.js
4
app.js
@ -37,5 +37,7 @@ app.use("/api", news);
|
||||
//Events
|
||||
import Event from "./routes/EventsRoute.js"
|
||||
app.use("/api", Event);
|
||||
|
||||
//Offers
|
||||
import Offer from "./routes/OffersRoute.js"
|
||||
app.use("/api", Offer);
|
||||
export default app;
|
@ -78,7 +78,7 @@ export const getOneEvent = async (req, res) => {
|
||||
|
||||
};
|
||||
|
||||
// 3.update Category
|
||||
// 3.update Event
|
||||
export const updateEvent = async (req, res) => {
|
||||
try {
|
||||
const newEventData = {
|
||||
|
163
controllers/OffersController.js
Normal file
163
controllers/OffersController.js
Normal file
@ -0,0 +1,163 @@
|
||||
import Offers from "../models/OffersModel.js"
|
||||
import cloudinary from "cloudinary";
|
||||
|
||||
export const createOffer = 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, location, description, bisunessName } = req.body;
|
||||
|
||||
const data = await Offers.create({
|
||||
title,
|
||||
image: {
|
||||
public_id: myCloud.public_id,
|
||||
url: myCloud.secure_url,
|
||||
},
|
||||
location,
|
||||
description,
|
||||
bisunessName
|
||||
|
||||
});
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
msg: " create Offer Successfully!!",
|
||||
data,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: "Failled to create Offer !!"
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
//get All Product
|
||||
export const getAllOffer = async (req, res) => {
|
||||
|
||||
try {
|
||||
const offer = await Offers.find();
|
||||
// console.log(category)
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
msg: " fetch All Offer Successfully!!",
|
||||
offer,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: "Failled to fetch !!"
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
//get One Product
|
||||
export const getOneOffer = async (req, res) => {
|
||||
|
||||
try {
|
||||
const offer = await Offers.findById(req.params.id);
|
||||
// console.log(category)
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
msg: " fetch Successfully!!",
|
||||
offer,
|
||||
});
|
||||
} catch (error) {
|
||||
// console.log(error)
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: "Failled to fetch !!"
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// 3.update offer
|
||||
export const updateOffer = async (req, res) => {
|
||||
try {
|
||||
const newOfferData = {
|
||||
title: req.body.title,
|
||||
description: req.body.description,
|
||||
location: req.body.location,
|
||||
bisunessName: req.body.bisunessName
|
||||
};
|
||||
|
||||
|
||||
if (req.files) {
|
||||
const files = req.files.image;
|
||||
const getOffer = await Offers.findById(req.params.id);
|
||||
|
||||
const imageId = getOffer.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)
|
||||
newOfferData.image = {
|
||||
public_id: myCloud.public_id,
|
||||
url: myCloud.secure_url,
|
||||
};
|
||||
}
|
||||
|
||||
//req.user.id,
|
||||
const ModifyOffer = await Offers.findByIdAndUpdate(req.params.id, newOfferData,
|
||||
|
||||
{ new: true }
|
||||
// runValidators: true,
|
||||
// useFindAndModify: false,
|
||||
);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
ModifyOffer
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: "Failled to UpDate Offer!!"
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//delete one category
|
||||
export const deleteOffer = async (req, res) => {
|
||||
|
||||
try {
|
||||
//delete image from cloudinary
|
||||
const getOffer = await Offers.findById(req.params.id);
|
||||
// console.log(categ)
|
||||
const imageId = getOffer.image.public_id;
|
||||
await cloudinary.uploader.destroy(imageId)
|
||||
|
||||
//-------------------------//
|
||||
const offer = await Offers.findByIdAndDelete(req.params.id)
|
||||
if (!offer) {
|
||||
return res.status(400).json({ message: 'Offer Not Found' });
|
||||
}
|
||||
await offer.remove();
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
msg: "Offer Deleted Successfully!!",
|
||||
// category,
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
msg: "Failled to Delete Offer !!"
|
||||
});
|
||||
}
|
||||
|
||||
};
|
@ -4,10 +4,11 @@ export const createDirectory = async (req, res) => {
|
||||
|
||||
try {
|
||||
let images;
|
||||
// console.log(req.body)
|
||||
// console.log(req.files.image)
|
||||
console.log(req.body)
|
||||
console.log(req.files)
|
||||
if (req.files) {
|
||||
const files = req.files.image;
|
||||
console.log(files)
|
||||
const myCloud = await cloudinary.uploader.upload(files.tempFilePath, {
|
||||
folder: "cmp/image",
|
||||
},
|
||||
|
39
models/OffersModel.js
Normal file
39
models/OffersModel.js
Normal file
@ -0,0 +1,39 @@
|
||||
import mongoose from "mongoose"
|
||||
const offerSchema = new mongoose.Schema(
|
||||
{
|
||||
title: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
image:
|
||||
{
|
||||
public_id: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
url: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
bisunessName: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
location: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
description: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
addedOn: {
|
||||
type: Date,
|
||||
default: Date.now
|
||||
},
|
||||
|
||||
}, { timestamps: true }
|
||||
);
|
||||
const offerModel = mongoose.model("offer", offerSchema);
|
||||
export default offerModel
|
16
routes/OffersRoute.js
Normal file
16
routes/OffersRoute.js
Normal file
@ -0,0 +1,16 @@
|
||||
import express from "express";
|
||||
import {
|
||||
createOffer,
|
||||
getAllOffer,
|
||||
updateOffer,
|
||||
deleteOffer,
|
||||
getOneOffer
|
||||
} from "../controllers/OffersController.js"
|
||||
const router = express.Router();
|
||||
|
||||
router.route("/offer/create/").post(createOffer)
|
||||
router.route("/offer/getAll/").get(getAllOffer)
|
||||
router.route("/offer/getOne/:id").get(getOneOffer)
|
||||
router.route("/offer/update/:id").put(updateOffer);
|
||||
router.route("/offer/delete/:id").delete(deleteOffer);
|
||||
export default router;
|
BIN
tmp/tmp-1-1655124832373
Normal file
BIN
tmp/tmp-1-1655124832373
Normal file
Binary file not shown.
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1655180676619
Normal file
BIN
tmp/tmp-1-1655180676619
Normal file
Binary file not shown.
After Width: | Height: | Size: 620 KiB |
BIN
tmp/tmp-1-1655191402583
Normal file
BIN
tmp/tmp-1-1655191402583
Normal file
Binary file not shown.
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-1-1655192835089
Normal file
BIN
tmp/tmp-1-1655192835089
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.7 MiB |
BIN
tmp/tmp-1-1655193210829
Normal file
BIN
tmp/tmp-1-1655193210829
Normal file
Binary file not shown.
After Width: | Height: | Size: 80 KiB |
BIN
tmp/tmp-2-1655192977224
Normal file
BIN
tmp/tmp-2-1655192977224
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.4 MiB |
Loading…
Reference in New Issue
Block a user