-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
191 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
P03_StackOverflow'sMiniature/P03_Bhanu0202/route/Profile.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters