diff --git a/P03_StackOverflow'sMiniature/P03_Bhanu0202/index.js b/P03_StackOverflow'sMiniature/P03_Bhanu0202/index.js index c4d9f7b8..fd048436 100644 --- a/P03_StackOverflow'sMiniature/P03_Bhanu0202/index.js +++ b/P03_StackOverflow'sMiniature/P03_Bhanu0202/index.js @@ -6,7 +6,9 @@ var key = require("./setup/myurl"); const jsonwt = require("jsonwebtoken"); var passport = require("passport"); require("./strategies/jsonwtStrategy") (passport); -const auth = require("./route/User"); +const user = require("./route/User"); +const ques = require("./route/Ques"); +//const profile = require("./route/Profile"); var app = express(); @@ -26,11 +28,9 @@ mongoose console.log("error is ", err.message); }); -// app.get("/", (req, res) => { -// res.send("Hey there Big stack"); -// }); - -app.use("", auth); +app.use("/api/user", user); +//app.use("/api/profile", profile); +app.use("/api/questions", ques); app.listen(port, hostname, () => { console.log(`Server is running on ${hostname}:${port}`); diff --git a/P03_StackOverflow'sMiniature/P03_Bhanu0202/models/Profile.js b/P03_StackOverflow'sMiniature/P03_Bhanu0202/models/Profile.js new file mode 100644 index 00000000..0d90adac --- /dev/null +++ b/P03_StackOverflow'sMiniature/P03_Bhanu0202/models/Profile.js @@ -0,0 +1,71 @@ +var mongoose = require("mongoose"); +const Schema = mongoose.Schema; + +const ProfileSchema = new Schema({ + user: { + type: Schema.Types.ObjectId, + ref: "myPerson" + }, + username: { + type: String, + required: true, + max: 50 + }, + website: { + type: String + }, + country: { + type: String + }, + languages: { + type: [String], + required: true + }, + portfolio: { + type: String + }, + workrole: [ + { + role: { + type: String, + required: true + }, + company: { + type: String + }, + country: { + type: String + }, + from: { + type: Date + }, + to: { + type: Date + }, + current: { + type: Boolean, + default: false + }, + details: { + type: String + } + } + ], + social: { + youtube: { + type: String + }, + facebook: { + type: String + }, + instagram: { + type: String + } + }, + date: { + type: Date, + default: Date.now + } +}); + +module.exports = Profile = mongoose.model("ProfileSchema", ProfileSchema); \ No newline at end of file diff --git a/P03_StackOverflow'sMiniature/P03_Bhanu0202/models/Ques.js b/P03_StackOverflow'sMiniature/P03_Bhanu0202/models/Ques.js new file mode 100644 index 00000000..43b3f55d --- /dev/null +++ b/P03_StackOverflow'sMiniature/P03_Bhanu0202/models/Ques.js @@ -0,0 +1,53 @@ +var mongoose = require("mongoose"); +const Schema = mongoose.Schema; + +const QuesSchema = new Schema({ + user: { + type: mongoose.Types.ObjectId, + ref: "myPerson" + }, + header: { + type: String, + required: true + }, + description: { + type: String, + required: true + }, + name: { + type: String + }, + upvotes: [ + { + user: { + type: Schema.Types.ObjectId, + ref: "myPerson" + } + } + ], + answers: [ + { + user: { + type: Schema.Types.ObjectId, + ref: "myPerson" + }, + text: { + type: String, + required: true + }, + name: { + type: String + }, + date: { + type: Date, + default: Date.now + } + } + ], + date: { + type: Date, + default: Date.now + } +}); + +module.exports = Ques = mongoose.model("QuesSchema", QuesSchema); diff --git a/P03_StackOverflow'sMiniature/P03_Bhanu0202/modals/User.js b/P03_StackOverflow'sMiniature/P03_Bhanu0202/models/User.js similarity index 66% rename from P03_StackOverflow'sMiniature/P03_Bhanu0202/modals/User.js rename to P03_StackOverflow'sMiniature/P03_Bhanu0202/models/User.js index 5995154e..865aee47 100644 --- a/P03_StackOverflow'sMiniature/P03_Bhanu0202/modals/User.js +++ b/P03_StackOverflow'sMiniature/P03_Bhanu0202/models/User.js @@ -1,5 +1,6 @@ var mongoose = require("mongoose"); const Schema = mongoose.Schema; + const UserSchema = new Schema({ username: { type: String, @@ -12,7 +13,15 @@ const UserSchema = new Schema({ password : { type: String, require: true - } + }, + profilepic: { + type: String, + default: "https://learncodeonline.in/manicon.png" + }, + date: { + type: Date, + default: Date.now + } }); module.exports = User = mongoose.model("UserSchema", UserSchema); \ No newline at end of file diff --git a/P03_StackOverflow'sMiniature/P03_Bhanu0202/route/Ques.js b/P03_StackOverflow'sMiniature/P03_Bhanu0202/route/Ques.js new file mode 100644 index 00000000..45948cb6 --- /dev/null +++ b/P03_StackOverflow'sMiniature/P03_Bhanu0202/route/Ques.js @@ -0,0 +1,56 @@ +var express = require('express'); +var route = express.Router(); +var key = require("../setup/myurl"); +const jsonwt = require("jsonwebtoken"); +var passport = require("passport"); +require("../strategies/jsonwtStrategy") (passport); +var bcrypt = require("bcrypt"); +var saltRounds = 10; + +//Models +var User = require("../models/User"); +var Profile = require("../models/Profile"); +var Ques = require("../models/Ques"); + +route.get("/", (req, res) => { + Ques.find() + .sort({ date: "desc" }) + .then(questions => res.json(questions)) + .catch(err => res.json({ noquestions: "NO questions to display" })); +}); + +route.post("/", passport.authenticate("jwt", { session: false }), + (req, res) => { + const newQues = new Ques({ + header: req.body.header, + description: req.body.description, + user: req.user.id, + name: req.body.name + }); + newQues + .save() + .then(ques => res.json(ques)) + .catch(err => console.log("Error is " + err)); +}); + +route.post("/answer:id", passport.authenticate("jwt", { session: false }), + (req, res) => { + Ques.findById(req.params.id) + .then(ques => { + const newAns = { + user: req.user.id, + name: req.body.name, + text: req.body.text + }; + ques.answers.unshift(newAns); + + ques + .save() + .then(ques => res.json(ques)) + .catch(err => console.log("Error is " + err)); + }) + .catch(err => console.log("Error is " + err)); +}); + + +module.exports = route; \ No newline at end of file diff --git a/P03_StackOverflow'sMiniature/P03_Bhanu0202/route/User.js b/P03_StackOverflow'sMiniature/P03_Bhanu0202/route/User.js index 3831b64c..79f092b6 100644 --- a/P03_StackOverflow'sMiniature/P03_Bhanu0202/route/User.js +++ b/P03_StackOverflow'sMiniature/P03_Bhanu0202/route/User.js @@ -1,13 +1,16 @@ var express = require('express'); var route = express.Router(); var key = require("../setup/myurl"); -var User = require("../modals/User"); const jsonwt = require("jsonwebtoken"); var passport = require("passport"); require("../strategies/jsonwtStrategy") (passport); var bcrypt = require("bcrypt"); var saltRounds = 10; +//Models +var User = require("../models/User"); +var Profile = require("../models/Profile"); + route.get('/', (req, res) => { res.status(200).send(`Welcome to login and signup API`); }); @@ -91,7 +94,9 @@ route.get( console.log(req); res.json({ id: req.user.id, - username: req.user.username + username: req.user.username, + email: req.user.email, + profilepic: req.user.profilepic }); } );