Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Feedback system to the Blog #18

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
12 changes: 6 additions & 6 deletions models/blog.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const mongoose = require("mongoose");

var blogSchema = mongoose.Schema({
rating: {
type: Number,default :0,
},
name: {
type: String,
},
initialNode: {
type: mongoose.Schema.Types.ObjectId,
ref: "Node",
},
author: {
type: String,
},
rating: {
type: Number,
},



nodes: [
{
type: mongoose.Schema.Types.ObjectId,
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 20 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
{
"name": "csechack",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"serve": "nodemon server.js",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"async": "^2.6.1",
"ejs": "^2.6.1",
"express": "^4.16.4",
"mongoose": "^5.7.5",
"nodemon": "^1.18.9"
}
"name": "csechack",
"version": "1.0.0",
"description": "**ABOUT** our blog-'iBlog'",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"serve": "nodemon server.js",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"async": "^2.6.1",
"ejs": "^2.6.1",
"express": "^4.16.4",
"express-validator": "^6.6.1",
"mongoose": "^5.7.5",
"nodemon": "^1.18.9"
},
"devDependencies": {}
}
70 changes: 68 additions & 2 deletions routes/reading.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
var express = require("express");
var router = express.Router();

const bodyParser=require("body-parser");
var Blog = require("../models/blog.js");
var Node = require("../models/node.js");


const { body, validationResult } = require('express-validator');
router.use(
bodyParser.urlencoded({
extended: true,
})
);


Blog.find().sort({"rating" : -1});




router.get("/read/:id", function (req, res) {

var nodesArr = [];
Blog.findOne({ _id: req.params.id }).then((blog) => {
blog.nodes.forEach((blogNode, index) => {
Expand All @@ -29,7 +44,12 @@ router.get("/read/:id", function (req, res) {
});

router.get("/", function (req, res) {
Blog.find({}, function (err, blogs) {

Blog.find().sort({"rating" : -1});



Blog.find().sort({"rating" : -1}).find({}, function (err, blogs) {
var nodesArr = [];
blogs.forEach((blog, index) => {
Node.findOne({ _id: blog.initialNode })
Expand All @@ -55,10 +75,56 @@ router.get("/", function (req, res) {
});
});

router.get("/feedback/:id",function(req,res)
{
res.render("test",{id:req.params.id});
});

router.get("/load/:id", function (req, res) {
Node.findOne({ _id: req.params.id }).then((node) => {
res.send(node);
});
});

router.post("/new/:id", [

body('rating').isNumeric({ min: 0,max :5})
], function(req,res)
{
const errors=validationResult(req);
console.log(errors);
if (!errors.isEmpty()) {

res.send("Invalid Rating");
console.log(errors);
// console.log(req.body.nodes[0].content);

}
else
{
var num1=Number(req.body.rating);

let id=req.param.id;

Blog.findByIdAndUpdate(
req.params.id,
{$inc: {rating:num1 }},

{upsert:false},

function(err, document){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can write a simple console.log here cause otherwise there's no purpose of an error handling function.


});


res.redirect("/");

}
});





module.exports = router;
43 changes: 41 additions & 2 deletions routes/writing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var express = require("express");
var router = express.Router();
var bodyParser = require("body-parser");
const { body, validationResult } = require('express-validator');

var mongoose = require("mongoose");
var async = require("async");

Expand All @@ -10,6 +12,7 @@ var Blog = require("../models/blog");
router.use(bodyParser.urlencoded({ extended: false }));
router.use(bodyParser.json());


router.get("/", function (req, res) {
res.render("index");
});
Expand Down Expand Up @@ -42,8 +45,31 @@ router.get("/viewAllBlogs", function (req, res) {
});
});

router.post("/post", function (req, res) {
var reqBlog = req.body.blog;
router.post("/post",[

body('blog.name')
.trim().not().isEmpty(),
body('nodes.*.content')
.trim().not().isEmpty()


], function (req, res) {

const errors=validationResult(req);
console.log(errors);
if (!errors.isEmpty()) {

res.send(500);
Copy link
Member

@vaibhavrajsingh2001 vaibhavrajsingh2001 Aug 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errors are sent with a message in this format:

return res.status(400).json({ msg: 'Empty request' });

console.log(errors);
// console.log(req.body.nodes[0].content);

}
else{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use try and catch blocks for error handling btw. This helps in sending error responses for all sorts of errors, instead of sending individual response for particular errors.




var reqBlog = req.body.blog;

var nodes = [];
var newBlog = new Blog({
name: reqBlog.name,
Expand Down Expand Up @@ -81,6 +107,8 @@ router.post("/post", function (req, res) {
res.send(200);
})
.catch((err) => {});
}

});

router.post("/edit/:id", function (req, res) {
Expand All @@ -105,6 +133,17 @@ router.post("/edit/:id", function (req, res) {
});
});


router.get("/posterror", function (req, res) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't write a separate route just for errors. Errors should be written for each individual route.

res.send('<h1>Error:Invalid Data Sent</h1>');
});

router.get("/postStory", function (req, res) {
res.render("postStory");
});



router.get("/edit/:id", function (req, res) {
var nodesArr = [];
// console.log(req.params.id);
Expand Down
19 changes: 8 additions & 11 deletions views/postStory.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,31 @@

<script>
var data = [];

var addBlockDiv = '<div class="node">' +
'<textarea class="content" style="resize: none" rows="4" cols="50" placeholder="Content here...">' +
'</textarea> <br />' +
'<input class="end" type="checkbox" checked> Story Ends?</div><br />';

var questionDiv = '<div class="question">' +
'<button class="addOption">Add Option</button><br />' +
'<input type="text" placeholder="Question Content.." class="question_content">' +
'<br /><input type="text" placeholder="Option.." class="options">' +
'</div>'

$('document').ready(function() {
addBlock();
});

$(document).on('click', 'input[type=checkbox]', function() {
$(this).parent().append(questionDiv);
});

$(document).on('click', '.addOption', function() {
var optionInput = '<br /><input type="text" placeholder="Option.." class="options">';
$(this).parent().append(optionInput);
});

$(document).on('click', 'input[type=checkbox]:checked', function() {
$(this).parent().children(".question").empty();
});

function addBlock() {
$('#storyBoard').append(addBlockDiv);
}

function submitStory() {
var arr = [], arObj = {}, blog={};
blog.name = $('#title').val();
Expand All @@ -60,20 +52,25 @@
obj.options.push($(option).val());
});
}

arr.push(obj);
arObj.nodes = arr;
arObj.blog = blog;

});

});
$.ajax({
url: "/writing/post",
type: "post",
data: arObj,
success: function() {
window.location.replace('/writing/viewAllBlogs');
},
error: function() {
window.location.replace('/writing/posterror');
}




});
}
</script>
Expand Down
14 changes: 11 additions & 3 deletions views/readStory.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<% include ./partials/header %>

<h1><%=blog.name%></h1>
<div id="contentDiv">
<p id="content"><%=nodesArr[0].content %></p>
Expand All @@ -10,6 +11,7 @@
<button class="opt" onclick="loadStory('<%= nodesArr[0].question.options[j].mapping %>')"><%= nodesArr[0].question.options[j].key %></button>
<% } %>
</div>

</div>


Expand All @@ -18,7 +20,7 @@
function renderNewData(data) {
// console.log(data.content);
$('#content').html(($('#content').html() + "<br />" + data.content));

// console.log(typeof data.end);


Expand All @@ -29,7 +31,8 @@
$('#options').append(`<button class="opt" onclick="loadStory('${data.question.options[i].mapping}')">${data.question.options[i].key}</button>`);
}
} else {
$('#question').html(`<center><button>Thank You!</button></center>`);
$('#question').html('<center><button type="button" onclick="pageRedirect()">Feedback</button></center>');

}
}

Expand All @@ -44,5 +47,10 @@

}
</script>
<script>
function pageRedirect() {
window.location.replace("/reading/feedback/<%=blog.id%>/");
}
</script>

<% include ./partials/footer %>
<% include ./partials/footer %>
Loading