Implementación de un CRUD para sistema de inventario
npm init -y
npm install express sequelize mysql2 cors jsonwebtoken
npm install swagger-jsdoc swagger-ui-express dotenv
const { Sequelize } = require('sequelize');
require('dotenv').config();
const sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USER,
process.env.DB_PASSWORD,
{
host: process.env.DB_HOST,
dialect: 'mysql',
logging: false
}
);
module.exports = sequelize;
const { DataTypes } = require('sequelize');
const sequelize = require('../config/db');
const Producto = sequelize.define('Producto', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
nombre: {
type: DataTypes.STRING,
allowNull: false
},
descripcion: {
type: DataTypes.TEXT
},
precio: {
type: DataTypes.FLOAT,
allowNull: false
},
stock: {
type: DataTypes.INTEGER,
defaultValue: 0
}
}, {
timestamps: true
});
module.exports = Producto;
const express = require('express');
const router = express.Router();
const Producto = require('../models/Producto');
const authMiddleware = require('../middlewares/auth');
// Obtener todos los productos
router.get('/', async (req, res) => {
try {
const productos = await Producto.findAll();
res.json(productos);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Crear producto (protegido por JWT)
router.post('/', authMiddleware, async (req, res) => {
try {
const producto = await Producto.create(req.body);
res.status(201).json(producto);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
module.exports = router;
const jwt = require('jsonwebtoken');
require('dotenv').config();
module.exports = (req, res, next) => {
const token = req.header('Authorization')?.replace('Bearer ', '');
if (!token) {
return res.status(401).json({ error: 'Acceso no autorizado' });
}
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET);
req.user = decoded;
next();
} catch (error) {
res.status(401).json({ error: 'Token inválido' });
}
};
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'API de Inventario',
version: '1.0.0',
description: 'Documentación del CRUD de productos',
},
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
}
}
},
security: [{ bearerAuth: [] }]
},
apis: ['./routes/*.js'],
};
const specs = swaggerJsdoc(swaggerOptions);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
/**
* @swagger
* /api/productos:
* get:
* summary: Obtener todos los productos
* tags: [Productos]
* responses:
* 200:
* description: Lista de productos
* content:
* application/json:
* schema:
* type: array
* items:
* $ref: '#/components/schemas/Producto'
*/