-
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
1 parent
2f6edcf
commit db930c4
Showing
9 changed files
with
436 additions
and
11 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
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,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); |
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: 'User' } | ||
}); | ||
|
||
|
||
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
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; |
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.