Skip to content

Commit

Permalink
Merge pull request #35 from mpriyadarshini/master
Browse files Browse the repository at this point in the history
Day 5
  • Loading branch information
HrithikMittal authored Jul 6, 2019
2 parents 7af19e4 + 3bc3708 commit 800d2bd
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 5 deletions.
116 changes: 112 additions & 4 deletions P03_StackOverflow'sMiniature/P03_Megha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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) => {
Expand Down Expand Up @@ -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}`);
});
36 changes: 36 additions & 0 deletions P03_StackOverflow'sMiniature/P03_Megha/models/Question.js
Original file line number Diff line number Diff line change
@@ -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);
3 changes: 2 additions & 1 deletion P03_StackOverflow'sMiniature/P03_Megha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>",
"license": "ISC",
"dependencies": {
"bcrypt": "^3.0.6",
Expand Down

0 comments on commit 800d2bd

Please sign in to comment.