Skip to content

Commit 8d06abf

Browse files
committed
chore: initial commit
0 parents  commit 8d06abf

38 files changed

Lines changed: 4727 additions & 0 deletions

.env.example

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Educational FastAPI Notes App - Environment Configuration
2+
# Copy this file to .env and adjust values for your environment
3+
4+
# Database Configuration
5+
DATABASE_URL=postgresql://transferdemo:transferdemo@localhost:5432/transferdemo
6+
7+
# For SQLite (development/testing)
8+
# DATABASE_URL=sqlite:///./notes.db
9+
10+
# Application Configuration
11+
DEBUG=true
12+
SECRET_KEY=dev-secret-key-change-in-production
13+
14+
# Server Configuration
15+
HOST=0.0.0.0
16+
PORT=8000
17+
18+
# Logging Configuration
19+
LOG_LEVEL=INFO
20+
21+
# CORS Configuration (for frontend development)
22+
CORS_ORIGINS=["http://localhost:3000", "http://localhost:8080", "http://127.0.0.1:8000"]
23+
24+
# Database Pool Configuration
25+
DB_POOL_SIZE=5
26+
DB_MAX_OVERFLOW=10

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Python-generated files
2+
__pycache__/
3+
*.py[oc]
4+
build/
5+
dist/
6+
wheels/
7+
*.egg-info
8+
9+
# Virtual environments
10+
.venv
11+
/.env
12+
.coverage
13+
htmlcov/

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.13

README.md

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
# Notes Demo - Python ORM Lernprojekt
2+
3+
[![FastAPI](https://img.shields.io/badge/FastAPI-0.104+-009688.svg)](https://fastapi.tiangolo.com)
4+
[![Python](https://img.shields.io/badge/Python-3.11+-blue.svg)](https://www.python.org/)
5+
[![PostgreSQL](https://img.shields.io/badge/PostgreSQL-17-blue.svg)](https://www.postgresql.org/)
6+
[![UV](https://img.shields.io/badge/UV-Package%20Manager-purple.svg)](https://docs.astral.sh/uv/)
7+
8+
Ein umfassendes Lernprojekt für den Kurs **"Relationale Datenbanken"** der [Höheren Fachschule für Technik Mittelland](https://hftm.ch) zur praktischen Demonstration von Object Relational Mapping (ORM) mit modernen Python-Technologien.
9+
10+
🔗 **[Kurs-Website](https://relational-databases.erhardt.consulting/)**
11+
12+
## 📚 Lernziele
13+
14+
Dieses Projekt vermittelt Studierenden folgende Kernkonzepte:
15+
16+
- **Object Relational Mapping (ORM)** mit SQLAlchemy
17+
- **FastAPI Fundamentals** - Asynchrone APIs, Dependency Injection, Auto-Dokumentation
18+
- **Clean Architecture** - Separation of Concerns zwischen Model-, Schema-, Service- und Router-Schichten
19+
- **Datenbankmanagement** - PostgreSQL Integration, Alembic Migrationen
20+
- **Containerisierte Entwicklung** - Docker Compose für lokale Umgebung
21+
- **Type-Safe Development** - Pydantic für sichere Datenvalidierung
22+
23+
## 🛠️ Technologie-Stack
24+
25+
### Backend
26+
- **FastAPI** - Modernes, schnelles Web-Framework
27+
- **Python 3.11+** - Moderne Python-Features und Performance
28+
- **SQLAlchemy 2.0** - Object Relational Mapping
29+
- **PostgreSQL 17** - Relationale Datenbank
30+
- **Alembic** - Datenbankmigrationen
31+
32+
### Frontend
33+
- **Jinja2** - Server-side HTML Templating
34+
- **Bootstrap CSS** - Responsive Web Design
35+
36+
### Entwicklung & Testing
37+
- **UV** - Blitzschnelles Package Management
38+
- **Pytest** - Testing Framework
39+
- **Docker Compose** - Lokale Entwicklungsumgebung
40+
41+
## 🚀 Installation und Einrichtung
42+
43+
### Voraussetzungen
44+
45+
- **Python 3.11** oder höher ([Python.org](https://www.python.org/))
46+
- **UV** für Package Management ([Installation](https://docs.astral.sh/uv/))
47+
- **Docker & Docker Compose** für die Datenbankumgebung
48+
- **Git** für Versionskontrolle
49+
50+
### 1. Projekt klonen
51+
52+
```bash
53+
git clone <repository-url>
54+
cd notes-app
55+
```
56+
57+
### 2. Datenbank starten
58+
59+
```bash
60+
# PostgreSQL Datenbank mit Docker Compose starten
61+
docker-compose -f docker-compose.db.yaml up -d
62+
63+
# Überprüfen, ob die Datenbank läuft
64+
docker ps
65+
```
66+
67+
### 3. Anwendung kompilieren und starten
68+
69+
```bash
70+
# Dependencies mit UV installieren
71+
uv sync
72+
73+
# Umgebungsvariablen konfigurieren
74+
cp .env.example .env
75+
76+
# Anwendung starten
77+
uv run python -m app.main
78+
79+
# Alternative: Mit uvicorn direkt
80+
uv run uvicorn app.main:app --reload
81+
```
82+
83+
### 4. Anwendung testen
84+
85+
- **Webanwendung:** http://localhost:8000
86+
- **API Dokumentation:** http://localhost:8000/docs
87+
- **Datenbank:** PostgreSQL auf localhost:5432
88+
- Database: `notesapp`
89+
- Username: `notesapp`
90+
- Password: `notesapp`
91+
92+
## 🏗️ Projektarchitektur
93+
94+
Das Projekt folgt einer **Layered Architecture** mit klarer Trennung der Verantwortlichkeiten:
95+
96+
```
97+
app/
98+
├── routers/ # 🌐 Presentation Layer
99+
│ ├── web.py # Web UI Endpoints
100+
│ └── api.py # REST API Endpoints
101+
├── services/ # 💼 Business Logic Layer
102+
│ └── note_service.py # Geschäftslogik & Datenbankoperationen
103+
├── schemas/ # 📋 Data Transfer Objects
104+
│ └── note.py # Pydantic Schemas für Validierung
105+
├── models/ # 💾 Data Model Layer
106+
│ └── note.py # SQLAlchemy ORM Models
107+
├── database.py # 🔧 Datenbankverbindung
108+
├── config.py # ⚙️ Konfigurationsmanagement
109+
└── main.py # 🚀 Application Factory
110+
```
111+
112+
### 🔄 Datenfluss
113+
114+
```
115+
HTTP Request → Router → Service → SQLAlchemy → Database
116+
117+
Schema/DTO ← Pydantic ← Model ← SQLAlchemy
118+
```
119+
120+
## 💡 Kernkonzepte und Patterns
121+
122+
### 1. **ORM Model Definition**
123+
```python
124+
# SQLAlchemy Model (Datenbankrepräsentation)
125+
class Note(Base):
126+
__tablename__ = "notes"
127+
128+
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
129+
title = Column(String(100), nullable=False)
130+
content = Column(Text, nullable=False)
131+
created_at = Column(DateTime, server_default=func.now())
132+
updated_at = Column(DateTime, onupdate=func.now())
133+
```
134+
135+
### 2. **Schema Validation mit Pydantic**
136+
```python
137+
# Pydantic Schema für Datenvalidierung
138+
class NoteCreate(BaseModel):
139+
title: str = Field(..., min_length=1, max_length=100)
140+
content: str = Field(..., min_length=1)
141+
142+
class NoteResponse(BaseModel):
143+
id: UUID
144+
title: str
145+
content: str
146+
created_at: datetime
147+
updated_at: Optional[datetime]
148+
149+
model_config = ConfigDict(from_attributes=True)
150+
```
151+
152+
### 3. **Service Layer Pattern**
153+
```python
154+
class NoteService:
155+
def __init__(self, db: Session = Depends(get_db)):
156+
self.db = db
157+
158+
async def create_note(self, note_data: NoteCreate) -> Note:
159+
note = Note(**note_data.model_dump())
160+
self.db.add(note)
161+
self.db.commit()
162+
self.db.refresh(note)
163+
return note
164+
```
165+
166+
### 4. **Dependency Injection**
167+
```python
168+
@router.post("/notes", response_model=NoteResponse)
169+
async def create_note(
170+
note_data: NoteCreate,
171+
service: NoteService = Depends()
172+
):
173+
return await service.create_note(note_data)
174+
```
175+
176+
## 🧪 Testing
177+
178+
### Unit Tests ausführen
179+
```bash
180+
uv run pytest tests/unit -v
181+
```
182+
183+
### Integration Tests ausführen
184+
```bash
185+
uv run pytest tests/integration -v
186+
```
187+
188+
### Test Coverage Report
189+
```bash
190+
uv run pytest --cov=app --cov-report=html
191+
# Report verfügbar unter: htmlcov/index.html
192+
```
193+
194+
## 🎯 Pädagogische Übungen
195+
196+
### Grundübungen
197+
1. **CRUD Operationen verstehen** - Analysiere die vollständigen Create/Read/Update/Delete Workflows
198+
2. **Schema-Validierung erkunden** - Untersuche, wie Pydantic Eingabedaten validiert
199+
3. **ORM-Queries schreiben** - Verstehe SQLAlchemy Query-Syntax und Filteroperationen
200+
201+
### Erweiterungsübungen
202+
1. **Neue Entität hinzufügen** - Erstelle eine `Tag` Entität mit Many-to-Many Beziehung zu `Note`
203+
2. **Erweiterte Validierung** - Füge Custom Validators in Pydantic Schemas hinzu
204+
3. **Suchoption einbauen** - Implementiere Volltextsuche in Notizen
205+
4. **Pagination hinzufügen** - Erweitere die API um Seitennummerierung
206+
207+
### Fortgeschrittene Übungen
208+
1. **Asynchrone Datenbankoperationen** - Migriere zu async SQLAlchemy
209+
2. **Caching implementieren** - Füge Redis-basiertes Caching hinzu
210+
3. **Background Tasks** - Implementiere asynchrone Aufgaben mit Celery
211+
4. **API Versionierung** - Erstelle versionierte API Endpoints
212+
213+
## 📁 Wichtige Dateien
214+
215+
| Datei | Zweck | Lernfokus |
216+
|-------|-------|-----------|
217+
| `models/note.py` | SQLAlchemy Model | ORM Mapping, Relationships |
218+
| `schemas/note.py` | Pydantic Schemas | Datenvalidierung, Serialisierung |
219+
| `services/note_service.py` | Business Logic | Service Pattern, Transaktionen |
220+
| `routers/api.py` | REST API | FastAPI Routing, Dependencies |
221+
| `routers/web.py` | Web UI | Template Rendering, Forms |
222+
| `config.py` | Konfiguration | Settings Management |
223+
| `alembic/` | Migrationen | Datenbankevolution |
224+
225+
## 🐳 Docker Entwicklungsumgebung
226+
227+
Das Projekt nutzt Docker Compose für eine konsistente Entwicklungsumgebung:
228+
229+
```yaml
230+
# docker-compose.db.yaml
231+
services:
232+
postgres:
233+
image: postgres:17
234+
environment:
235+
POSTGRES_DB: notesapp
236+
POSTGRES_USER: notesapp
237+
POSTGRES_PASSWORD: notesapp
238+
ports:
239+
- "5432:5432"
240+
volumes:
241+
- postgres_data:/var/lib/postgresql/data
242+
```
243+
244+
### Nützliche Docker Befehle
245+
```bash
246+
# Datenbank starten
247+
docker-compose -f docker-compose.db.yaml up -d
248+
249+
# Logs anzeigen
250+
docker-compose -f docker-compose.db.yaml logs -f
251+
252+
# Datenbank stoppen
253+
docker-compose -f docker-compose.db.yaml down
254+
255+
# Datenbank zurücksetzen
256+
docker-compose -f docker-compose.db.yaml down -v
257+
```
258+
259+
## 📖 Zusätzliche Ressourcen
260+
261+
### Dokumentation
262+
- [FastAPI Dokumentation](https://fastapi.tiangolo.com/)
263+
- [SQLAlchemy 2.0 Tutorial](https://docs.sqlalchemy.org/en/20/tutorial/)
264+
- [Pydantic Dokumentation](https://docs.pydantic.dev/)
265+
- [UV Package Manager](https://docs.astral.sh/uv/)
266+
267+
### Lernmaterialien
268+
- [Kurs: Relationale Datenbanken](https://relational-databases.erhardt.consulting/)
269+
- [Python Async Programming](https://realpython.com/async-io-python/)
270+
- [ORM Patterns in Python](https://www.sqlalchemy.org/library.html)
271+
272+
## 🤝 Beitragen
273+
274+
Dieses Projekt dient Bildungszwecken. Verbesserungsvorschläge und Erweiterungen sind willkommen:
275+
276+
1. Fork des Repositories erstellen
277+
2. Feature Branch erstellen (`git checkout -b feature/neue-funktion`)
278+
3. Änderungen committen (`git commit -am 'Füge neue Funktion hinzu'`)
279+
4. Branch pushen (`git push origin feature/neue-funktion`)
280+
5. Pull Request erstellen
281+
282+
## 📄 Lizenz
283+
284+
Dieses Projekt steht unter der MIT-Lizenz und dient ausschliesslich Bildungszwecken im Rahmen des Kurses "Relationale Datenbanken" der Höheren Fachschule für Technik Mittelland.

0 commit comments

Comments
 (0)