Softwareentwicklung mit Claude Code
Dani Zysman
AI Curriculum Manager @ DataCamp

music-analytics-api/
app.py # Bestehende API (aktualisiert, um db_routes zu registrieren)
routes.py # Bestehende Routen
models.py # Artist-/Album-Modelle
db_routes.py # <- Neuer, datenbankgestützter Blueprint
chinook.db
test_routes.py
CLAUDE.md
> Erstelle einen neuen Blueprint db_routes.py mit einem Endpoint unter
/artists/<artist_id>/albums, der alle Alben eines
bestimmten Künstlers als JSON aus chinook,db zurückgibt. Registriere ihn in @app.py
1. db_routes py – Neuer Flask-Blueprint mit:
- get_db_connection()-Helper, um chinook.db zu verbinden
- Hinzugefügtem Endpoint, der alle Alben eines bestimmten Künstlers aus der DB abfragt
- Gibt JSON mit den Feldern AlbumId, Title und ArtistId zurück
2. Aktualisierte app.py
- Blueprint registriert, damit der neue Endpoint aktiv ist
Der Endpoint gibt 404 zurück, wenn der Künstler nicht existiert,
und liefert Alben als JSON, nach Titel sortiert.
@app.route("/artists/<int:artist_id>/albums")
def get_artist_albums(artist_id):
conn = get_db()
artist = conn.execute("SELECT ... WHERE ArtistId = ?", ...)
albums = conn.execute("SELECT ... WHERE ArtistId = ?", ...)
conn.close()
return jsonify({"artist": ..., "albums": [...]})
$ python app.py & PID=$! && sleep 3 && curl http://127.0.0.1:5000/artists/1/albums
&& kill $PID
127.0.0.1 "GET /artists/1/albums HTTP/1.1" 200
[
{
"album_id": 1,
"artist_id": 1,
"title": "For Those About To Rock We Salute You"
},
{
"album_id": 4,
"artist_id": 1,
"title": "Let There Be Rock"
}
]
{"albums": []} zurückfrom pydantic import BaseModel
from typing import List
class Album(BaseModel):
id: int
title: str
class ArtistAlbums(BaseModel):
artist_id: int
albums: List[Album]
> Füge Pydantic-Validierung zu @db_routes.py hinzu. Gib 404 mit Meldung zurück, wenn der
Künstler nicht existiert oder keine Alben hat.
Fertig! Pydantic-Validierung zu db_routes.py hinzugefügt:
Änderungen:
- Album-Modell (album_id, title, artist_id) ergänzt
- Gibt 404 "Artist not found" zurück, wenn Künstler nicht existiert
- Gibt 404 "Artist has no albums" zurück, wenn Künstler keine Alben hat
- Validiert jedes Album gegen das Album-Modell vor der Rückgabe
- Gibt 400 bei Validierungsfehlern zurück
Antworten:
- 404 "Artist not found"
- 404 "Artist has no albums"
- 200 mit validierten Albumdaten
Softwareentwicklung mit Claude Code