Added Blog Backend
This commit is contained in:
parent
bc90edb1aa
commit
7151cba2c4
8
app.js
8
app.js
|
@ -8,21 +8,19 @@ const courseRoute = require("./routes/course");
|
||||||
const couponRoute = require("./routes/Coupon");
|
const couponRoute = require("./routes/Coupon");
|
||||||
const queryRoute = require("./routes/query");
|
const queryRoute = require("./routes/query");
|
||||||
const adminRoute = require("./routes/admin");
|
const adminRoute = require("./routes/admin");
|
||||||
|
const blogRouter = require("./routes/blog");
|
||||||
const port = process.env.PORT || 5000;
|
const port = process.env.PORT || 5000;
|
||||||
|
|
||||||
const cors = require("cors");
|
const cors = require("cors");
|
||||||
|
const app = express();
|
||||||
|
|
||||||
//const passport = require('passport');
|
//const passport = require('passport');
|
||||||
//const cookieSession = require('cookie-session') ;
|
//const cookieSession = require('cookie-session') ;
|
||||||
//require('./passport-setup') ;
|
//require('./passport-setup') ;
|
||||||
|
|
||||||
const app = express();
|
|
||||||
|
|
||||||
const MONGO_URI = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@cluster0.dqxva.mongodb.net/${process.env.MONGO_DEFAULT_DATABASE}?retryWrites=true&w=majority`;
|
const MONGO_URI = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@cluster0.dqxva.mongodb.net/${process.env.MONGO_DEFAULT_DATABASE}?retryWrites=true&w=majority`;
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(bodyparser.json());
|
app.use(bodyparser.json());
|
||||||
require("./models/Coupon");
|
|
||||||
|
|
||||||
// app.use(cookieSession({
|
// app.use(cookieSession({
|
||||||
// name: 'test-session',
|
// name: 'test-session',
|
||||||
|
@ -104,4 +102,6 @@ app.use(adminRoute);
|
||||||
|
|
||||||
app.use(couponRoute);
|
app.use(couponRoute);
|
||||||
|
|
||||||
|
app.use(blogRouter);
|
||||||
|
|
||||||
app.use(queryRoute);
|
app.use(queryRoute);
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,103 @@
|
||||||
|
const { log } = require("handlebars");
|
||||||
|
const Blog = require("../models/Blog");
|
||||||
|
const Course = require("../models/Course");
|
||||||
|
|
||||||
|
module.exports.getAllBlogs = async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
let blog = await Blog.find();
|
||||||
|
if (blog) {
|
||||||
|
res.json({ blogs: blog });
|
||||||
|
} else {
|
||||||
|
console.log("Error in Blog", blog);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
(err) => {
|
||||||
|
res.status(422).json({ error: err });
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.addBlog = async (req, res, next) => {
|
||||||
|
const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog.
|
||||||
|
try {
|
||||||
|
let course = await Course.find({ _id });
|
||||||
|
if (!title) {
|
||||||
|
res.status(422).json({ message: "Please, Add Title of the Blog" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!body) {
|
||||||
|
res.status(422).json({ message: "Please, Add Body of the Blog" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (course) {
|
||||||
|
let blog = new Blog({
|
||||||
|
title,
|
||||||
|
body,
|
||||||
|
image,
|
||||||
|
date: new Date(),
|
||||||
|
});
|
||||||
|
await blog.save();
|
||||||
|
res.json({ message: "Blog Saved Successfully!" });
|
||||||
|
} else {
|
||||||
|
res.status.json({ error: "User Doesn't Exists" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
(err) => {
|
||||||
|
res.status(422).json({ error: err });
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.deleteBlog = async (req, res, next) => {
|
||||||
|
const { _id } = req.body;
|
||||||
|
try {
|
||||||
|
let blog = await Blog.find({ _id });
|
||||||
|
if (blog) {
|
||||||
|
await Blog.findByIdAndDelete(_id);
|
||||||
|
res.json({ message: "Blog Deleted!" });
|
||||||
|
} else {
|
||||||
|
res.status(404).json({ message: "Blog Not Found" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
(err) => {
|
||||||
|
res.status(422).json({ error: err });
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.editBlog = async (req, res, next) => {
|
||||||
|
const { _id, body, title, image } = req.body;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!title) {
|
||||||
|
res.status(422).json({ message: "Please, Add the Title of the Blog." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!body) {
|
||||||
|
res.status(422).json({ message: "Please, Add the Body of the Blog." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!image) {
|
||||||
|
res.status(422).json({ message: "Please, Add the Image of the Blog." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let blog = Blog.findById(_id);
|
||||||
|
if (blog) {
|
||||||
|
blog.title = title;
|
||||||
|
blog.body = body;
|
||||||
|
blog.image = image;
|
||||||
|
await blog.save();
|
||||||
|
res.json({ message: "Blog Updated!" });
|
||||||
|
} else {
|
||||||
|
res.status(422).json({ error: "Blog Doesn't Found" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
(err) => {
|
||||||
|
res.status(422).json({ error: err });
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
|
@ -1,64 +1,52 @@
|
||||||
const Coupon = require("../models/Coupon");
|
const Coupon = require("../models/Coupon");
|
||||||
|
|
||||||
module.exports.getAllCoupons = async (req , res , next) => {
|
module.exports.getAllCoupons = async (req, res, next) => {
|
||||||
try
|
try {
|
||||||
{
|
const coupons = await Coupon.find();
|
||||||
const coupons = await Coupon.find() ;
|
console.log(coupons);
|
||||||
console.log(coupons) ;
|
|
||||||
res.json({
|
res.json({
|
||||||
coupons : coupons
|
coupons: coupons,
|
||||||
}) ;
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
}
|
}
|
||||||
catch(err)
|
};
|
||||||
{
|
|
||||||
console.log(err) ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports.addCoupon = async (req , res , next) => {
|
module.exports.addCoupon = async (req, res, next) => {
|
||||||
try
|
try {
|
||||||
{
|
const couponCode = req.body.couponCode;
|
||||||
const couponCode = req.body.couponCode ;
|
const percentage = req.body.percentage;
|
||||||
const percentage = req.body.percentage ;
|
const numAllowed = req.body.numAllowed;
|
||||||
const numAllowed = req.body.numAllowed ;
|
|
||||||
|
|
||||||
let coupon = await Coupon.findOne({couponCode : couponCode}) ;
|
let coupon = await Coupon.findOne({ couponCode: couponCode });
|
||||||
if(coupon)
|
if (coupon) {
|
||||||
{
|
|
||||||
res.json({
|
res.json({
|
||||||
error:"coupon already Exist"
|
error: "Coupon already Exist",
|
||||||
})
|
});
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
let coupon = new Coupon({
|
let coupon = new Coupon({
|
||||||
couponCode : couponCode ,
|
couponCode: couponCode,
|
||||||
percentage : percentage ,
|
percentage: percentage,
|
||||||
numAllowed : numAllowed
|
numAllowed: numAllowed,
|
||||||
}) ;
|
});
|
||||||
coupon = await coupon.save() ;
|
coupon = await coupon.save();
|
||||||
res.json({
|
res.json({
|
||||||
message: "Created Successfully"
|
message: "Created Successfully",
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
} catch (err) {
|
||||||
catch(err)
|
|
||||||
{
|
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
module.exports.deleteCoupon = async (req , res, next) => {
|
module.exports.deleteCoupon = async (req, res, next) => {
|
||||||
try
|
try {
|
||||||
{
|
const couponCode = req.body.couponCode;
|
||||||
const couponCode = req.body.couponCode ;
|
await Coupon.deleteOne({ couponCode: couponCode });
|
||||||
await Coupon.deleteOne({couponCode : couponCode}) ;
|
|
||||||
res.json({
|
res.json({
|
||||||
message: "Deleted Successfully"
|
message: "Deleted Successfully",
|
||||||
})
|
});
|
||||||
}
|
} catch (err) {
|
||||||
catch(err)
|
|
||||||
{
|
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
const mongoose = require("mongoose");
|
||||||
|
const blogSchema = new mongoose.Schema({
|
||||||
|
title: {
|
||||||
|
type: String,
|
||||||
|
require: true,
|
||||||
|
},
|
||||||
|
author: {
|
||||||
|
type: String,
|
||||||
|
ref: "Course",
|
||||||
|
},
|
||||||
|
date: {
|
||||||
|
type: Date,
|
||||||
|
},
|
||||||
|
image: {
|
||||||
|
type: String,
|
||||||
|
default: "",
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
isBookMarked: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
module.exports = mongoose.model("Blog", blogSchema);
|
|
@ -0,0 +1,14 @@
|
||||||
|
const express = require("express");
|
||||||
|
const router = express.Router();
|
||||||
|
const BlogController = require("../controllers/blog");
|
||||||
|
const isAuth = require("../middleware/requirelogin");
|
||||||
|
|
||||||
|
router.post("/add-blog", BlogController.addBlog);
|
||||||
|
|
||||||
|
router.get("/get-all-blogs", BlogController.getAllBlogs);
|
||||||
|
|
||||||
|
router.post("/delete-blog/", isAuth, BlogController.deleteBlog);
|
||||||
|
|
||||||
|
router.post("/edit-blog/", isAuth, BlogController.editBlog);
|
||||||
|
|
||||||
|
module.exports = router;
|
Loading…
Reference in New Issue