Skip to content

Commit

Permalink
Productos y Categorias
Browse files Browse the repository at this point in the history
  • Loading branch information
francislagares committed Oct 12, 2019
1 parent 2f6edcf commit db930c4
Show file tree
Hide file tree
Showing 9 changed files with 436 additions and 11 deletions.
1 change: 0 additions & 1 deletion index.js

This file was deleted.

2 changes: 1 addition & 1 deletion server/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ process.env.PORT = process.env.PORT || 3000;
process.env.NODE_ENV = process.env.NODE_ENV || 'dev';

// Vencimiento del Token
process.env.CADUCIDAD_TOKEN = 60 * 60 * 24 * 30;
process.env.CADUCIDAD_TOKEN = '48h';

// SEED de autenticación
process.env.SEED = process.env.SEED || 'este-es-el-seed-de-desarrollo';
Expand Down
9 changes: 3 additions & 6 deletions server/middlewares/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,20 @@ let verificaToken = (req, res, next) => {
});
}

req.usuario = decoded.usuario;
req.user = decoded.user;
next();

});



};

// =====================
// Verifica AdminRole
// =====================
let verificaAdmin_Role = (req, res, next) => {

let usuario = req.body;
let user = req.body;

if (usuario.role === 'ADMIN_ROLE') {
if (user.role === 'ADMIN_ROLE') {
next();
} else {

Expand Down
9 changes: 9 additions & 0 deletions server/models/categoria.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const categoriaSchema = new Schema({
descripcion: { type: String, unique: true, required: [true, 'La descripción es obligatoria'] },
usuario: { type: Schema.Types.ObjectId, ref: 'User' }
});

module.exports = mongoose.model('Categoria', categoriaSchema);
15 changes: 15 additions & 0 deletions server/models/producto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;


var productoSchema = new Schema({
nombre: { type: String, required: [true, 'El nombre es necesario'] },
precioUni: { type: Number, required: [true, 'El precio únitario es necesario'] },
descripcion: { type: String, required: false },
disponible: { type: Boolean, required: true, default: true },
categoria: { type: Schema.Types.ObjectId, ref: 'Categoria', required: true },
usuario: { type: Schema.Types.ObjectId, ref: 'User' }
});


module.exports = mongoose.model('Producto', productoSchema);
164 changes: 164 additions & 0 deletions server/routes/categoria.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
const express = require('express');

const { verificaToken, verificaAdmin_Role } = require('../middlewares/authentication');
const Categoria = require('../models/categoria');

const app = express();

// Mostrar todas las categorias
app.get('/categoria', verificaToken, (req, res) => {

let body = req.body;

Categoria.find({})
.sort('descripcion')
.populate('usuario', 'nombre email')
.exec((err, categorias) => {

if (err) {
res.status(500).json({
ok: false,
err
});
}

res.json({
categorias
});

});
});

// Mostrar una categoria por ID
app.get('/categoria/:id', verificaToken, (req, res) => {

let id = req.params.id;

Categoria.findById(id, (err, categoriaDB) => {

if (err) {
return res.status(500).json({
ok: false,
err
});
}

if (!categoriaDB) {
return res.status(400).json({
ok: false,
err: {
message: 'El ID no es correcto'
}
});
}

res.json({
ok: true,
categoria: categoriaDB
});

});

});

// Crear nueva categoria
app.post('/categoria', verificaToken, (req, res) => {

let body = req.body;

let categoria = new Categoria({
descripcion: body.descripcion,
usuario: req.user._id
});

categoria.save((err, categoriaDB) => {

if (err) {
return res.status(500).json({
ok: false,
err
});
}

if (!categoriaDB) {
return res.status(400).json({
ok: false,
err
});
}

res.json({
ok: true,
categoria: categoriaDB
});

});

});


app.put('/categoria/:id', verificaToken, (req, res) => {

let id = req.params.id;
let body = req.body;

let desCategoria = {
descripcion: body.descripcion,
}

Categoria.findByIdAndUpdate(id, desCategoria, { new: true, runValidators: true }, (err, categoriaDB) => {

if (err) {
return res.status(500).json({
ok: false,
err
});
}

if (!categoriaDB) {
return res.status(400).json({
ok: false,
err
});

}

res.json({
ok: true,
categoria: categoriaDB
});
});

});


app.delete('/categoria/:id', [verificaToken, verificaAdmin_Role], (req, res) => {

let id = req.params.id;

Categoria.findByIdAndRemove(id, (err, categoriaDB) => {

if (err) {
return res.status(500).json({
ok: false,
err
});
}

if (!categoriaDB) {
return res.status(400).json({
ok: false,
err
});
}

res.json({
ok: true,
message: categoriaDB
});
});

});


module.exports = app;
2 changes: 2 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const app = express();

app.use(require('./user'));
app.use(require('./login'));
app.use(require('./categoria'));
app.use(require('./producto'));


module.exports = app;
Loading

0 comments on commit db930c4

Please sign in to comment.