82 lines
2.3 KiB
Markdown
82 lines
2.3 KiB
Markdown
|
|
# Refactor
|
|||
|
|
|
|||
|
|
Refactore den Code ohne die Funktionalität zu ändern.
|
|||
|
|
|
|||
|
|
## Wichtige Regel:
|
|||
|
|
**Erst planen, dann umsetzen – niemals beides gleichzeitig.**
|
|||
|
|
Zeige den Plan und warte auf Bestätigung bevor du Code schreibst.
|
|||
|
|
|
|||
|
|
## Backend Refactoring (main.py aufteilen):
|
|||
|
|
|
|||
|
|
### Zielstruktur:
|
|||
|
|
```
|
|||
|
|
backend/
|
|||
|
|
├── main.py # Nur App-Setup, Middleware, Router-Import (~100 Zeilen)
|
|||
|
|
├── database.py # DB-Verbindung, init_db, safe_alters, r2d()
|
|||
|
|
├── auth.py # require_auth, require_admin, hash_pin, verify_pin, sessions
|
|||
|
|
├── models.py # Alle Pydantic Models
|
|||
|
|
├── email.py # SMTP, send_email, email_html_wrapper
|
|||
|
|
├── utils.py # make_token(), helper functions
|
|||
|
|
└── routers/
|
|||
|
|
├── __init__.py
|
|||
|
|
├── weight.py
|
|||
|
|
├── caliper.py
|
|||
|
|
├── circumference.py
|
|||
|
|
├── nutrition.py
|
|||
|
|
├── activity.py
|
|||
|
|
├── photos.py
|
|||
|
|
├── insights.py
|
|||
|
|
├── prompts.py
|
|||
|
|
├── admin.py
|
|||
|
|
└── export.py
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Vorgehen:
|
|||
|
|
1. Analysiere main.py vollständig
|
|||
|
|
2. Erstelle Plan welche Zeilen wohin kommen
|
|||
|
|
3. Warte auf Bestätigung
|
|||
|
|
4. Erstelle neue Dateien eine nach der anderen
|
|||
|
|
5. Nach jeder Datei: Syntax prüfen
|
|||
|
|
6. main.py anpassen (imports + router registrierung)
|
|||
|
|
7. Finaler Test: Backend startet ohne Fehler
|
|||
|
|
|
|||
|
|
### Syntax-Check nach jedem Schritt:
|
|||
|
|
```bash
|
|||
|
|
python3 -m py_compile backend/main.py
|
|||
|
|
python3 -m py_compile backend/database.py
|
|||
|
|
# etc.
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Test nach Refactoring:
|
|||
|
|
```bash
|
|||
|
|
# Auf dem Pi:
|
|||
|
|
docker compose build --no-cache backend
|
|||
|
|
docker compose up -d
|
|||
|
|
docker logs mitai-api --tail 20
|
|||
|
|
curl -s http://localhost:8002/api/auth/status
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## Frontend Refactoring (Komponenten extrahieren):
|
|||
|
|
|
|||
|
|
### Wiederverwendbare Komponenten extrahieren nach:
|
|||
|
|
```
|
|||
|
|
frontend/src/components/
|
|||
|
|
├── MetricCard.jsx # Kennzahl-Karte
|
|||
|
|
├── ChartCombo.jsx # Kombinations-Chart
|
|||
|
|
├── LoadingSpinner.jsx # Ladekreis
|
|||
|
|
├── EmptyState.jsx # Leerer Zustand
|
|||
|
|
├── Avatar.jsx # Nutzer-Avatar
|
|||
|
|
└── ConfirmDialog.jsx # Bestätigungs-Dialog
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Vorgehen:
|
|||
|
|
1. Identifiziere wiederholte Code-Muster
|
|||
|
|
2. Extrahiere in eigene Komponenten
|
|||
|
|
3. Ersetze in allen Pages
|
|||
|
|
4. Frontend Build testen: `cd frontend && npm run build`
|
|||
|
|
|
|||
|
|
## Nach dem Refactoring:
|
|||
|
|
- Commit mit `refactor:` Prefix
|
|||
|
|
- CLAUDE.md Verzeichnisstruktur aktualisieren
|
|||
|
|
- Kein neues Feature in diesem Commit!
|