2026-03-22 22:36:00 +00:00
|
|
|
import { Router } from 'express';
|
|
|
|
|
import { ProtocolController } from '../controllers/ProtocolController';
|
|
|
|
|
import { GetAllProtocolsUseCase } from '../../../application/usecases/GetAllProtocolsUseCase';
|
|
|
|
|
import { GetProtocolByIdUseCase } from '../../../application/usecases/GetProtocolByIdUseCase';
|
2026-03-25 10:59:07 +00:00
|
|
|
import { MongoProtocolRepository } from '../../persistence/MongoProtocolRepository';
|
2026-03-22 22:36:00 +00:00
|
|
|
|
|
|
|
|
// manual dependency injection for routes
|
2026-03-25 10:59:07 +00:00
|
|
|
const repository = new MongoProtocolRepository();
|
2026-03-22 22:36:00 +00:00
|
|
|
const getAllUseCase = new GetAllProtocolsUseCase(repository);
|
|
|
|
|
const getByIdUseCase = new GetProtocolByIdUseCase(repository);
|
|
|
|
|
const controller = new ProtocolController(getAllUseCase, getByIdUseCase);
|
|
|
|
|
|
|
|
|
|
const router = Router();
|
|
|
|
|
|
|
|
|
|
router.get('/', controller.getAll);
|
|
|
|
|
router.get('/:id', controller.getById);
|
|
|
|
|
|
2026-03-25 10:59:07 +00:00
|
|
|
// CRUD endpoints for Admin
|
|
|
|
|
router.post('/', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
await repository.save(req.body);
|
|
|
|
|
res.status(201).json({ message: 'Protocolo creado correctamente' });
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
2026-03-22 22:36:00 +00:00
|
|
|
});
|
|
|
|
|
|
2026-03-25 10:59:07 +00:00
|
|
|
router.put('/:id', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
await repository.save({ ...req.body, id: req.params.id });
|
|
|
|
|
res.json({ message: 'Protocolo actualizado correctamente' });
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
2026-03-22 22:36:00 +00:00
|
|
|
});
|
|
|
|
|
|
2026-03-25 10:59:07 +00:00
|
|
|
router.delete('/:id', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
await repository.delete(req.params.id);
|
|
|
|
|
res.json({ message: 'Protocolo eliminado correctamente' });
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
}
|
2026-03-22 22:36:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default router;
|