-
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
11 changed files
with
526 additions
and
6 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,118 @@ | ||
const { response } = require("express"); | ||
const { Usuario, Categoria, Producto } = require("../models"); | ||
const { ObjectId } = require("mongoose").Types; | ||
|
||
const coleccionesPermitidas = ["usuarios", "categorias", "productos", "roles"]; | ||
|
||
const buscarUsuarios = async (termino = "", res = response) => { | ||
const esMongoID = ObjectId.isValid(termino); | ||
|
||
if (esMongoID) { | ||
const usuario = await Usuario.findById(termino); | ||
return res.status(200).json({ | ||
results: usuario ? [usuario] : [], | ||
}); | ||
} | ||
|
||
const regex = new RegExp(termino, "i"); | ||
const usuarios = await Usuario.find({ | ||
$or: [{ nombre: regex }, { correo: regex }], | ||
$and: [{ estado: true }], | ||
}); | ||
|
||
return res.status(200).json({ | ||
results: usuarios, | ||
}); | ||
}; | ||
|
||
const buscarCategorias = async (termino = "", res = response) => { | ||
const esMongoID = ObjectId.isValid(termino); | ||
|
||
if (esMongoID) { | ||
const categoria = await Categoria.findById(termino); | ||
return res.status(200).json({ | ||
results: categoria ? [categoria] : [], | ||
}); | ||
} | ||
|
||
const regex = new RegExp(termino, "i"); | ||
const categorias = await Categoria.find({ nombre: regex, estado: true }); | ||
|
||
return res.status(200).json({ | ||
results: categorias, | ||
}); | ||
}; | ||
|
||
const buscarProductos = async (termino = "", res = response) => { | ||
const esMongoID = ObjectId.isValid(termino); | ||
|
||
if (esMongoID) { | ||
const producto = await Producto.findById(termino); | ||
return res | ||
.status(200) | ||
.json({ | ||
results: producto ? [producto] : [], | ||
}) | ||
.populate("categoria", "nombre"); | ||
} | ||
|
||
const regex = new RegExp(termino, "i"); | ||
const productos = await Producto.find({ | ||
nombre: regex, | ||
estado: true, | ||
}).populate("categoria", "nombre"); | ||
|
||
return res.status(200).json({ | ||
results: productos, | ||
}); | ||
}; | ||
|
||
const buscarRoles = async (termino = "", res = response) => { | ||
const esMongoID = ObjectId.isValid(termino); | ||
|
||
if (esMongoID) { | ||
const usuario = await Usuario.findById(termino); | ||
return res.status(200).json({ | ||
results: usuario ? [usuario] : [], | ||
}); | ||
} | ||
|
||
const regex = new RegExp(termino, "i"); | ||
const usuarios = await Usuario.find({ | ||
$or: [{ nombre: regex }, { correo: regex }], | ||
$and: [{ estado: true }], | ||
}); | ||
|
||
return res.status(200).json({ | ||
results: usuarios, | ||
}); | ||
}; | ||
|
||
const buscar = (req, res = response) => { | ||
const { coleccion, termino } = req.params; | ||
|
||
if (!coleccionesPermitidas.includes(coleccion)) | ||
return res.status(400).json({ | ||
msg: `Las colecciones permitidas son ${coleccionesPermitidas}`, | ||
}); | ||
|
||
switch (coleccion) { | ||
case "usuarios": | ||
buscarUsuarios(termino, res); | ||
break; | ||
case "categorias": | ||
buscarCategorias(termino, res); | ||
break; | ||
case "productos": | ||
buscarProductos(termino, res); | ||
break; | ||
case "roles": | ||
break; | ||
default: | ||
res.status(500).json({ | ||
msg: "Se me olvido hacer está búsqueda", | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = { buscar }; |
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,83 @@ | ||
const { response } = require("express"); | ||
const { Categoria } = require("../models"); | ||
|
||
const obtenerCategoria = async (req, res = response) => { | ||
const { id } = req.params; | ||
|
||
const categoria = await Categoria.findById(id).populate("usuario", "nombre"); | ||
|
||
res.status(200).json({ categoria }); | ||
}; | ||
|
||
const obtenerCategorias = async (req, res = response) => { | ||
const { limite = 5, desde = 0 } = req.query; | ||
const query = { estado: true }; | ||
|
||
const [total, categorias] = await Promise.all([ | ||
Categoria.countDocuments(query), | ||
Categoria.find(query) | ||
.populate("usuario", "nombre") | ||
.skip(Number(desde)) | ||
.limit(Number(limite)), | ||
]); | ||
|
||
res.status(200).json({ | ||
total, | ||
categorias, | ||
}); | ||
}; | ||
|
||
const crearCategoria = async (req, res = response) => { | ||
const nombre = req.body.nombre.toUpperCase(); | ||
|
||
const categoriaDB = await Categoria.findOne({ nombre }); | ||
|
||
if (categoriaDB) { | ||
return res.status(400).json({ | ||
msg: `La categoria ${categoriaDB.nombre}, ya existe`, | ||
}); | ||
} | ||
|
||
// Generar la data a guardar | ||
const data = { | ||
nombre, | ||
usuario: req.usuario._id, | ||
}; | ||
|
||
const categoria = await new Categoria(data); | ||
await categoria.save(); | ||
|
||
res.status(201).json(categoria); | ||
}; | ||
|
||
const actualizarCategoria = async (req, res = response) => { | ||
const { id } = req.params; | ||
const { estado, usuario, ...data } = req.body; | ||
|
||
data.nombre = data.nombre.toUpperCase(); | ||
data.usuario = req.usuario._id; | ||
|
||
const categoria = await Categoria.findByIdAndUpdate(id, data, { new: true }); | ||
|
||
res.status(200).json(categoria); | ||
}; | ||
|
||
const borrarCategoria = async (req, res = response) => { | ||
const { id } = req.params; | ||
|
||
const categoria = await Categoria.findByIdAndUpdate( | ||
id, | ||
{ estado: false }, | ||
{ new: true } | ||
); | ||
|
||
res.status(200).json(categoria); | ||
}; | ||
|
||
module.exports = { | ||
obtenerCategoria, | ||
obtenerCategorias, | ||
crearCategoria, | ||
actualizarCategoria, | ||
borrarCategoria, | ||
}; |
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,86 @@ | ||
const { response } = require("express"); | ||
const { Producto } = require("../models"); | ||
|
||
const obtenerProducto = async (req, res = response) => { | ||
const { id } = req.params; | ||
|
||
const producto = await Producto.findById(id) | ||
.populate("usuario", "nombre") | ||
.populate("categoria", "nombre"); | ||
|
||
res.status(200).json({ producto }); | ||
}; | ||
|
||
const obtenerProductos = async (req, res = response) => { | ||
const { limite = 5, desde = 0 } = req.query; | ||
const query = { estado: true }; | ||
|
||
const [total, productos] = await Promise.all([ | ||
Producto.countDocuments(query), | ||
Producto.find(query) | ||
.populate("usuario", "nombre") | ||
.populate("categoria", "nombre") | ||
.skip(Number(desde)) | ||
.limit(Number(limite)), | ||
]); | ||
|
||
res.status(200).json({ | ||
total, | ||
productos, | ||
}); | ||
}; | ||
|
||
const crearProducto = async (req, res = response) => { | ||
const { estado, usuario, ...body } = req.body; | ||
|
||
const productoDB = await Producto.findOne({ nombre: body.nombre }); | ||
|
||
if (productoDB) | ||
return res.status(400).json({ | ||
msg: `El producto ${productoDB.nombre}, ya existe`, | ||
}); | ||
|
||
// Generar la data a guardar | ||
const data = { | ||
...body, | ||
nombre: body.nombre.toUpperCase(), | ||
usuario: req.usuario._id, | ||
}; | ||
|
||
const producto = await new Producto(data); | ||
await producto.save(); | ||
|
||
res.status(201).json(producto); | ||
}; | ||
|
||
const actualizarProducto = async (req, res = response) => { | ||
const { id } = req.params; | ||
const { estado, usuario, ...data } = req.body; | ||
|
||
if (data.nombre) data.nombre = data.nombre.toUpperCase(); | ||
data.usuario = req.usuario._id; | ||
|
||
const producto = await Producto.findByIdAndUpdate(id, data, { new: true }); | ||
|
||
res.status(200).json(producto); | ||
}; | ||
|
||
const borrarProducto = async (req, res = response) => { | ||
const { id } = req.params; | ||
|
||
const producto = await Producto.findByIdAndUpdate( | ||
id, | ||
{ estado: false }, | ||
{ new: true } | ||
); | ||
|
||
res.status(200).json(producto); | ||
}; | ||
|
||
module.exports = { | ||
obtenerProducto, | ||
obtenerProductos, | ||
crearProducto, | ||
actualizarProducto, | ||
borrarProducto, | ||
}; |
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,26 @@ | ||
const { Schema, model } = require("mongoose"); | ||
|
||
const CategoriaSchema = Schema({ | ||
nombre: { | ||
type: String, | ||
required: [true, "El nombre es obligatorio"], | ||
unique: true, | ||
}, | ||
estado: { | ||
type: Boolean, | ||
default: true, | ||
required: true, | ||
}, | ||
usuario: { | ||
type: Schema.Types.ObjectId, | ||
ref: "Usuario", | ||
required: true, | ||
}, | ||
}); | ||
|
||
CategoriaSchema.methods.toJSON = function () { | ||
const { __v, estado, ...data } = this.toObject(); | ||
return data; | ||
}; | ||
|
||
module.exports = 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,13 @@ | ||
const Categoria = require("./categoria"); | ||
const Producto = require("./producto"); | ||
const Role = require("./role"); | ||
const Server = require("./server"); | ||
const Usuario = require("./usuario"); | ||
|
||
module.exports = { | ||
Categoria, | ||
Producto, | ||
Role, | ||
Server, | ||
Usuario, | ||
}; |
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,37 @@ | ||
const { Schema, model } = require("mongoose"); | ||
|
||
const ProductoSchema = Schema({ | ||
nombre: { | ||
type: String, | ||
required: [true, "El nombre es obligatorio"], | ||
unique: true, | ||
}, | ||
estado: { | ||
type: Boolean, | ||
default: true, | ||
required: true, | ||
}, | ||
usuario: { | ||
type: Schema.Types.ObjectId, | ||
ref: "Usuario", | ||
required: true, | ||
}, | ||
precio: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
categoria: { | ||
type: Schema.Types.ObjectId, | ||
ref: "Categoria", | ||
required: true, | ||
}, | ||
descripcion: { type: String }, | ||
disponible: { type: Boolean, default: true }, | ||
}); | ||
|
||
ProductoSchema.methods.toJSON = function () { | ||
const { __v, estado, ...data } = this.toObject(); | ||
return data; | ||
}; | ||
|
||
module.exports = model("Producto", ProductoSchema); |
Oops, something went wrong.