diff --git a/P03_StackOverflow'sMiniature/P03_Megha/index.js b/P03_StackOverflow'sMiniature/P03_Megha/index.js index 67ff29af..5cce9987 100644 --- a/P03_StackOverflow'sMiniature/P03_Megha/index.js +++ b/P03_StackOverflow'sMiniature/P03_Megha/index.js @@ -3,6 +3,7 @@ const mongoose = require("mongoose"); const bodyparser = require("body-parser"); const passport = require("passport"); const User = require("./models/User"); +const Question = require("./models/Question"); const db = require("./mysetup/myurl").myurl; const bcrypt = require("bcrypt"); const jsonwt = require("jsonwebtoken"); @@ -29,7 +30,7 @@ app.use(passport.initialize()); require("./strategies/jsonwtStrategy")(passport); app.get("/", (req, res) => { - res.status(200).send(`Hi Welcome to the Login and Signup API`); + res.json({ message: "pong" }); }); app.post("/api/auth/register", async (req, res) => { @@ -87,13 +88,120 @@ app.get( "/api/auth/profile", passport.authenticate("jwt", { session: false }), (req, res) => { - res.json({ - id: req.user.id, - email: req.user.email + User.findById(req.user.id) + .lean() + .then(user => { + delete user.password; + res.json(user); + }); + } +); +//get(public route) route to get all the Questions. +app.get( + "/api/questions", + passport.authenticate("jwt", { session: false }), + (req, res) => { + Question.find() + .sort({ date: "desc" }) + .lean() + .then(questions => + res.json( + questions.map(q => { + const question = { ...q }; + delete question.answers; + return question; + }) + ) + ) + .catch(err => res.json({ noquestions: "No Questions" })); + } +); + +//route(private route) to post the Question. +app.post( + "/api/questions", + passport.authenticate("jwt", { session: false }), + (req, res) => { + const newQuestion = new Question({ + text: req.body.text, + user: req.user.id, + name: req.body.name }); + newQuestion + .save() + .then(question => res.json(question)) + .catch(err => console.log("unable to push question")); + } +); + +app.get( + "/api/questions/:id", + passport.authenticate("jwt", { session: false }), + (req, res, next) => { + Question.findById(req.params["id"]) + .lean() + .then(question => { + delete question.answers; + res.json(question); + }) + .catch(err => next(err)); } ); +//public route to get all the Answers +app.get( + "/api/questions/:id/answers", + passport.authenticate("jwt", { session: false }), + (req, res, next) => { + Question.findById(req.params["id"]) + .lean() + .then(question => { + res.json(question.answers); + }) + .catch(err => next(err)); + } +); + +//private route to post the Answer only for the existing Questions. +app.post( + "/api/questions/:id/answers", + passport.authenticate("jwt", { session: false }), + (req, res) => { + Question.findById(req.params.id) + .then(question => { + const newAnswer = { + user: req.user.id, + name: req.body.name, + text: req.body.text + }; + question.answers.unshift(newAnswer); + return question.save(); + }) + .then(question => res.json(question)) + .catch(err => console.log(err)); + } +); + +app.post("/api/questions/:id/answers/:answer_id/upvote", (req, res, next) => { + const answerId = parseInt(req.params["answer_id"]); + Question.findById(req.params.id) + .then(question => { + console.log(question); + question.answers[answerId].votes++; + return question.save(); + }) + .then(question => res.json(question.answers[answerId])) + .catch(next); +}); + +app.use((error, req, res, next) => { + console.log("Server encountered error", error); + res.json({ + error, + message: error.message || error || "Not found" + }); +}); + app.listen(port, () => { console.log(`Server is listening on port ${port}`); }); diff --git a/P03_StackOverflow'sMiniature/P03_Megha/models/Question.js b/P03_StackOverflow'sMiniature/P03_Megha/models/Question.js new file mode 100644 index 00000000..c403c33f --- /dev/null +++ b/P03_StackOverflow'sMiniature/P03_Megha/models/Question.js @@ -0,0 +1,36 @@ +const mongoose = require("mongoose"); +const Schema = mongoose.Schema; + +const QuestionSchema = new Schema({ + name: { + type: String, + required: true + }, + text: { + type: String, + required: true + }, + user: { + type: Schema.Types.ObjectId, + ref: "User" + }, + + answers: [ + { + name: { + type: String, + required: true + }, + text: { + type: String, + required: true + }, + votes: { + type: Number, + default: 0, + required: true + } + } + ] +}); +module.exports = Question = mongoose.model("Question", QuestionSchema); diff --git a/P03_StackOverflow'sMiniature/P03_Megha/package.json b/P03_StackOverflow'sMiniature/P03_Megha/package.json index d0959a37..7f484ef8 100644 --- a/P03_StackOverflow'sMiniature/P03_Megha/package.json +++ b/P03_StackOverflow'sMiniature/P03_Megha/package.json @@ -4,9 +4,10 @@ "description": "", "main": "index.js", "scripts": { + "start": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, - "author": "", + "author": "Megha Priyadarshini ", "license": "ISC", "dependencies": { "bcrypt": "^3.0.6",