Skip to content

Instantly share code, notes, and snippets.

@kaoussi
Created June 3, 2018 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaoussi/55b62a57bb4d4afb324cc3a7afdfe36f to your computer and use it in GitHub Desktop.
Save kaoussi/55b62a57bb4d4afb324cc3a7afdfe36f to your computer and use it in GitHub Desktop.
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const passport = require('passport');
const Profile = require('../../models/Profile');
const User = require('../../models/User');
const validateProfileInput = require('../../validation/profile');
const validateExperienceInput = require('../../validation/experience');
// router.get('/test', (req, res) => res.json({msg: "Profile Work!"}));
// Visit User Profile
router.get(
'/',
passport.authenticate('jwt', {session: false}),
(req, res) => {
const errors = {};
Profile.findOne({ user: req.user.id })
.populate('user', ['name', 'avatar'])
.then(profile => {
if(!profile) {
errors.noprofile = 'There is no profile for this user';
return res.status(404).json({
errors
})
}
res.json(profile);
})
.catch(err => res.status(404).json(err));
});
// Create User Profile
router.post('/', passport.authenticate('jwt', {session: false}), (req, res) => {
const {errors, isValid} = validateProfileInput(req.body);
if(!isValid){
return res.status(400).json(errors);
}
const profileFields = {};
profileFields.user = req.user.id;
if(req.body.handle) profileFields.handle = req.body.handle;
if(req.body.company) profileFields.company = req.body.company;
if(req.body.website) profileFields.website = req.body.website;
if(req.body.location) profileFields.location = req.body.location;
if(req.body.status) profileFields.status = req.body.status;
if(req.body.bio) profileFields.bio = req.body.bio;
if(req.body.githubusername) profileFields.githubusername = req.body.handle;
if(typeof req.body.skills != "undefined" ) {
profileFields.skills = req.body.skills.split(',');
}
profileFields.social = {};
if(req.body.youtube) profileFields.social.youtube = req.body.youtube;
if(req.body.facebook) profileFields.social.facebook = req.body.facebook;
if(req.body.instagram) profileFields.social.instagram = req.body.instagram;
if(req.body.linkedin) profileFields.social.linkedin = req.body.linkedin;
if(req.body.twitter) profileFields.social.twitter = req.body.twitter;
Profile.findOne({ user: req.user.id })
.then( profile => {
if(profile) {
Profile.findOneAndUpdate(
{ user: req.user.id },
{ $set: profileFields },
{ new: true })
.then(profile => res.json(profile));
} else {
Profile.findOne({
handle: profileFields.handle
}).then(profile => {
if(profile) {
errors.handle = "That Handle Already Exists";
res.stats(400).json(errors);
}
new Profile(profileFields).save().then(profile => res.json(profile));
});
}
});
});
router.get('/handle/:handle', (req, res) => {
const errors = {};
Profile.findOne({ handle: req.params.handle })
.populate('user', ['name', 'avatar'])
.then(profile => {
if(!profile) {
errors.noprofile= "There is no profile for this User";
res.status(404).json(errors);
}
res.json(profile);
}).catch(err => res.status(404).json(err));
});
router.get('/user/:user_id', (req, res) => {
const errors = {};
Profile.findOne({ user: req.params.user_id })
.populate('user', ['name', 'avatar'])
.then(profile => {
if(!profile) {
errors.noprofile= "There is no profile for this User";
res.status(404).json(errors);
}
res.json(profile);
}).catch(err => res.status(404).json({
profile: "There is no profile for this user"
}));
});
router.get('/all', (req, res) => {
const errors = {};
Profile.find()
.populate('user', ['name', 'avatar'])
.then(profiles => {
if(!profiles) {
errors.noprofile= "There are no profiles yet";
return res.status(404).json(errors);
}
res.json(profiles);
}).catch(err => res.status(404).json({
profiles: "There are no profile yet"
}));
});
router.post('/experience', passport.authenticate('jwt', { session: false }), (req, res) => {
const errors = {};
Profile.findOne({ user: req.user.id })
.then(profile => {
const newExperience = {
title: req.body.title,
company: req.body.company,
location: req.body.location,
from: req.body.from,
to: req.body.to,
current: req.body.current,
description: req.body.description
}
profile.experience.unshift(newExperience);
profile.save().then(profile => res.json(profile));
});
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment