-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'i05_US05_user_sign_up' of https://www.github.com/fga-ep…
…s-mds/2020.1-Grupo2-BackEnd into i05_US05_user_sign_up
- Loading branch information
Showing
3 changed files
with
115 additions
and
1 deletion.
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
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,21 @@ | ||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
|
||
const userSchema = new Schema({ | ||
username: { | ||
type: String, | ||
required: true | ||
}, | ||
password: { | ||
type: String, | ||
required: true | ||
}, | ||
email: { | ||
type: String, | ||
required: true | ||
} | ||
}); | ||
|
||
const User = mongoose.model('User', userSchema); | ||
|
||
module.exports = User; |
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,91 @@ | ||
const express = require('express'); | ||
|
||
const router = express.Router(); | ||
|
||
const User = require('../models/user'); | ||
const userSchema = require('../schemas/userSchema'); | ||
|
||
router.get('/', (req, res) => { | ||
res.json({ | ||
message: 'Authentication!' | ||
}); | ||
}); | ||
|
||
router.post('/signup', async(req, res, next) => { | ||
|
||
try { | ||
|
||
const newUserData = req.body; | ||
const result = userSchema.validate(req.body); | ||
|
||
if( await User.findOne({ username: newUserData.username}) ) { | ||
const error = new Error('Username already being used.'); | ||
return next(error); | ||
} | ||
|
||
if( result.error ) { | ||
return next(result.error); | ||
} | ||
|
||
const user = new User(newUserData); | ||
|
||
user.save() | ||
.then( result => { | ||
return res.send(result); | ||
}) | ||
.catch( err => next(err)); | ||
|
||
} catch(err) { | ||
return next(err); | ||
} | ||
|
||
}); | ||
|
||
router.put('/update-user/:id', async(req, res, next) => { | ||
|
||
try { | ||
|
||
const user = await User.findById(req.params.id); | ||
const newData = req.body; | ||
|
||
if ( !newData.username ) { | ||
newData.username = user.username; | ||
} | ||
if ( !newData.password ) { | ||
newData.password = user.password; | ||
} | ||
if ( !newData.email ) { | ||
newData.email = user.email; | ||
} | ||
|
||
const result = userSchema.validate(newData); | ||
|
||
if(result.error) { | ||
return next(result.error); | ||
} | ||
|
||
await User.findOneAndUpdate({_id: req.params.id}, req.body, { useFindAndModify: false}) | ||
.then( () => { | ||
res.send({ message: 'User updated successfully.'}); | ||
}); | ||
|
||
} catch(err) { | ||
return next(err); | ||
} | ||
|
||
}); | ||
|
||
router.delete('/delete-user/:id', async(req, res, next) => { | ||
|
||
try { | ||
|
||
await User.findByIdAndDelete(req.params.id); | ||
return res.send({ message: 'User successfully deleted.' }); | ||
|
||
} catch(err) { | ||
return next(err); | ||
} | ||
|
||
}); | ||
|
||
module.exports = router; |