From 08f0d3a45d11b9e5fb7b550fa9627e3f50060b38 Mon Sep 17 00:00:00 2001 From: yashrajverma Date: Wed, 19 May 2021 15:48:45 +0530 Subject: [PATCH] Fixed --- .history/app_20210519122521.js | 112 ------ .history/app_20210519122841.js | 107 ----- .history/controllers/auth_20210519122521.js | 419 -------------------- .history/controllers/auth_20210519122752.js | 404 ------------------- .history/controllers/auth_20210519123339.js | 405 ------------------- .history/controllers/blog_20210519114159.js | 103 +++++ .history/controllers/blog_20210519145955.js | 104 +++++ .history/controllers/blog_20210519150616.js | 104 +++++ .history/controllers/blog_20210519151427.js | 104 +++++ .history/controllers/blog_20210519151454.js | 104 +++++ .history/controllers/blog_20210519151725.js | 106 +++++ .history/controllers/blog_20210519151735.js | 106 +++++ .history/controllers/blog_20210519151822.js | 106 +++++ .history/controllers/blog_20210519151942.js | 104 +++++ .history/controllers/blog_20210519152021.js | 104 +++++ .history/controllers/blog_20210519152037.js | 104 +++++ .history/controllers/blog_20210519152116.js | 104 +++++ .history/controllers/blog_20210519152132.js | 104 +++++ .history/controllers/blog_20210519152207.js | 104 +++++ .history/controllers/blog_20210519152224.js | 104 +++++ .history/controllers/blog_20210519152240.js | 104 +++++ .history/controllers/blog_20210519152310.js | 104 +++++ .history/controllers/blog_20210519152325.js | 104 +++++ .history/controllers/blog_20210519152415.js | 104 +++++ .history/controllers/blog_20210519152419.js | 104 +++++ .history/controllers/blog_20210519152443.js | 104 +++++ .history/controllers/blog_20210519152758.js | 104 +++++ .history/controllers/blog_20210519152820.js | 105 +++++ .history/controllers/blog_20210519152920.js | 105 +++++ .history/controllers/blog_20210519153031.js | 105 +++++ .history/controllers/blog_20210519153142.js | 105 +++++ .history/controllers/blog_20210519153454.js | 105 +++++ .history/controllers/blog_20210519153459.js | 105 +++++ .history/controllers/blog_20210519153836.js | 105 +++++ .history/controllers/blog_20210519153855.js | 105 +++++ .history/controllers/blog_20210519153953.js | 106 +++++ .history/controllers/blog_20210519154014.js | 106 +++++ .history/controllers/blog_20210519154059.js | 106 +++++ .history/controllers/blog_20210519154126.js | 105 +++++ .history/controllers/blog_20210519154454.js | 106 +++++ .history/controllers/blog_20210519154456.js | 106 +++++ .history/controllers/blog_20210519154625.js | 106 +++++ .history/controllers/blog_20210519154733.js | 106 +++++ .history/controllers/blog_20210519154825.js | 104 +++++ .history/models/Blog_20210519103433.js | 27 ++ .history/models/Blog_20210519150611.js | 28 ++ .history/models/Blog_20210519150818.js | 25 ++ .history/models/Blog_20210519152942.js | 25 ++ .history/models/Blog_20210519153800.js | 25 ++ .history/routes/blog_20210519114750.js | 14 + .history/routes/blog_20210519151054.js | 14 + controllers/blog.js | 7 +- models/Blog.js | 6 +- routes/blog.js | 4 +- 54 files changed, 4250 insertions(+), 1456 deletions(-) delete mode 100644 .history/app_20210519122521.js delete mode 100644 .history/app_20210519122841.js delete mode 100644 .history/controllers/auth_20210519122521.js delete mode 100644 .history/controllers/auth_20210519122752.js delete mode 100644 .history/controllers/auth_20210519123339.js create mode 100644 .history/controllers/blog_20210519114159.js create mode 100644 .history/controllers/blog_20210519145955.js create mode 100644 .history/controllers/blog_20210519150616.js create mode 100644 .history/controllers/blog_20210519151427.js create mode 100644 .history/controllers/blog_20210519151454.js create mode 100644 .history/controllers/blog_20210519151725.js create mode 100644 .history/controllers/blog_20210519151735.js create mode 100644 .history/controllers/blog_20210519151822.js create mode 100644 .history/controllers/blog_20210519151942.js create mode 100644 .history/controllers/blog_20210519152021.js create mode 100644 .history/controllers/blog_20210519152037.js create mode 100644 .history/controllers/blog_20210519152116.js create mode 100644 .history/controllers/blog_20210519152132.js create mode 100644 .history/controllers/blog_20210519152207.js create mode 100644 .history/controllers/blog_20210519152224.js create mode 100644 .history/controllers/blog_20210519152240.js create mode 100644 .history/controllers/blog_20210519152310.js create mode 100644 .history/controllers/blog_20210519152325.js create mode 100644 .history/controllers/blog_20210519152415.js create mode 100644 .history/controllers/blog_20210519152419.js create mode 100644 .history/controllers/blog_20210519152443.js create mode 100644 .history/controllers/blog_20210519152758.js create mode 100644 .history/controllers/blog_20210519152820.js create mode 100644 .history/controllers/blog_20210519152920.js create mode 100644 .history/controllers/blog_20210519153031.js create mode 100644 .history/controllers/blog_20210519153142.js create mode 100644 .history/controllers/blog_20210519153454.js create mode 100644 .history/controllers/blog_20210519153459.js create mode 100644 .history/controllers/blog_20210519153836.js create mode 100644 .history/controllers/blog_20210519153855.js create mode 100644 .history/controllers/blog_20210519153953.js create mode 100644 .history/controllers/blog_20210519154014.js create mode 100644 .history/controllers/blog_20210519154059.js create mode 100644 .history/controllers/blog_20210519154126.js create mode 100644 .history/controllers/blog_20210519154454.js create mode 100644 .history/controllers/blog_20210519154456.js create mode 100644 .history/controllers/blog_20210519154625.js create mode 100644 .history/controllers/blog_20210519154733.js create mode 100644 .history/controllers/blog_20210519154825.js create mode 100644 .history/models/Blog_20210519103433.js create mode 100644 .history/models/Blog_20210519150611.js create mode 100644 .history/models/Blog_20210519150818.js create mode 100644 .history/models/Blog_20210519152942.js create mode 100644 .history/models/Blog_20210519153800.js create mode 100644 .history/routes/blog_20210519114750.js create mode 100644 .history/routes/blog_20210519151054.js diff --git a/.history/app_20210519122521.js b/.history/app_20210519122521.js deleted file mode 100644 index 2f07095..0000000 --- a/.history/app_20210519122521.js +++ /dev/null @@ -1,112 +0,0 @@ -const express = require("express"); -const mongoose = require("mongoose"); -const bodyparser = require("body-parser"); -const authRoute = require("./routes/auth"); -const profileRoute = require("./routes/profile"); -const paymentRoute = require("./routes/payment"); -const courseRoute = require("./routes/course"); -const couponRoute = require("./routes/Coupon"); -const queryRoute = require("./routes/query"); -const adminRoute = require("./routes/admin"); -const blogRouter = require("./routes/blog"); -const port = process.env.PORT || 5000; -const cors = require("cors"); -const app = express(); - -//const passport = require('passport'); -//const cookieSession = require('cookie-session') ; -//require('./passport-setup') ; - -<<<<<<< HEAD -======= -const app = express(); - ->>>>>>> ef4c9b6a526e0ef10b94f271654809ce636d8ab0 -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(bodyparser.json()); - -// app.use(cookieSession({ -// name: 'test-session', -// keys: ['key1', 'key2'] -// })) - -// const isLoggedIn = (req , res , next) => { -// if(req.user) -// { -// next() -// } -// else -// { -// res.json({ -// error : "No user" -// }) -// } -// } - -// app.use(passport.initialize()); -// app.use(passport.session()); - -// app.get('/failed' , (req , res) => { -// res.json({ -// error : "You have failed to login" -// }) -// }) - -// app.get('/' , (req , res) => { -// console.log('you are not logged in'); - -// }) - -// app.get('/good' ,isLoggedIn , (req , res) => { -// res.json({ -// message:"success" , -// user : req.user -// }) -// }) - -// app.get('/auth/google', -// passport.authenticate('google', { scope: ['profile' , 'email'] })); - -// app.get('/auth/google/callback', -// passport.authenticate('google', { failureRedirect: '/failed' }), -// function(req, res) { -// res.redirect('/good'); -// }); - -// app.get('/logout', (req , res) => { -// req.session = null ; -// req.logout() ; -// res.redirect('/') ; -// }) -mongoose - .connect(MONGO_URI, { - useNewUrlParser: true, - useUnifiedTopology: true, - useFindAndModify: false, - }) - .then((result) => { - console.log("connected"); - app.listen(port, () => { - console.log("server is running on port", port); - }); - }) - .catch((err) => { - console.log(err); - }); -app.use(authRoute); - -app.use(profileRoute); - -app.use(paymentRoute); - -app.use(courseRoute); - -app.use(adminRoute); - -app.use(couponRoute); - -app.use(blogRouter); - -app.use(queryRoute); diff --git a/.history/app_20210519122841.js b/.history/app_20210519122841.js deleted file mode 100644 index b02365d..0000000 --- a/.history/app_20210519122841.js +++ /dev/null @@ -1,107 +0,0 @@ -const express = require("express"); -const mongoose = require("mongoose"); -const bodyparser = require("body-parser"); -const authRoute = require("./routes/auth"); -const profileRoute = require("./routes/profile"); -const paymentRoute = require("./routes/payment"); -const courseRoute = require("./routes/course"); -const couponRoute = require("./routes/Coupon"); -const queryRoute = require("./routes/query"); -const adminRoute = require("./routes/admin"); -const blogRouter = require("./routes/blog"); -const port = process.env.PORT || 5000; -const cors = require("cors"); -const app = express(); - -//const passport = require('passport'); -//const cookieSession = require('cookie-session') ; -//require('./passport-setup') ; - -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(bodyparser.json()); - -// app.use(cookieSession({ -// name: 'test-session', -// keys: ['key1', 'key2'] -// })) - -// const isLoggedIn = (req , res , next) => { -// if(req.user) -// { -// next() -// } -// else -// { -// res.json({ -// error : "No user" -// }) -// } -// } - -// app.use(passport.initialize()); -// app.use(passport.session()); - -// app.get('/failed' , (req , res) => { -// res.json({ -// error : "You have failed to login" -// }) -// }) - -// app.get('/' , (req , res) => { -// console.log('you are not logged in'); - -// }) - -// app.get('/good' ,isLoggedIn , (req , res) => { -// res.json({ -// message:"success" , -// user : req.user -// }) -// }) - -// app.get('/auth/google', -// passport.authenticate('google', { scope: ['profile' , 'email'] })); - -// app.get('/auth/google/callback', -// passport.authenticate('google', { failureRedirect: '/failed' }), -// function(req, res) { -// res.redirect('/good'); -// }); - -// app.get('/logout', (req , res) => { -// req.session = null ; -// req.logout() ; -// res.redirect('/') ; -// }) -mongoose - .connect(MONGO_URI, { - useNewUrlParser: true, - useUnifiedTopology: true, - useFindAndModify: false, - }) - .then((result) => { - console.log("connected"); - app.listen(port, () => { - console.log("server is running on port", port); - }); - }) - .catch((err) => { - console.log(err); - }); -app.use(authRoute); - -app.use(profileRoute); - -app.use(paymentRoute); - -app.use(courseRoute); - -app.use(adminRoute); - -app.use(couponRoute); - -app.use(blogRouter); - -app.use(queryRoute); diff --git a/.history/controllers/auth_20210519122521.js b/.history/controllers/auth_20210519122521.js deleted file mode 100644 index f9fd292..0000000 --- a/.history/controllers/auth_20210519122521.js +++ /dev/null @@ -1,419 +0,0 @@ -const bcrypt = require("bcryptjs"); -const User = require("../models/User"); -const Student = require("../models/Student"); -const jwt = require("jsonwebtoken"); -const JWT_secret = "Cantileverlabs"; -const messagebird = require("messagebird")("llVKD53ve6QRpbCKOHzWBADaS", null, [ - "ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX", -]); -const nodemailer = require("nodemailer"); -const smtpTransport = require("nodemailer-smtp-transport"); - -// -------------------------------------------- mail transporter ----------------------------------------- - -var transport = nodemailer.createTransport( - smtpTransport({ - host: `email-smtp.us-east-1.amazonaws.com`, //`${process.env.HOST}` - port: 465, - auth: { - user: `AKIA2G7743RRTZMVXE3X`, //`${process.env.EMAIL}` - pass: `BJSjV3jArJfsnk1LhFc/hUmisEyEtbLNGgrRbv0noh8c`, //`${process.env.PASS}` - }, - }) -); - -// -------------------------------------------- mail transporter ----------------------------------------- - -module.exports.Protected = async (req, res, next) => { - res.send("Hello User"); -}; -module.exports.postSignup = async (req, res, next) => { - try { - //we need firstName , lastName , email , password as input - let firstName = req.body.firstName || " "; - let lastName = req.body.lastName || " "; - const { sending_company_email, email, password, subject } = req.body; - let user = await User.findOne({ email: email }); - if (user) { - res.json({ - message: "User already exist", - type: "error", - }); - } else { - const email_otp = Math.floor(100000 + Math.random() * 900000); - console.log("otp", email_otp); - const hashedPass = await bcrypt.hash(password, 12); - user = new User({ - firstName: firstName, - lastName: lastName, - email: email, - password: hashedPass, - isAdmin: false, - // email_otp, - }); - user = await user.save(); - await Student.deleteOne({ user: user._id }); - let student = new Student({ - user: user._id, - }); - student = await student.save(); - user.student = student._id; - await user.save(); - // const message = { - // from: `${sending_company_email}`, // Sender address - // to: `${email}`, // List of recipients - // subject: `${subject}`, // Subject line - // html: '', // design html for email message. - // }; - // transport.sendMail(message, function (err, info) { - // if (err) { - // console.log(err); - // } else { - // console.log(info); - // } - // }); - res.json({ - message: "You Are Registered, Please Login", - type: "success", - }); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.verfiyemail = async (req, res, next) => { - const { email, otp } = req.body; - try { - let user = await User.findOne({ email: email }); - if (user) { - const isMatched = await (user.email_otp == otp ? true : false); - if (isMatched) { - if (!user.isVerified) { - user.isVerified = true; - await user.save(); - res.json({ - message: "User Verified, Please Login", - }); - } else { - res.json({ - message: "User Already Verified, Please Login", - }); - } - } else { - res.json({ - message: "OTP Doesn't Matched!", - type: "error", - }); - } - } else { - res.json({ - message: "No user with this email exists", - type: "error", - }); - } - } catch { - (err) => { - console.log(err); - }; - } -}; - -module.exports.postSignin = async (req, res, next) => { - try { - //we need email and password as input - let email = req.body.email; - let password = req.body.password; - let user = await User.findOne({ email: email }); - if (user) { - const isMatched = await bcrypt.compare(password, user.password); - if (isMatched) { - const token = jwt.sign({ _id: user._id }, JWT_secret); - res.json({ - token: token, - }); - } else { - res.json({ - message: "email and password doesn't match", - type: "error", - }); - } - } else { - res.json({ - message: "No user with this email exists", - type: "error", - }); - } - } catch (err) { - console.log(err); - } -}; - -// Phone verification Starts. -// ----------------------------------------------------------------------------------------------- - -module.exports.sendOTP = (req, res, next) => { - //uNNYosMopvvCW9RTR1tRWJmYC test - //llVKD53ve6QRpbCKOHzWBADaS live - const { phoneNumber } = req.body; - try { - if (!phoneNumber) { - res.status(422).json({ message: "Please Add All Required Fields" }); - return; - } else { - messagebird.verify.create( - phoneNumber, - { - template: "Your verification code is %token", - }, - function (err, response) { - if (err) { - console.log(err); - res.status(422).json({ message: err.errors[0].description }); - } else { - console.log(response); - res.json({ id: response.id }); - } - } - ); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.getOTP = (req, res, next) => { - try { - const { id, otp } = req.body; - messagebird.verify.verify(id, otp, function (err, response) { - if (err) { - console.log({ error: err.errors[0].description, id: id }); - res.json({ error: err.errors[0].description, id: id }); - } else { - console.log(response); - res.json({ message: "Code Verified" }); - } - }); - } catch (err) { - console.log(err); - } -}; -// Phone verification End. -// ----------------------------------------------------------------------------------------------- - -// ----------------------------------------------------------------------------------------------- -<<<<<<< HEAD -// Forgot password Starts - -module.exports.forgotpassword = async (req, res, next) => { - const { email, link, sending_company_email, subject } = req.body; -======= -// Email verification Starts - -var transport = nodemailer.createTransport({ - service: "gmail", - auth: { - user: "5578544cc56856", //replace it with the companies mail - pass: "a510d3d969d3b3", //replace it with the companies pass - }, -}); -module.exports.forgotpassword = async (req, res, next) => { - const { email, link, _html, sending_company_email, subject } = req.body; ->>>>>>> ef4c9b6a526e0ef10b94f271654809ce636d8ab0 - //link = https://cantileverlabs.herokuapp.com/resetpassword/:id/:token - try { - await User.findOne({ email }).then((user) => { - if (!user) { - res.status(404).json({ error: "User not found with this Email" }); - return; - } else { - const payload = { - email: user.email, - _id: user._id, - }; - const secret = JWT_secret + user.password; - const token = jwt.sign(payload, secret, { expiresIn: "10m" }); - User.findByIdAndUpdate(user._id, { - $set: { passwordResetToken: token }, - }) - .then((data) => { - const reset_link = `${link}/${user._id}/${token}`; - const message = { - from: `${sending_company_email}`, // Sender address - to: `${user.email}`, // List of recipients - subject: `${subject}`, // Subject line - html: ` - - - - - - Reset Password Email Template - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - -
 
- -
 
- - - - - - - - - - -
 
- - logo -

You have - requested to reset your password

- -

- We cannot simply send you your old password. A unique link to reset your - password has been generated for you. To reset your password, click the - following link and follow the instructions. -

- Reset - Password - -

- Facing any issue? Write us at -

info@cantileverlabs.com

-logo -

- Privacy Policy -Terms Of Use -Contact Us -

-

-
 
-
 
-
- - copyright 2018 Cantilever Labs -
-
 
-
- - - - - `, // design html for email message. - }; - transport.sendMail(message, function (err, info) { - if (err) { - console.log(err); - } else { - console.log(info); - } - }); - res.status(200).json({ - message: "Link is Active for 10 mins", - reset_link, - }); - }) - .catch((err) => { - console.log(err); - }); - } - }); - } catch { - (error) => { - console.log("Error from forgot pass", error); - }; - } -}; -module.exports.resetpassword = async (req, res, next) => { - const { _id, token } = req.params; - const { password } = req.body; - try { - let user = await User.findById({ _id }); - if (!user) { - res.json({ error: "User not Found or WrongId" }); - return; - } else { - const secret = JWT_secret + user.password; - const user_token = user.passwordResetToken; - const payload = jwt.verify(token, secret); - const hashedPass = await bcrypt.hash(password, 12); - if (token == user_token) { - user.password = hashedPass; - await user - .save() - .then((ok) => { - res.header("Access-Control-Allow-Origin", "*"); - res.header("Access-Control-Allow-Headers", "*"); - res.json({ message: "Password Updated!" }); - }) - .catch((err) => { - console.log("Error in save", err); - }); - } else { - res.status(422).json({ error: "Either Token not found or Expired!" }); - return; - } - } - } catch { - (err) => { - console.log("error from try catch resetpass", err); - }; - } -}; - -<<<<<<< HEAD -// Forgot password Ends -======= -// Email verification Ends ->>>>>>> ef4c9b6a526e0ef10b94f271654809ce636d8ab0 -// ----------------------------------------------------------------------------------------------- - -module.exports.checkProtected = (req, res, next) => { - console.log(req.user); - res.json({ - message: "Protected", - user: req.user, - }); -}; diff --git a/.history/controllers/auth_20210519122752.js b/.history/controllers/auth_20210519122752.js deleted file mode 100644 index e8e42c9..0000000 --- a/.history/controllers/auth_20210519122752.js +++ /dev/null @@ -1,404 +0,0 @@ -const bcrypt = require("bcryptjs"); -const User = require("../models/User"); -const Student = require("../models/Student"); -const jwt = require("jsonwebtoken"); -const JWT_secret = "Cantileverlabs"; -const messagebird = require("messagebird")("llVKD53ve6QRpbCKOHzWBADaS", null, [ - "ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX", -]); -const nodemailer = require("nodemailer"); -const smtpTransport = require("nodemailer-smtp-transport"); - -// -------------------------------------------- mail transporter ----------------------------------------- - -var transport = nodemailer.createTransport( - smtpTransport({ - host: `email-smtp.us-east-1.amazonaws.com`, //`${process.env.HOST}` - port: 465, - auth: { - user: `AKIA2G7743RRTZMVXE3X`, //`${process.env.EMAIL}` - pass: `BJSjV3jArJfsnk1LhFc/hUmisEyEtbLNGgrRbv0noh8c`, //`${process.env.PASS}` - }, - }) -); - -// -------------------------------------------- mail transporter ----------------------------------------- - -module.exports.Protected = async (req, res, next) => { - res.send("Hello User"); -}; -module.exports.postSignup = async (req, res, next) => { - try { - //we need firstName , lastName , email , password as input - let firstName = req.body.firstName || " "; - let lastName = req.body.lastName || " "; - const { sending_company_email, email, password, subject } = req.body; - let user = await User.findOne({ email: email }); - if (user) { - res.json({ - message: "User already exist", - type: "error", - }); - } else { - const email_otp = Math.floor(100000 + Math.random() * 900000); - console.log("otp", email_otp); - const hashedPass = await bcrypt.hash(password, 12); - user = new User({ - firstName: firstName, - lastName: lastName, - email: email, - password: hashedPass, - isAdmin: false, - // email_otp, - }); - user = await user.save(); - await Student.deleteOne({ user: user._id }); - let student = new Student({ - user: user._id, - }); - student = await student.save(); - user.student = student._id; - await user.save(); - // const message = { - // from: `${sending_company_email}`, // Sender address - // to: `${email}`, // List of recipients - // subject: `${subject}`, // Subject line - // html: '', // design html for email message. - // }; - // transport.sendMail(message, function (err, info) { - // if (err) { - // console.log(err); - // } else { - // console.log(info); - // } - // }); - res.json({ - message: "You Are Registered, Please Login", - type: "success", - }); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.verfiyemail = async (req, res, next) => { - const { email, otp } = req.body; - try { - let user = await User.findOne({ email: email }); - if (user) { - const isMatched = await (user.email_otp == otp ? true : false); - if (isMatched) { - if (!user.isVerified) { - user.isVerified = true; - await user.save(); - res.json({ - message: "User Verified, Please Login", - }); - } else { - res.json({ - message: "User Already Verified, Please Login", - }); - } - } else { - res.json({ - message: "OTP Doesn't Matched!", - type: "error", - }); - } - } else { - res.json({ - message: "No user with this email exists", - type: "error", - }); - } - } catch { - (err) => { - console.log(err); - }; - } -}; - -module.exports.postSignin = async (req, res, next) => { - try { - //we need email and password as input - let email = req.body.email; - let password = req.body.password; - let user = await User.findOne({ email: email }); - if (user) { - const isMatched = await bcrypt.compare(password, user.password); - if (isMatched) { - const token = jwt.sign({ _id: user._id }, JWT_secret); - res.json({ - token: token, - }); - } else { - res.json({ - message: "email and password doesn't match", - type: "error", - }); - } - } else { - res.json({ - message: "No user with this email exists", - type: "error", - }); - } - } catch (err) { - console.log(err); - } -}; - -// Phone verification Starts. -// ----------------------------------------------------------------------------------------------- - -module.exports.sendOTP = (req, res, next) => { - //uNNYosMopvvCW9RTR1tRWJmYC test - //llVKD53ve6QRpbCKOHzWBADaS live - const { phoneNumber } = req.body; - try { - if (!phoneNumber) { - res.status(422).json({ message: "Please Add All Required Fields" }); - return; - } else { - messagebird.verify.create( - phoneNumber, - { - template: "Your verification code is %token", - }, - function (err, response) { - if (err) { - console.log(err); - res.status(422).json({ message: err.errors[0].description }); - } else { - console.log(response); - res.json({ id: response.id }); - } - } - ); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.getOTP = (req, res, next) => { - try { - const { id, otp } = req.body; - messagebird.verify.verify(id, otp, function (err, response) { - if (err) { - console.log({ error: err.errors[0].description, id: id }); - res.json({ error: err.errors[0].description, id: id }); - } else { - console.log(response); - res.json({ message: "Code Verified" }); - } - }); - } catch (err) { - console.log(err); - } -}; -// Phone verification End. -// ----------------------------------------------------------------------------------------------- - -// ----------------------------------------------------------------------------------------------- -// Forgot password Starts - -module.exports.forgotpassword = async (req, res, next) => { - const { email, link, sending_company_email, subject } = req.body; - //link = https://cantileverlabs.herokuapp.com/resetpassword/:id/:token - try { - await User.findOne({ email }).then((user) => { - if (!user) { - res.status(404).json({ error: "User not found with this Email" }); - return; - } else { - const payload = { - email: user.email, - _id: user._id, - }; - const secret = JWT_secret + user.password; - const token = jwt.sign(payload, secret, { expiresIn: "10m" }); - User.findByIdAndUpdate(user._id, { - $set: { passwordResetToken: token }, - }) - .then((data) => { - const reset_link = `${link}/${user._id}/${token}`; - const message = { - from: `${sending_company_email}`, // Sender address - to: `${user.email}`, // List of recipients - subject: `${subject}`, // Subject line - html: ` - - - - - - Reset Password Email Template - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - -
 
- -
 
- - - - - - - - - - -
 
- - logo -

You have - requested to reset your password

- -

- We cannot simply send you your old password. A unique link to reset your - password has been generated for you. To reset your password, click the - following link and follow the instructions. -

- Reset - Password - -

- Facing any issue? Write us at -

info@cantileverlabs.com

-logo -

- Privacy Policy -Terms Of Use -Contact Us -

-

-
 
-
 
-
- - copyright 2018 Cantilever Labs -
-
 
-
- - - - - `, // design html for email message. - }; - transport.sendMail(message, function (err, info) { - if (err) { - console.log(err); - } else { - console.log(info); - } - }); - res.status(200).json({ - message: "Link is Active for 10 mins", - reset_link, - }); - }) - .catch((err) => { - console.log(err); - }); - } - }); - } catch { - (error) => { - console.log("Error from forgot pass", error); - }; - } -}; -module.exports.resetpassword = async (req, res, next) => { - const { _id, token } = req.params; - const { password } = req.body; - try { - let user = await User.findById({ _id }); - if (!user) { - res.json({ error: "User not Found or WrongId" }); - return; - } else { - const secret = JWT_secret + user.password; - const user_token = user.passwordResetToken; - const payload = jwt.verify(token, secret); - const hashedPass = await bcrypt.hash(password, 12); - if (token == user_token) { - user.password = hashedPass; - await user - .save() - .then((ok) => { - res.header("Access-Control-Allow-Origin", "*"); - res.header("Access-Control-Allow-Headers", "*"); - res.json({ message: "Password Updated!" }); - }) - .catch((err) => { - console.log("Error in save", err); - }); - } else { - res.status(422).json({ error: "Either Token not found or Expired!" }); - return; - } - } - } catch { - (err) => { - console.log("error from try catch resetpass", err); - }; - } -}; - -// Forgot password Ends - -// Email verification Ends - -// ----------------------------------------------------------------------------------------------- - -module.exports.checkProtected = (req, res, next) => { - console.log(req.user); - res.json({ - message: "Protected", - user: req.user, - }); -}; diff --git a/.history/controllers/auth_20210519123339.js b/.history/controllers/auth_20210519123339.js deleted file mode 100644 index 675a180..0000000 --- a/.history/controllers/auth_20210519123339.js +++ /dev/null @@ -1,405 +0,0 @@ -const bcrypt = require("bcryptjs"); -const User = require("../models/User"); -const Student = require("../models/Student"); -const jwt = require("jsonwebtoken"); -const JWT_secret = "Cantileverlabs"; -const messagebird = require("messagebird")("llVKD53ve6QRpbCKOHzWBADaS", null, [ - "ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX", -]); -const nodemailer = require("nodemailer"); -const smtpTransport = require("nodemailer-smtp-transport"); - -// -------------------------------------------- mail transporter ----------------------------------------- - -var transport = nodemailer.createTransport( - smtpTransport({ - host: `${process.env.HOST}`, //`${process.env.HOST}` - port: 465, - auth: { - user: `${process.env.EMAIL}`, //`${process.env.EMAIL}` - pass: `${process.env.PASS}`, //`${process.env.PASS}` - }, - }) -); - -// -------------------------------------------- mail transporter ----------------------------------------- - -module.exports.Protected = async (req, res, next) => { - res.send("Hello User"); -}; -module.exports.postSignup = async (req, res, next) => { - try { - //we need firstName , lastName , email , password as input - let firstName = req.body.firstName || " "; - let lastName = req.body.lastName || " "; - const { sending_company_email, email, password, subject } = req.body; - let user = await User.findOne({ email: email }); - if (user) { - res.json({ - message: "User already exist", - type: "error", - }); - } else { - const email_otp = Math.floor(100000 + Math.random() * 900000); - console.log("otp", email_otp); - const hashedPass = await bcrypt.hash(password, 12); - user = new User({ - firstName: firstName, - lastName: lastName, - email: email, - password: hashedPass, - isAdmin: false, - // email_otp, - }); - user = await user.save(); - await Student.deleteOne({ user: user._id }); - let student = new Student({ - user: user._id, - }); - student = await student.save(); - user.student = student._id; - await user.save(); - // const message = { - // from: `${sending_company_email}`, // Sender address - // to: `${email}`, // List of recipients - // subject: `${subject}`, // Subject line - // html: '', // design html for email message. - // }; - // transport.sendMail(message, function (err, info) { - // if (err) { - // console.log(err); - // } else { - // console.log(info); - // } - // }); - res.json({ - message: "You Are Registered, Please Login", - type: "success", - }); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.verfiyemail = async (req, res, next) => { - const { email, otp } = req.body; - try { - let user = await User.findOne({ email: email }); - if (user) { - const isMatched = await (user.email_otp == otp ? true : false); - if (isMatched) { - if (!user.isVerified) { - user.isVerified = true; - await user.save(); - res.json({ - message: "User Verified, Please Login", - }); - } else { - res.json({ - message: "User Already Verified, Please Login", - }); - } - } else { - res.json({ - message: "OTP Doesn't Matched!", - type: "error", - }); - } - } else { - res.json({ - message: "No user with this email exists", - type: "error", - }); - } - } catch { - (err) => { - console.log(err); - }; - } -}; - -module.exports.postSignin = async (req, res, next) => { - try { - //we need email and password as input - let email = req.body.email; - let password = req.body.password; - let user = await User.findOne({ email: email }); - if (user) { - const isMatched = await bcrypt.compare(password, user.password); - if (isMatched) { - const token = jwt.sign({ _id: user._id }, JWT_secret); - res.json({ - token: token, - }); - } else { - res.json({ - message: "email and password doesn't match", - type: "error", - }); - } - } else { - res.json({ - message: "No user with this email exists", - type: "error", - }); - } - } catch (err) { - console.log(err); - } -}; - -// Phone verification Starts. -// ----------------------------------------------------------------------------------------------- - -module.exports.sendOTP = (req, res, next) => { - //uNNYosMopvvCW9RTR1tRWJmYC test - //llVKD53ve6QRpbCKOHzWBADaS live - const { phoneNumber } = req.body; - try { - if (!phoneNumber) { - res.status(422).json({ message: "Please Add All Required Fields" }); - return; - } else { - messagebird.verify.create( - phoneNumber, - { - template: "Your verification code is %token", - }, - function (err, response) { - if (err) { - console.log(err); - res.status(422).json({ message: err.errors[0].description }); - } else { - console.log(response); - res.json({ id: response.id }); - } - } - ); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.getOTP = (req, res, next) => { - try { - const { id, otp } = req.body; - messagebird.verify.verify(id, otp, function (err, response) { - if (err) { - console.log({ error: err.errors[0].description, id: id }); - res.json({ error: err.errors[0].description, id: id }); - } else { - console.log(response); - res.json({ message: "Code Verified" }); - } - }); - } catch (err) { - console.log(err); - } -}; -// Phone verification End. -// ----------------------------------------------------------------------------------------------- - -// ----------------------------------------------------------------------------------------------- -// Forgot password Starts - -module.exports.forgotpassword = async (req, res, next) => { - const { email, link, sending_company_email, subject } = req.body; - //link = https://cantileverlabs.herokuapp.com/resetpassword/:id/:token - try { - await User.findOne({ email }).then((user) => { - if (!user) { - res.status(404).json({ error: "User not found with this Email" }); - return; - } else { - const payload = { - email: user.email, - _id: user._id, - }; - const secret = JWT_secret + user.password; - const token = jwt.sign(payload, secret, { expiresIn: "10m" }); - User.findByIdAndUpdate(user._id, { - $set: { passwordResetToken: token }, - }) - .then((data) => { - const reset_link = `${link}/${user._id}/${token}`; - const message = { - from: `${sending_company_email}`, // Sender address - to: `${user.email}`, // List of recipients - subject: `${subject}`, // Subject line - html: ` - - - - - - Reset Password Email Template - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - -
 
- -
 
- - - - - - - - - - -
 
- - logo -

You have - requested to reset your password

- -

- We cannot simply send you your old password. A unique link to reset your - password has been generated for you. To reset your password, click the - following link and follow the instructions. -

- Reset - Password - -

- Facing any issue? Write us at -

info@cantileverlabs.com

-logo - -

- Privacy Policy -Terms Of Use -Contact Us -

-

-
 
-
 
-
- - copyright 2018 Cantilever Labs -
-
 
-
- - - - - `, // design html for email message. - }; - transport.sendMail(message, function (err, info) { - if (err) { - console.log(err); - } else { - console.log(info); - } - }); - res.status(200).json({ - message: "Link is Active for 10 mins", - reset_link, - }); - }) - .catch((err) => { - console.log(err); - }); - } - }); - } catch { - (error) => { - console.log("Error from forgot pass", error); - }; - } -}; -module.exports.resetpassword = async (req, res, next) => { - const { _id, token } = req.params; - const { password } = req.body; - try { - let user = await User.findById({ _id }); - if (!user) { - res.json({ error: "User not Found or WrongId" }); - return; - } else { - const secret = JWT_secret + user.password; - const user_token = user.passwordResetToken; - const payload = jwt.verify(token, secret); - const hashedPass = await bcrypt.hash(password, 12); - if (token == user_token) { - user.password = hashedPass; - await user - .save() - .then((ok) => { - res.header("Access-Control-Allow-Origin", "*"); - res.header("Access-Control-Allow-Headers", "*"); - res.json({ message: "Password Updated!" }); - }) - .catch((err) => { - console.log("Error in save", err); - }); - } else { - res.status(422).json({ error: "Either Token not found or Expired!" }); - return; - } - } - } catch { - (err) => { - console.log("error from try catch resetpass", err); - }; - } -}; - -// Forgot password Ends - -// Email verification Ends - -// ----------------------------------------------------------------------------------------------- - -module.exports.checkProtected = (req, res, next) => { - console.log(req.user); - res.json({ - message: "Protected", - user: req.user, - }); -}; diff --git a/.history/controllers/blog_20210519114159.js b/.history/controllers/blog_20210519114159.js new file mode 100644 index 0000000..f6ea6c1 --- /dev/null +++ b/.history/controllers/blog_20210519114159.js @@ -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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519145955.js b/.history/controllers/blog_20210519145955.js new file mode 100644 index 0000000..16d1963 --- /dev/null +++ b/.history/controllers/blog_20210519145955.js @@ -0,0 +1,104 @@ +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(), + author: course._id, + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519150616.js b/.history/controllers/blog_20210519150616.js new file mode 100644 index 0000000..16d1963 --- /dev/null +++ b/.history/controllers/blog_20210519150616.js @@ -0,0 +1,104 @@ +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(), + author: course._id, + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519151427.js b/.history/controllers/blog_20210519151427.js new file mode 100644 index 0000000..b77e01b --- /dev/null +++ b/.history/controllers/blog_20210519151427.js @@ -0,0 +1,104 @@ +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(), + author: course, + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519151454.js b/.history/controllers/blog_20210519151454.js new file mode 100644 index 0000000..16d1963 --- /dev/null +++ b/.history/controllers/blog_20210519151454.js @@ -0,0 +1,104 @@ +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(), + author: course._id, + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519151725.js b/.history/controllers/blog_20210519151725.js new file mode 100644 index 0000000..41e17ac --- /dev/null +++ b/.history/controllers/blog_20210519151725.js @@ -0,0 +1,106 @@ +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 }) + .populate("_id ") + .select("-password"); + 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(), + author: course._id, + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519151735.js b/.history/controllers/blog_20210519151735.js new file mode 100644 index 0000000..bb6bf06 --- /dev/null +++ b/.history/controllers/blog_20210519151735.js @@ -0,0 +1,106 @@ +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 }) + .populate("_id ") + .select("-password"); + 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(), + author: course, + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519151822.js b/.history/controllers/blog_20210519151822.js new file mode 100644 index 0000000..41e17ac --- /dev/null +++ b/.history/controllers/blog_20210519151822.js @@ -0,0 +1,106 @@ +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 }) + .populate("_id ") + .select("-password"); + 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(), + author: course._id, + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519151942.js b/.history/controllers/blog_20210519151942.js new file mode 100644 index 0000000..6e3a814 --- /dev/null +++ b/.history/controllers/blog_20210519151942.js @@ -0,0 +1,104 @@ +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 }).populate("").select("-password"); + 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(), + author: course, + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152021.js b/.history/controllers/blog_20210519152021.js new file mode 100644 index 0000000..f3614e1 --- /dev/null +++ b/.history/controllers/blog_20210519152021.js @@ -0,0 +1,104 @@ +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(), + author: , + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152037.js b/.history/controllers/blog_20210519152037.js new file mode 100644 index 0000000..ecb3602 --- /dev/null +++ b/.history/controllers/blog_20210519152037.js @@ -0,0 +1,104 @@ +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 }); + console.log(course); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152116.js b/.history/controllers/blog_20210519152116.js new file mode 100644 index 0000000..e57c2c9 --- /dev/null +++ b/.history/controllers/blog_20210519152116.js @@ -0,0 +1,104 @@ +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); + console.log(course); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152132.js b/.history/controllers/blog_20210519152132.js new file mode 100644 index 0000000..ecb3602 --- /dev/null +++ b/.history/controllers/blog_20210519152132.js @@ -0,0 +1,104 @@ +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 }); + console.log(course); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152207.js b/.history/controllers/blog_20210519152207.js new file mode 100644 index 0000000..8d93c09 --- /dev/null +++ b/.history/controllers/blog_20210519152207.js @@ -0,0 +1,104 @@ +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.findById({ _id }); + console.log(course); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152224.js b/.history/controllers/blog_20210519152224.js new file mode 100644 index 0000000..eb2bed9 --- /dev/null +++ b/.history/controllers/blog_20210519152224.js @@ -0,0 +1,104 @@ +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.findById({ _id }); + console.log("Course ", course); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152240.js b/.history/controllers/blog_20210519152240.js new file mode 100644 index 0000000..3fcbb8d --- /dev/null +++ b/.history/controllers/blog_20210519152240.js @@ -0,0 +1,104 @@ +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.findById(_id); + console.log("Course ", course); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152310.js b/.history/controllers/blog_20210519152310.js new file mode 100644 index 0000000..16ff14d --- /dev/null +++ b/.history/controllers/blog_20210519152310.js @@ -0,0 +1,104 @@ +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); + console.log("Course ", course); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152325.js b/.history/controllers/blog_20210519152325.js new file mode 100644 index 0000000..1fe749e --- /dev/null +++ b/.history/controllers/blog_20210519152325.js @@ -0,0 +1,104 @@ +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 }); + console.log("Course ", course); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152415.js b/.history/controllers/blog_20210519152415.js new file mode 100644 index 0000000..72b3d1c --- /dev/null +++ b/.history/controllers/blog_20210519152415.js @@ -0,0 +1,104 @@ +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.findOne({ _id }); + console.log("Course ", course); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152419.js b/.history/controllers/blog_20210519152419.js new file mode 100644 index 0000000..72b3d1c --- /dev/null +++ b/.history/controllers/blog_20210519152419.js @@ -0,0 +1,104 @@ +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.findOne({ _id }); + console.log("Course ", course); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152443.js b/.history/controllers/blog_20210519152443.js new file mode 100644 index 0000000..1fe749e --- /dev/null +++ b/.history/controllers/blog_20210519152443.js @@ -0,0 +1,104 @@ +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 }); + console.log("Course ", course); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152758.js b/.history/controllers/blog_20210519152758.js new file mode 100644 index 0000000..2f29cf6 --- /dev/null +++ b/.history/controllers/blog_20210519152758.js @@ -0,0 +1,104 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + console.log("User ", user); + 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 (user) { + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152820.js b/.history/controllers/blog_20210519152820.js new file mode 100644 index 0000000..b0cfe9a --- /dev/null +++ b/.history/controllers/blog_20210519152820.js @@ -0,0 +1,105 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + console.log("User ", user); + 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 (user) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: user, + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519152920.js b/.history/controllers/blog_20210519152920.js new file mode 100644 index 0000000..596af29 --- /dev/null +++ b/.history/controllers/blog_20210519152920.js @@ -0,0 +1,105 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + console.log("User ", user); + 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 (user) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: user.firstName + " " + user.lastName, + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519153031.js b/.history/controllers/blog_20210519153031.js new file mode 100644 index 0000000..a0f4d7d --- /dev/null +++ b/.history/controllers/blog_20210519153031.js @@ -0,0 +1,105 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + console.log("User ", user); + 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 (user._id == _id) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: user.firstName + " " + user.lastName, + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519153142.js b/.history/controllers/blog_20210519153142.js new file mode 100644 index 0000000..1b4efdc --- /dev/null +++ b/.history/controllers/blog_20210519153142.js @@ -0,0 +1,105 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + console.log("User ", user); + 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 (user._id == _id) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: user._id, + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519153454.js b/.history/controllers/blog_20210519153454.js new file mode 100644 index 0000000..b8c7f06 --- /dev/null +++ b/.history/controllers/blog_20210519153454.js @@ -0,0 +1,105 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + console.log("User ", user); + 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 (user._id == _id) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: user, + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519153459.js b/.history/controllers/blog_20210519153459.js new file mode 100644 index 0000000..13ac015 --- /dev/null +++ b/.history/controllers/blog_20210519153459.js @@ -0,0 +1,105 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + // console.log("User ", user); + 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 (user._id == _id) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: user, + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519153836.js b/.history/controllers/blog_20210519153836.js new file mode 100644 index 0000000..fa3e179 --- /dev/null +++ b/.history/controllers/blog_20210519153836.js @@ -0,0 +1,105 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + // console.log("User ", user); + 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 (user._id == _id) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: User.find({ _id }), + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519153855.js b/.history/controllers/blog_20210519153855.js new file mode 100644 index 0000000..1eb7526 --- /dev/null +++ b/.history/controllers/blog_20210519153855.js @@ -0,0 +1,105 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + // console.log("User ", user); + 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 (user._id == _id) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: , + }); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519153953.js b/.history/controllers/blog_20210519153953.js new file mode 100644 index 0000000..ae60f82 --- /dev/null +++ b/.history/controllers/blog_20210519153953.js @@ -0,0 +1,106 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + // console.log("User ", user); + 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 (user._id == _id) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: user.firstName, + }); + console.log("Brfore save", blog); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519154014.js b/.history/controllers/blog_20210519154014.js new file mode 100644 index 0000000..98a24d4 --- /dev/null +++ b/.history/controllers/blog_20210519154014.js @@ -0,0 +1,106 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + // console.log("User ", user); + 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 (user) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: user.firstName, + }); + console.log("Brfore save", blog); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519154059.js b/.history/controllers/blog_20210519154059.js new file mode 100644 index 0000000..98274a1 --- /dev/null +++ b/.history/controllers/blog_20210519154059.js @@ -0,0 +1,106 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + // console.log("User ", user); + 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 (user) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: user, + }); + console.log("Brfore save", blog); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519154126.js b/.history/controllers/blog_20210519154126.js new file mode 100644 index 0000000..b7d1f2b --- /dev/null +++ b/.history/controllers/blog_20210519154126.js @@ -0,0 +1,105 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + // console.log("User ", user); + 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 (user) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + }); + console.log("Brfore save", blog); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519154454.js b/.history/controllers/blog_20210519154454.js new file mode 100644 index 0000000..959a2a9 --- /dev/null +++ b/.history/controllers/blog_20210519154454.js @@ -0,0 +1,106 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + // console.log("User ", user); + 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 (user) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author:user. + }); + console.log("Before save", blog); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519154456.js b/.history/controllers/blog_20210519154456.js new file mode 100644 index 0000000..7ff2191 --- /dev/null +++ b/.history/controllers/blog_20210519154456.js @@ -0,0 +1,106 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + // console.log("User ", user); + 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 (user) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: user, + }); + console.log("Before save", blog); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519154625.js b/.history/controllers/blog_20210519154625.js new file mode 100644 index 0000000..b18be73 --- /dev/null +++ b/.history/controllers/blog_20210519154625.js @@ -0,0 +1,106 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.find({ _id }); + console.log("User ", user._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 (user) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: user, + }); + // console.log("Before save", blog); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519154733.js b/.history/controllers/blog_20210519154733.js new file mode 100644 index 0000000..7150d52 --- /dev/null +++ b/.history/controllers/blog_20210519154733.js @@ -0,0 +1,106 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.findById({ _id }); + console.log("User ", user._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 (user) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: user, + }); + // console.log("Before save", blog); + 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 }); + }; + } +}; diff --git a/.history/controllers/blog_20210519154825.js b/.history/controllers/blog_20210519154825.js new file mode 100644 index 0000000..b7c474e --- /dev/null +++ b/.history/controllers/blog_20210519154825.js @@ -0,0 +1,104 @@ +const { log } = require("handlebars"); +const Blog = require("../models/Blog"); +const User = require("../models/User"); + +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 user = await User.findById({ _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 (user) { + let blog = new Blog({ + title, + body, + image, + date: new Date(), + author: user, + }); + 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 }); + }; + } +}; diff --git a/.history/models/Blog_20210519103433.js b/.history/models/Blog_20210519103433.js new file mode 100644 index 0000000..33e6c55 --- /dev/null +++ b/.history/models/Blog_20210519103433.js @@ -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); diff --git a/.history/models/Blog_20210519150611.js b/.history/models/Blog_20210519150611.js new file mode 100644 index 0000000..ffc56dd --- /dev/null +++ b/.history/models/Blog_20210519150611.js @@ -0,0 +1,28 @@ +const mongoose = require("mongoose"); +const { ObjectId } = mongoose.Schema.Types; +const blogSchema = new mongoose.Schema({ + title: { + type: String, + require: true, + }, + author: { + type: ObjectId, + 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); diff --git a/.history/models/Blog_20210519150818.js b/.history/models/Blog_20210519150818.js new file mode 100644 index 0000000..6ad1746 --- /dev/null +++ b/.history/models/Blog_20210519150818.js @@ -0,0 +1,25 @@ +const mongoose = require("mongoose"); +const { ObjectId } = mongoose.Schema.Types; +const blogSchema = new mongoose.Schema({ + title: { + type: String, + require: true, + }, + author: { ref: "Course", type: ObjectId }, + date: { + type: Date, + }, + image: { + type: String, + default: "", + }, + body: { + type: String, + required: true, + }, + isBookMarked: { + type: Boolean, + default: false, + }, +}); +module.exports = mongoose.model("Blog", blogSchema); diff --git a/.history/models/Blog_20210519152942.js b/.history/models/Blog_20210519152942.js new file mode 100644 index 0000000..a4bd0fc --- /dev/null +++ b/.history/models/Blog_20210519152942.js @@ -0,0 +1,25 @@ +const mongoose = require("mongoose"); +const { ObjectId } = mongoose.Schema.Types; +const blogSchema = new mongoose.Schema({ + title: { + type: String, + require: true, + }, + author: { ref: "User", type: ObjectId }, + date: { + type: Date, + }, + image: { + type: String, + default: "", + }, + body: { + type: String, + required: true, + }, + isBookMarked: { + type: Boolean, + default: false, + }, +}); +module.exports = mongoose.model("Blog", blogSchema); diff --git a/.history/models/Blog_20210519153800.js b/.history/models/Blog_20210519153800.js new file mode 100644 index 0000000..774666e --- /dev/null +++ b/.history/models/Blog_20210519153800.js @@ -0,0 +1,25 @@ +const mongoose = require("mongoose"); + +const blogSchema = new mongoose.Schema({ + title: { + type: String, + require: true, + }, + author: { ref: "User", type: mongoose.Schema.Types.ObjectId }, + date: { + type: Date, + }, + image: { + type: String, + default: "", + }, + body: { + type: String, + required: true, + }, + isBookMarked: { + type: Boolean, + default: false, + }, +}); +module.exports = mongoose.model("Blog", blogSchema); diff --git a/.history/routes/blog_20210519114750.js b/.history/routes/blog_20210519114750.js new file mode 100644 index 0000000..51e5d57 --- /dev/null +++ b/.history/routes/blog_20210519114750.js @@ -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; diff --git a/.history/routes/blog_20210519151054.js b/.history/routes/blog_20210519151054.js new file mode 100644 index 0000000..9336f60 --- /dev/null +++ b/.history/routes/blog_20210519151054.js @@ -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/", BlogController.deleteBlog); + +router.post("/edit-blog/", BlogController.editBlog); + +module.exports = router; diff --git a/controllers/blog.js b/controllers/blog.js index f6ea6c1..b7c474e 100644 --- a/controllers/blog.js +++ b/controllers/blog.js @@ -1,6 +1,6 @@ const { log } = require("handlebars"); const Blog = require("../models/Blog"); -const Course = require("../models/Course"); +const User = require("../models/User"); module.exports.getAllBlogs = async (req, res, next) => { try { @@ -21,7 +21,7 @@ module.exports.getAllBlogs = async (req, res, next) => { 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 }); + let user = await User.findById({ _id }); if (!title) { res.status(422).json({ message: "Please, Add Title of the Blog" }); return; @@ -30,12 +30,13 @@ module.exports.addBlog = async (req, res, next) => { res.status(422).json({ message: "Please, Add Body of the Blog" }); return; } - if (course) { + if (user) { let blog = new Blog({ title, body, image, date: new Date(), + author: user, }); await blog.save(); res.json({ message: "Blog Saved Successfully!" }); diff --git a/models/Blog.js b/models/Blog.js index 33e6c55..774666e 100644 --- a/models/Blog.js +++ b/models/Blog.js @@ -1,13 +1,11 @@ const mongoose = require("mongoose"); + const blogSchema = new mongoose.Schema({ title: { type: String, require: true, }, - author: { - type: String, - ref: "Course", - }, + author: { ref: "User", type: mongoose.Schema.Types.ObjectId }, date: { type: Date, }, diff --git a/routes/blog.js b/routes/blog.js index 51e5d57..9336f60 100644 --- a/routes/blog.js +++ b/routes/blog.js @@ -7,8 +7,8 @@ router.post("/add-blog", BlogController.addBlog); router.get("/get-all-blogs", BlogController.getAllBlogs); -router.post("/delete-blog/", isAuth, BlogController.deleteBlog); +router.post("/delete-blog/", BlogController.deleteBlog); -router.post("/edit-blog/", isAuth, BlogController.editBlog); +router.post("/edit-blog/", BlogController.editBlog); module.exports = router;