Skip to content

Commit

Permalink
day 3-4
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhanu0202 committed Jul 6, 2019
1 parent f98203b commit b065e11
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 9 deletions.
12 changes: 6 additions & 6 deletions P03_StackOverflow'sMiniature/P03_Bhanu0202/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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}`);
Expand Down
71 changes: 71 additions & 0 deletions P03_StackOverflow'sMiniature/P03_Bhanu0202/models/Profile.js
Original file line number Diff line number Diff line change
@@ -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);
53 changes: 53 additions & 0 deletions P03_StackOverflow'sMiniature/P03_Bhanu0202/models/Ques.js
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var mongoose = require("mongoose");
const Schema = mongoose.Schema;

const UserSchema = new Schema({
username: {
type: String,
Expand All @@ -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);
56 changes: 56 additions & 0 deletions P03_StackOverflow'sMiniature/P03_Bhanu0202/route/Ques.js
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 7 additions & 2 deletions P03_StackOverflow'sMiniature/P03_Bhanu0202/route/User.js
Original file line number Diff line number Diff line change
@@ -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`);
});
Expand Down Expand Up @@ -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
});
}
);
Expand Down

0 comments on commit b065e11

Please sign in to comment.