Created
October 15, 2018 10:10
-
-
Save GalloDaSballo/0d07d3a4c688e09f0b7b46efbc57767e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const mongoose = require('mongoose') | |
const Profiles = mongoose.model('profiles') | |
const requireLogin = require('../middleware/requireLogin') | |
module.exports = (app) => { | |
app.get('/api/profiles', requireLogin, async (req,res) => { | |
const allProfiles = await Profiles.find({}) | |
res.send(allProfiles) | |
}) | |
app.get('/api/profiles/:id', async (req,res) => { | |
// console.log(req.params.id) | |
// console.log(typeof req.params.id) | |
const profile = await Profiles.findOne({_id: req.params.id}) | |
res.send(profile) | |
}) | |
app.post('/api/profiles/:id', requireLogin, async (req, res) => { | |
const currentUser = req.user.id | |
const requestedUser = req.params.id | |
const {userName, bio, link, picture} = req.body | |
if(currentUser == requestedUser){ | |
//Can create or edit the profile | |
//Check if profile exists -> update | |
const profile = await Profiles.findOne({_id: currentUser}) | |
//TODO: As of now we allow pictures to come from anyplace in the world | |
if(!!profile){ | |
//Update | |
console.log("Profile is being Updated") | |
const profile = await Profiles.updateOne({_id: currentUser, userName, bio, link, picture}) | |
res.send(profile) | |
} else { | |
//Create | |
console.log("Profile is being created") | |
const profile = await Profiles.create({_id: currentUser, userName, bio, link, picture}) | |
res.send(profile) | |
} | |
} else { | |
res.status(401).send({error: `Can't edit a profile you don't own`}) | |
} | |
}) | |
// app.get('api/users/current') | |
// | |
// app.post('api/users/new') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment