Merge pull request #6 from Cantilever-Labs/commentsection
comment section
This commit is contained in:
commit
026b8cde45
3
app.js
3
app.js
|
@ -13,6 +13,7 @@ const blogRouter = require("./routes/blog");
|
|||
const port = process.env.PORT || 5000;
|
||||
const cors = require("cors");
|
||||
|
||||
|
||||
//const passport = require('passport');
|
||||
//const cookieSession = require('cookie-session') ;
|
||||
//require('./passport-setup') ;
|
||||
|
@ -75,7 +76,7 @@ app.use(bodyparser.json());
|
|||
// req.logout() ;
|
||||
// res.redirect('/') ;
|
||||
// })
|
||||
|
||||
const url = "mongodb://127.0.0.1:27017/game-of-dice";
|
||||
mongoose.connect(MONGO_URI, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
|
|
|
@ -119,3 +119,56 @@ module.exports.editBlog = async (req, res, next) => {
|
|||
};
|
||||
}
|
||||
};
|
||||
module.exports.commentBlog = async (req, res, next) => {
|
||||
const comment = req.body.comment;
|
||||
const today = new Date();
|
||||
var dd = today.getDate();
|
||||
var mm = today.getMonth() + 1;
|
||||
|
||||
var yyyy = today.getFullYear();
|
||||
if (dd < 10) {
|
||||
dd = "0" + dd;
|
||||
}
|
||||
if (mm < 10) {
|
||||
mm = "0" + mm;
|
||||
}
|
||||
var today = dd + "/" + mm + "/" + yyyy;
|
||||
|
||||
const comments1 = {
|
||||
user: req.user._id,
|
||||
review: comment,
|
||||
date: datenow,
|
||||
};
|
||||
|
||||
try {
|
||||
if (comment) {
|
||||
Blog.findByIdAndUpdate(
|
||||
{ _id: req.params.id },
|
||||
{
|
||||
$push: {
|
||||
comments: comments1,
|
||||
},
|
||||
},
|
||||
function (err, docs) {
|
||||
if (err) {
|
||||
res.status(503).json({
|
||||
message: "internal server error cant post the comment",
|
||||
err,
|
||||
});
|
||||
} else {
|
||||
res.status(201).json({
|
||||
message: "Comment posted!",
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
if (err) {
|
||||
res.status(501).json({
|
||||
message: "internal Server error please try again after some time",
|
||||
err,
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -21,5 +21,16 @@ const blogSchema = new mongoose.Schema({
|
|||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
comments: [
|
||||
{
|
||||
user: { type: mongoose.Schema.Types.ObjectId, ref: "Users" },
|
||||
review: {
|
||||
type: String,
|
||||
},
|
||||
date: {
|
||||
type: Date,
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
module.exports = mongoose.model("Blog", blogSchema);
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
"start": "node app.js",
|
||||
"test": "mocha ||true"
|
||||
},
|
||||
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
|
|
|
@ -39,4 +39,6 @@ router.post("/deleteBlog/", BlogController.deleteBlog);
|
|||
|
||||
router.post("/editBlog/", BlogController.editBlog);
|
||||
|
||||
router.post("/commentBlog/:id",BlogController.commentBlog)
|
||||
|
||||
module.exports = router;
|
||||
|
|
Loading…
Reference in New Issue