Skip to content

Commit

Permalink
day 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhanu0202 committed Jul 6, 2019
1 parent b065e11 commit 45f216c
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 2 deletions.
4 changes: 2 additions & 2 deletions P03_StackOverflow'sMiniature/P03_Bhanu0202/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var passport = require("passport");
require("./strategies/jsonwtStrategy") (passport);
const user = require("./route/User");
const ques = require("./route/Ques");
//const profile = require("./route/Profile");
const profile = require("./route/Profile");

var app = express();

Expand All @@ -29,7 +29,7 @@ mongoose
});

app.use("/api/user", user);
//app.use("/api/profile", profile);
app.use("/api/profile", profile);
app.use("/api/questions", ques);

app.listen(port, hostname, () => {
Expand Down
169 changes: 169 additions & 0 deletions P03_StackOverflow'sMiniature/P03_Bhanu0202/route/Profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
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");

route.get(
"/",
passport.authenticate("jwt", { session: false }),
(req, res) => {
Profile.findOne({ user: req.user.id })
.then(profile => {
if (!profile) {
return res.status(404).json({ profilenotfound: "No profile Found" });
}
res.json(profile);
})
.catch(err => console.log("got some error in profile " + err));
}
);

route.post(
"/",
passport.authenticate("jwt", { session: false }),
(req, res) => {
const profileValues = {};
profileValues.user = req.user.id;
if (req.body.username) profileValues.username = req.body.username;
if (req.body.website) profileValues.website = req.body.website;
if (req.body.country) profileValues.country = req.body.country;
if (req.body.portfolio) profileValues.portfolio = req.body.portfolio;
if (typeof req.body.languages !== undefined) {
profileValues.languages = req.body.languages.split(",");
}

profileValues.social = {};

if (req.body.youtube) profileValues.social.youtube = req.body.youtube;
if (req.body.facebook) profileValues.social.facebook = req.body.facebook;
if (req.body.instagram) profileValues.social.instagram = req.body.instagram;

Profile.findOne({ user: req.user.id })
.then(profile => {
if (profile) {
Profile.findOneAndUpdate(
{ user: req.user.id },
{ $set: profileValues },
{ new: true }
)
.then(profile => res.json(profile))
.catch(err => console.log("problem in update" + err));
} else {
Profile.findOne({ username: profileValues.username })
.then(profile => {

if (profile) {
res.status(400).json({ username: "Username already exists" });
}

new Profile(profileValues)
.save()
.then(profile => res.json(profile))
.catch(err => console.log(err));
})
.catch(err => console.log(err));
}
})
.catch(err => console.log("Problem in fetching profile" + err));
}
);


route.get("/:username", (req, res) => {
Profile.findOne({ username: req.params.username })
.populate("user", ["name", "profilepic"])
.then(profile => {
if (!profile) {
res.status(404).json({ usernotfound: "User not found" });
}
res.json(profile);
})
.catch(err => console.log("Error in fetching username " + err));
});


route.get("/find/everyone", (req, res) => {
Profile.find()
.populate("user", ["name", "profilepic"])
.then(profiles => {
if (!profiles) {
res.status(404).json({ usernotfound: "NO profile was found" });
}
res.json(profiles);
})
.catch(err => console.log("Error in fetching username " + err));
});


route.delete(
"/",
passport.authenticate("jwt", { session: false }),
(req, res) => {
//Profile.findOne({ user: req.user.id });
Profile.findOneAndRemove({ user: req.user.id })
.then(() => {
Person.findOneAndRemove({ _id: req.user.id })
.then(() => res.json({ success: "delete was a success" }))
.catch(err => console.log(err));
})
.catch(err => console.log(err));
}
);


route.post(
"/workrole",
passport.authenticate("jwt", { session: false }),
(req, res) => {
Profile.findOne({ user: req.user.id })
.then(profile => {
const newWork = {
role: req.body.role,
company: req.body.company,
country: req.body.country,
from: req.body.from,
to: req.body.to,
current: req.body.current,
details: req.body.details
};
profile.workrole.unshift(newWork);
profile
.save()
.then(profile => res.json(profile))
.catch(err => console.log(err));
})
.catch(err => console.log(err));
}
);


route.delete(
"/workrole/:w_id",
passport.authenticate("jwt", { session: false }),
(req, res) => {
Profile.findOne({ user: req.user.id })
.then(profile => {
const removethis = profile.workrole
.map(item => item.id)
.indexOf(req.params.w_id);

profile.workrole.splice(removethis, 1);

profile
.save()
.then(profile => res.json(profile))
.catch(err => console.log(err));
})
.catch(err => console.log(err));
}
);

module.exports = route;
20 changes: 20 additions & 0 deletions P03_StackOverflow'sMiniature/P03_Bhanu0202/route/Ques.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,25 @@ route.post("/answer:id", passport.authenticate("jwt", { session: false }),
.catch(err => console.log("Error is " + err));
});

route.post("/upvote:id", passport.authenticate("jwt", { session: false }),
(req, res) => {
Ques.findById(req.params.id)
.then(ques => {
if (
ques.upvotes.filter(
upvote => upvote.user.toString() === req.user.id.toString()
).length > 0
) {
return res.status(400).json({ noupvote: "User already upvoted" });
}
ques.upvotes.unshift({ user: req.user.id });
ques
.save()
.then(ques => res.json(ques))
.catch(err => console.log("Error is " + err));
})
.catch(err => console.log("Error is " + err));
});


module.exports = route;

0 comments on commit 45f216c

Please sign in to comment.