diff --git a/models/blog.js b/models/blog.js index 3378f11..d9960a3 100644 --- a/models/blog.js +++ b/models/blog.js @@ -1,6 +1,9 @@ const mongoose = require("mongoose"); var blogSchema = mongoose.Schema({ + rating: { + type: Number,default :0, + }, name: { type: String, }, @@ -8,12 +11,9 @@ var blogSchema = mongoose.Schema({ type: mongoose.Schema.Types.ObjectId, ref: "Node", }, - author: { - type: String, - }, - rating: { - type: Number, - }, + + + nodes: [ { type: mongoose.Schema.Types.ObjectId, diff --git a/package-lock.json b/package-lock.json index db41ddf..c8b9d71 100644 --- a/package-lock.json +++ b/package-lock.json @@ -623,6 +623,15 @@ "vary": "~1.1.2" } }, + "express-validator": { + "version": "6.6.1", + "resolved": "https://registry.npmjs.org/express-validator/-/express-validator-6.6.1.tgz", + "integrity": "sha512-+MrZKJ3eGYXkNF9p9Zf7MS7NkPJFg9MDYATU5c80Cf4F62JdLBIjWxy6481tRC0y1NnC9cgOw8FuN364bWaGhA==", + "requires": { + "lodash": "^4.17.19", + "validator": "^13.1.1" + } + }, "extend-shallow": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend-shallow/-/extend-shallow-3.0.2.tgz", @@ -2655,6 +2664,11 @@ "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" }, + "validator": { + "version": "13.1.1", + "resolved": "https://registry.npmjs.org/validator/-/validator-13.1.1.tgz", + "integrity": "sha512-8GfPiwzzRoWTg7OV1zva1KvrSemuMkv07MA9TTl91hfhe+wKrsrgVN4H2QSFd/U/FhiU3iWPYVgvbsOGwhyFWw==" + }, "vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", diff --git a/package.json b/package.json index 517283f..84c2744 100644 --- a/package.json +++ b/package.json @@ -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": {} } diff --git a/routes/reading.js b/routes/reading.js index 6b7108b..fdc2fa2 100644 --- a/routes/reading.js +++ b/routes/reading.js @@ -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) => { @@ -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 }) @@ -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){ + + +}); + + + res.redirect("/"); + +} + }); + + + + + module.exports = router; diff --git a/routes/writing.js b/routes/writing.js index 7ef94ff..a20d037 100644 --- a/routes/writing.js +++ b/routes/writing.js @@ -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"); @@ -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"); }); @@ -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); + console.log(errors); + // console.log(req.body.nodes[0].content); + + } +else{ + + + + var reqBlog = req.body.blog; + var nodes = []; var newBlog = new Blog({ name: reqBlog.name, @@ -81,6 +107,8 @@ router.post("/post", function (req, res) { res.send(200); }) .catch((err) => {}); +} + }); router.post("/edit/:id", function (req, res) { @@ -105,6 +133,17 @@ router.post("/edit/:id", function (req, res) { }); }); + +router.get("/posterror", function (req, res) { + res.send('

Error:Invalid Data Sent

'); +}); + +router.get("/postStory", function (req, res) { + res.render("postStory"); +}); + + + router.get("/edit/:id", function (req, res) { var nodesArr = []; // console.log(req.params.id); diff --git a/views/postStory.ejs b/views/postStory.ejs index 2b428cb..9354de4 100644 --- a/views/postStory.ejs +++ b/views/postStory.ejs @@ -10,39 +10,31 @@ diff --git a/views/readStory.ejs b/views/readStory.ejs index 584aef3..8d6e4c8 100644 --- a/views/readStory.ejs +++ b/views/readStory.ejs @@ -1,4 +1,5 @@ <% include ./partials/header %> +

<%=blog.name%>

<%=nodesArr[0].content %>

@@ -10,6 +11,7 @@ <% } %>
+ @@ -18,7 +20,7 @@ function renderNewData(data) { // console.log(data.content); $('#content').html(($('#content').html() + "
" + data.content)); - + // console.log(typeof data.end); @@ -29,7 +31,8 @@ $('#options').append(``); } } else { - $('#question').html(`
`); +$('#question').html('
'); + } } @@ -44,5 +47,10 @@ } + -<% include ./partials/footer %> \ No newline at end of file +<% include ./partials/footer %> diff --git a/views/test.ejs b/views/test.ejs index 5ab2f8a..f4c846e 100644 --- a/views/test.ejs +++ b/views/test.ejs @@ -1 +1,90 @@ -Hello \ No newline at end of file + + + + + + + + + + + + +

Rate whatever you want

+
+ + + + + + +
+ + + + + + +
+ + +
+ + + +