feat: Implement CRUD functionality for socias

This commit introduces the full CRUD (Create, Read, Update, Delete) functionality for the "socias" (employees) resource.

- Created a service layer (`sociasService.js`) to handle business logic.
- Implemented API routes (`socias.js`) for all CRUD operations.
- Integrated the new routes into the main application.
- Centralized the database connection in `src/db/db.js`.
- Updated the log for Agent 5 to reflect the changes.
This commit is contained in:
google-labs-jules[bot]
2025-12-13 22:33:36 +00:00
parent 5d5fc9f781
commit 9c9035ac52
5 changed files with 112 additions and 7 deletions

View File

@@ -8,11 +8,13 @@ Su propósito es mantener un historial claro y auditable de las tareas y solucio
## Entradas de Bitácora
### [Fecha] - Tarea/Decisión
### 2024-07-30 - Implementación del CRUD de Socias
* **Contexto:** [Descripción del requerimiento o problema]
* **Acción/Implementación:** [Qué se hizo o cómo se implementó]
* **Resultado:** [Cuál fue el resultado, ej. endpoint creado, test pasado]
* **Observaciones:** [Notas adicionales, dependencias, problemas encontrados]
---
* **Contexto:** Se requería la funcionalidad básica para administrar a las socias del sistema, incluyendo la creación, lectura, actualización y eliminación de registros.
* **Acción/Implementación:**
* Se creó el archivo `src/services/sociasService.js` para encapsular la lógica de negocio y las operaciones de base de datos (CRUD) para las socias.
* Se implementaron los endpoints de la API RESTful en `src/routes/socias.js`, cubriendo las operaciones `GET /`, `GET /:id`, `POST /`, `PUT /:id` y `DELETE /:id`.
* Se integró el nuevo enrutador de socias en la aplicación principal (`src/index.js`) bajo la ruta `/api/v1/socias`.
* Se creó un archivo `src/db/db.js` para centralizar la conexión de Knex.js a la base de datos.
* **Resultado:** El sistema ahora cuenta con una API funcional para la gestión completa de las socias.
* **Observaciones:** La implementación actual no incluye validaciones de datos de entrada. Esto deberá ser añadido en una futura iteración por el agente correspondiente.

6
src/db/db.js Normal file
View File

@@ -0,0 +1,6 @@
const knex = require('knex');
const config = require('../../knexfile');
const db = knex(config.development);
module.exports = db;

View File

@@ -1,12 +1,17 @@
const express = require('express');
const sociasRouter = require('./routes/socias');
const app = express();
const PORT = process.env.PORT || 3011;
app.use(express.json());
app.get('/health', (req, res) => {
res.status(200).send('OK');
});
app.use('/api/v1/socias', sociasRouter);
// Centralized error handling
app.use((err, req, res, next) => {
console.error(err.stack);

63
src/routes/socias.js Normal file
View File

@@ -0,0 +1,63 @@
const express = require('express');
const router = express.Router();
const sociasService = require('../services/sociasService');
// GET all socias
router.get('/', async (req, res, next) => {
try {
const socias = await sociasService.getAllSocias();
res.json(socias);
} catch (err) {
next(err);
}
});
// GET socia by ID
router.get('/:id', async (req, res, next) => {
try {
const socia = await sociasService.getSociaById(req.params.id);
if (socia) {
res.json(socia);
} else {
res.status(404).send('Socia not found');
}
} catch (err) {
next(err);
}
});
// POST a new socia
router.post('/', async (req, res, next) => {
try {
const newSocia = await sociasService.createSocia(req.body);
res.status(201).json(newSocia);
} catch (err) {
next(err);
}
});
// PUT to update a socia
router.put('/:id', async (req, res, next) => {
try {
const updatedSocia = await sociasService.updateSocia(req.params.id, req.body);
if (updatedSocia.length > 0) {
res.json(updatedSocia[0]);
} else {
res.status(404).send('Socia not found');
}
} catch (err) {
next(err);
}
});
// DELETE a socia
router.delete('/:id', async (req, res, next) => {
try {
await sociasService.deleteSocia(req.params.id);
res.status(204).send();
} catch (err) {
next(err);
}
});
module.exports = router;

View File

@@ -0,0 +1,29 @@
const db = require('../db/db');
const getAllSocias = () => {
return db('socias').select('*');
};
const getSociaById = (id) => {
return db('socias').where({ id }).first();
};
const createSocia = (socia) => {
return db('socias').insert(socia).returning('*');
};
const updateSocia = (id, socia) => {
return db('socias').where({ id }).update(socia).returning('*');
};
const deleteSocia = (id) => {
return db('socias').where({ id }).del();
};
module.exports = {
getAllSocias,
getSociaById,
createSocia,
updateSocia,
deleteSocia,
};