-
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
Showing
8 changed files
with
472 additions
and
3 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
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 uniqueValidator = require('mongoose-unique-validator'); | ||
const Schema = mongoose.Schema; | ||
|
||
let categoriaSchema = new Schema({ | ||
|
||
descripcion: { | ||
type: String, | ||
unique: true, | ||
required: [true, 'La descripcion del producto es requerida'] | ||
}, | ||
usuario: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'Usuario' | ||
} | ||
|
||
}); | ||
|
||
categoriaSchema.plugin(uniqueValidator, { message: 'Errpr, {PATH} debe de ser unico' }); | ||
|
||
module.exports = mongoose.model('Categoria', categoriaSchema); |
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,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: 'Usuario' } | ||
}); | ||
|
||
|
||
module.exports = mongoose.model('Producto', productoSchema); |
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,164 @@ | ||
const express = require('express'); | ||
const _ = require('underscore'); | ||
|
||
const { verificaToken, verificaAdmin_Role } = require('../middlewares/authentication'); | ||
|
||
const app = express(); | ||
|
||
let Categoria = require('../models/categoria'); | ||
|
||
// ============================ | ||
// Mostrar todas las categorias | ||
// ============================ | ||
app.get('/categoria', verificaToken, (req, res) => { | ||
let from = req.query.from || 0; | ||
let limit = req.query.limit || 10; | ||
|
||
from = Number(from); | ||
limit = Number(limit); | ||
|
||
Categoria.find({}) | ||
.sort('descripcion') | ||
.skip(from) | ||
.limit(limit) | ||
.populate('usuario', 'nombre email') | ||
.exec((err, categoriasDB) => { | ||
if (err) { | ||
return res.status(400).json({ | ||
ok: false, | ||
err | ||
}) | ||
}; | ||
Categoria.countDocuments({}, (err, count) => { | ||
res.json({ | ||
ok: true, | ||
categorias: categoriasDB, | ||
cuantos: count | ||
}); | ||
}) | ||
|
||
}); | ||
}); | ||
|
||
// ============================ | ||
// Mostrar categoria 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 pertenece a ninguna categoria' | ||
} | ||
}); | ||
}; | ||
|
||
res.json({ | ||
ok: true, | ||
categoria: categoriaDB | ||
}) | ||
|
||
}) | ||
|
||
}); | ||
|
||
// ============================ | ||
// Crear nueva categoria | ||
// ============================ | ||
app.post('/categoria', [verificaToken, verificaAdmin_Role], (req, res) => { | ||
// Regresa la nueva categoria | ||
|
||
const body = req.body; | ||
const usuario = req.usuario | ||
|
||
let categoria = new Categoria({ | ||
descripcion: body.descripcion, | ||
usuario: usuario._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 | ||
}) | ||
}) | ||
}); | ||
|
||
// ============================ | ||
// Actualizar una categoria | ||
// ============================ | ||
app.put('/categoria/:id', [verificaToken, verificaAdmin_Role], (req, res) => { | ||
|
||
let id = req.params['id']; | ||
let body = _.pick(req.body, ['descripcion']); | ||
|
||
let opc = { new: true, runValidators: true, context: 'query' } | ||
|
||
Categoria.findByIdAndUpdate(id, body, opc, (err, categoriaDB) => { | ||
if (err) { | ||
return res.status(400).json({ | ||
ok: false, | ||
err | ||
}); | ||
}; | ||
|
||
res.json({ | ||
ok: true, | ||
categoria: categoriaDB | ||
}) | ||
}) | ||
}); | ||
|
||
|
||
// ============================ | ||
// Crear nueva categoria | ||
// ============================ | ||
app.delete('/categoria/:id', [verificaToken, verificaAdmin_Role], (req, res) => { | ||
// Solo un administrador puede borrar categoria | ||
const id = req.params.id; | ||
|
||
Categoria.findByIdAndDelete(id, (err, categoriaDB) => { | ||
if (err) { | ||
return res.status(500).json({ | ||
ok: false, | ||
err | ||
}); | ||
}; | ||
if (!categoriaDB) { | ||
return res.status(400).json({ | ||
ok: false, | ||
err: { | ||
message: 'Categoria no encontrado' | ||
} | ||
}) | ||
} | ||
res.json({ | ||
ok: true, | ||
categoria: categoriaDB | ||
}) | ||
}) | ||
}); | ||
|
||
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
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
Oops, something went wrong.