Skip to content

Commit

Permalink
Merge branch 'i05_US05_user_sign_up' of https://www.github.com/fga-ep…
Browse files Browse the repository at this point in the history
…s-mds/2020.1-Grupo2-BackEnd into i05_US05_user_sign_up
  • Loading branch information
senaarth committed Sep 24, 2020
2 parents f27ae46 + aab50a6 commit c3179b6
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const morgan = require('morgan');
const mongoose = require('mongoose');

const itemRoutes = require('./routes/itemRoutes');
const authRoutes = require('./routes/authRoutes');

// MongoDB connection
mongoose
Expand All @@ -25,8 +26,9 @@ app.use(express.json());

// routes
app.use('/item',itemRoutes);
app.use('/auth',authRoutes);

// starting the server
app.listen(app.get('port'), () => {
console.log(`Server on port ${app.get('port')}`);
});
});
21 changes: 21 additions & 0 deletions src/models/user.js
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;
91 changes: 91 additions & 0 deletions src/routes/authRoutes.js
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;

0 comments on commit c3179b6

Please sign in to comment.