-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit c9d2d65
Showing
8 changed files
with
759 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
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 @@ | ||
'use strict' |
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
{ | ||
"name": "rest-node-server", | ||
"version": "1.0.0", | ||
"description": "Basic Webserver made with Express", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node server.js", | ||
"nodemon": "nodemon server.js", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"bcryptjs": "^2.4.3", | ||
"body-parser": "^1.19.0", | ||
"express": "^4.17.1", | ||
"mongoose": "^5.7.3", | ||
"mongoose-unique-validator": "^2.0.3", | ||
"underscore": "^1.9.1" | ||
} | ||
} |
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,16 @@ | ||
// Puerto | ||
process.env.PORT = process.env.PORT || 3000; | ||
|
||
// Entorno | ||
process.env.NODE_ENV = process.env.NODE_ENV || 'dev'; | ||
|
||
// Base de datos | ||
let urlDB; | ||
|
||
if (process.env.NODE_ENV === 'dev') { | ||
urlDB = 'mongodb://localhost:27017/cafe'; | ||
} else { | ||
urlDB = 'mongodb+srv://cafe-user:Grr4nce1Kyn0YaQB@cluster0-sr586.mongodb.net/cafe'; | ||
} | ||
|
||
process.env.URLDB = urlDB; |
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,53 @@ | ||
const mongoose = require("mongoose"); | ||
const uniqueValidator = require('mongoose-unique-validator'); | ||
|
||
const validRoles = { | ||
values: ['ADMIN_ROLE', 'USER_ROLE'], | ||
message: '{VALUE} no es un rol válido' | ||
}; | ||
|
||
const Schema = mongoose.Schema; | ||
|
||
const userSchema = new Schema({ | ||
nombre: { | ||
type: String, | ||
required: [true, "El nombre es necesario"] | ||
}, | ||
email: { | ||
type: String, | ||
unique: true, | ||
required: [true, "El email es necesario"] | ||
}, | ||
password: { | ||
type: String, | ||
required: [true, "El password es necesario"] | ||
}, | ||
img: { | ||
type: String, | ||
required: false | ||
}, | ||
role: { | ||
type: String, | ||
default: "USER_ROLE", | ||
enum: validRoles | ||
}, | ||
state: { | ||
type: Boolean, | ||
default: true | ||
}, | ||
google: { | ||
type: Boolean, | ||
default: false | ||
} | ||
}); | ||
|
||
userSchema.methods.toJSON = function() { | ||
let user = this; | ||
let userObject = user.toObject(); | ||
delete userObject.password; | ||
|
||
return userObject; | ||
} | ||
|
||
userSchema.plugin(uniqueValidator, { message: '{PATH} debe ser único' }) | ||
module.exports = mongoose.model("User", userSchema); |
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,123 @@ | ||
const express = require('express'); | ||
const bcrypt = require('bcryptjs'); | ||
const _ = require('underscore'); | ||
const User = require('../models/user'); | ||
|
||
const app = express(); | ||
|
||
app.get('/usuario', (req, res) => { | ||
|
||
let desde = req.query.desde || 0; | ||
desde = Number(desde); | ||
|
||
let limite = req.query.limite || 0; | ||
limite = Number(limite); | ||
|
||
User.find({ state: true }, 'nombre email role state google img') | ||
.skip(desde) | ||
.limit(limite) | ||
.exec((err, usuario) => { | ||
|
||
if (err) { | ||
return res.status(400).json({ | ||
ok: false, | ||
err | ||
}); | ||
} | ||
|
||
User.count({ state: true }, (err, counter) => { | ||
|
||
res.json({ | ||
ok: true, | ||
usuario, | ||
counter | ||
}); | ||
|
||
}) | ||
|
||
}); | ||
}); | ||
|
||
app.post('/usuario', (req, res) => { | ||
|
||
let body = req.body; | ||
|
||
let user = new User({ | ||
nombre: body.nombre, | ||
email: body.email, | ||
password: bcrypt.hashSync(body.password, 10), | ||
role: body.role | ||
}); | ||
|
||
user.save((err, userDB) => { | ||
|
||
if (err) { | ||
return res.status(400).json({ | ||
ok: false, | ||
err | ||
}); | ||
} | ||
res.json({ | ||
ok: true, | ||
user: userDB | ||
}); | ||
}); | ||
}); | ||
|
||
app.put('/usuario/:id', (req, res) => { | ||
|
||
let id = req.params.id; | ||
let body = _.pick(req.body, ['nombre', 'email', 'img', 'role', 'state']); | ||
|
||
User.findByIdAndUpdate(id, body, { new: true }, (err, userDB) => { | ||
|
||
if (err) { | ||
return res.status(400).json({ | ||
ok: false, | ||
err | ||
}); | ||
} | ||
res.json({ | ||
ok: true, | ||
user: userDB | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
app.delete('/usuario/:id', (req, res) => { | ||
|
||
let id = req.params.id; | ||
let changeState = { | ||
state: false | ||
} | ||
|
||
User.findByIdAndUpdate(id, changeState, { new: true }, (err, usuarioBorrado) => { | ||
|
||
if (err) { | ||
return res.status(400).json({ | ||
ok: false, | ||
err | ||
}); | ||
} | ||
|
||
if (!usuarioBorrado) { | ||
return res.status(400).json({ | ||
ok: false, | ||
err: { | ||
message: 'Usuario no encontrado' | ||
} | ||
}); | ||
} | ||
|
||
res.json({ | ||
ok: true, | ||
usuario: usuarioBorrado | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
module.exports = app; |
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,25 @@ | ||
'use strict'; | ||
|
||
require('./config/config'); | ||
const express = require('express'); | ||
const mongoose = require('mongoose'); | ||
const bodyParser = require('body-parser'); | ||
|
||
const app = express(); | ||
const port = 3000; | ||
|
||
// parse application/x-www-form-urlencoded | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
|
||
// parse application/json | ||
app.use(bodyParser.json()); | ||
app.use(require('./routes/user')); | ||
|
||
mongoose.connect(process.env.URLDB, (err, res) => { | ||
|
||
if (err) throw err; | ||
|
||
console.log('Base de datos ONLINE'); | ||
}); | ||
|
||
app.listen(port, () => console.log(`Example app listening on port ${port}`)); |