Développement logiciel avec Claude Code
Dani Zysman
AI Curriculum Manager @ DataCamp

music-analytics-api/
app.py # API existante (mise à jour pour enregistrer db_routes)
routes.py # Routes existantes
models.py # Modèles Artist/Album
db_routes.py # <- Nouveau Blueprint connecté à la base
chinook.db
test_routes.py
CLAUDE.md
> Create a new db_routes.py blueprint with an endpoint at
/artists/<artist_id>/albums that returns all albums by
a specific artist as JSON from chinook,db. Register it with @app.py
1. db_routes py - Nouveau Blueprint Flask avec :
- helper get_db_connection() pour se connecter à chinook.db
- Endpoint ajouté qui interroge la base pour tous les albums d'un artiste donné
- Renvoie du JSON avec les champs AlbumId, Title et ArtistId
2. app.py mis à jour
- Blueprint enregistré pour activer le nouvel endpoint
L'endpoint renvoie 404 si l'artiste n'existe pas,
et renvoie les albums triés par titre en JSON.
@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": []}from pydantic import BaseModel
from typing import List
class Album(BaseModel):
id: int
title: str
class ArtistAlbums(BaseModel):
artist_id: int
albums: List[Album]
> Add Pydantic validation to @db_routes.py. Return 404 with a message if the
artist doesn't exist or has no albums.
Terminé ! Validation Pydantic ajoutée à db_routes.py :
Modifications :
- Ajout du modèle Album (album_id, title, artist_id)
- Renvoie 404 "Artist not found" si l'artiste n'existe pas
- Renvoie 404 "Artist has no albums" si l'artiste n'a aucun album
- Valide chaque album via le modèle Album avant retour
- Renvoie 400 en cas d'erreurs de validation
Réponses :
- 404 "Artist not found"
- 404 "Artist has no albums"
- 200 avec données d'album validées
Développement logiciel avec Claude Code