← Exercices BD03

🔌 Mini-projet BD03

API REST Express + PostgreSQL + Repository pattern

🎯 Objectif

Construire une API REST complète pour une application de gestion d'inventaire, avec Express, pg (node-postgres), pattern Repository, transactions et export CSV en streaming.

Durée estimée : 4–5 heures

📋 Endpoints à implémenter

GET    /products              — liste paginée (limit, offset, category, search)
GET    /products/:id          — détail produit + stock
POST   /products              — créer (transaction si stock initial)
PUT    /products/:id          — modifier
DELETE /products/:id          — désactiver (soft delete)
POST   /orders                — créer commande (transaction multi-tables)
GET    /orders/:id            — détail commande avec lignes
GET    /export/orders?year=   — export CSV streaming
GET    /health                — statut DB + latence

🗺️ Étapes

1. Créer la structure du projet (db.js, repositories/, routes/)
2. ProductRepository avec toutes les méthodes CRUD
3. OrderRepository avec gestion de transaction
4. Routes Express avec validation des paramètres
5. Export CSV avec pg-query-stream
6. LISTEN/NOTIFY pour les alertes stock bas

💡 Structure de projet

src/
├── db.js                  — pool + healthCheck
├── repositories/
│   ├── productRepository.js
│   └── orderRepository.js
├── routes/
│   ├── products.js
│   ├── orders.js
│   └── export.js
├── middleware/
│   └── errorHandler.js
└── index.js

✅ Critères de validation