126 lines
4.8 KiB
Python
126 lines
4.8 KiB
Python
|
|
"""Routes for tune_by_instrument entries and their notes."""
|
||
|
|
from flask import Blueprint, jsonify, request
|
||
|
|
from app.extensions import db
|
||
|
|
from app.models import Tune, Instrument, TuneByInstrument, TuneByInstrumentNote
|
||
|
|
from app.auth import require_api_key
|
||
|
|
|
||
|
|
instruments_bp = Blueprint("instruments", __name__)
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
# Instruments lookup (public)
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
@instruments_bp.get("/instruments")
|
||
|
|
def list_instruments():
|
||
|
|
return jsonify([i.to_dict() for i in
|
||
|
|
db.session.query(Instrument).order_by(Instrument.name).all()])
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
# List all tune_by_instrument entries for a tune
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
@instruments_bp.get("/tunes/<int:tune_id>/instruments")
|
||
|
|
def list_tune_instruments(tune_id):
|
||
|
|
db.get_or_404(Tune, tune_id)
|
||
|
|
entries = db.session.query(TuneByInstrument).filter_by(tune_id=tune_id).all()
|
||
|
|
return jsonify([e.to_dict() for e in entries])
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
# Create a tune_by_instrument entry
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
@instruments_bp.post("/tunes/<int:tune_id>/instruments")
|
||
|
|
@require_api_key
|
||
|
|
def create_tune_instrument(tune_id):
|
||
|
|
db.get_or_404(Tune, tune_id)
|
||
|
|
data = request.get_json(force=True)
|
||
|
|
|
||
|
|
# Validate instrument exists
|
||
|
|
instrument_id = data.get("instrument_id")
|
||
|
|
db.get_or_404(Instrument, instrument_id)
|
||
|
|
|
||
|
|
entry = TuneByInstrument(
|
||
|
|
tune_id=tune_id,
|
||
|
|
instrument_id=instrument_id,
|
||
|
|
learned_from_id=data.get("learned_from_id"),
|
||
|
|
tuning_id=data.get("tuning_id"),
|
||
|
|
date_learned=data.get("date_learned"),
|
||
|
|
callable=data.get("callable"),
|
||
|
|
review=data.get("review"),
|
||
|
|
to_learn=data.get("to_learn", False),
|
||
|
|
difficulty=data.get("difficulty"),
|
||
|
|
)
|
||
|
|
db.session.add(entry)
|
||
|
|
db.session.commit()
|
||
|
|
return jsonify(entry.to_dict()), 201
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
# Update a tune_by_instrument entry
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
@instruments_bp.patch("/tunes/<int:tune_id>/instruments/<int:entry_id>")
|
||
|
|
@require_api_key
|
||
|
|
def update_tune_instrument(tune_id, entry_id):
|
||
|
|
entry = db.get_or_404(TuneByInstrument, entry_id)
|
||
|
|
data = request.get_json(force=True)
|
||
|
|
fields = ("instrument_id", "learned_from_id", "tuning_id", "date_learned",
|
||
|
|
"callable", "review", "to_learn", "difficulty")
|
||
|
|
for field in fields:
|
||
|
|
if field in data:
|
||
|
|
setattr(entry, field, data[field])
|
||
|
|
db.session.commit()
|
||
|
|
return jsonify(entry.to_dict())
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
# Delete a tune_by_instrument entry
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
@instruments_bp.delete("/tunes/<int:tune_id>/instruments/<int:entry_id>")
|
||
|
|
@require_api_key
|
||
|
|
def delete_tune_instrument(tune_id, entry_id):
|
||
|
|
entry = db.get_or_404(TuneByInstrument, entry_id)
|
||
|
|
db.session.delete(entry)
|
||
|
|
db.session.commit()
|
||
|
|
return jsonify({"deleted": entry_id})
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
# Notes on a tune_by_instrument entry
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
@instruments_bp.get("/tunes/<int:tune_id>/instruments/<int:entry_id>/notes")
|
||
|
|
def list_instrument_notes(tune_id, entry_id):
|
||
|
|
db.get_or_404(TuneByInstrument, entry_id)
|
||
|
|
notes = db.session.query(TuneByInstrumentNote).filter_by(tune_by_instrument_id=entry_id).all()
|
||
|
|
return jsonify([n.to_dict() for n in notes])
|
||
|
|
|
||
|
|
|
||
|
|
@instruments_bp.post("/tunes/<int:tune_id>/instruments/<int:entry_id>/notes")
|
||
|
|
@require_api_key
|
||
|
|
def add_instrument_note(tune_id, entry_id):
|
||
|
|
db.get_or_404(TuneByInstrument, entry_id)
|
||
|
|
data = request.get_json(force=True)
|
||
|
|
note = TuneByInstrumentNote(tune_by_instrument_id=entry_id, note=data.get("note"))
|
||
|
|
db.session.add(note)
|
||
|
|
db.session.commit()
|
||
|
|
return jsonify(note.to_dict()), 201
|
||
|
|
|
||
|
|
|
||
|
|
@instruments_bp.patch("/tunes/<int:tune_id>/instruments/<int:entry_id>/notes/<int:note_id>")
|
||
|
|
@require_api_key
|
||
|
|
def update_instrument_note(tune_id, entry_id, note_id):
|
||
|
|
note = db.get_or_404(TuneByInstrumentNote, note_id)
|
||
|
|
data = request.get_json(force=True)
|
||
|
|
if "note" in data:
|
||
|
|
note.note = data["note"]
|
||
|
|
db.session.commit()
|
||
|
|
return jsonify(note.to_dict())
|
||
|
|
|
||
|
|
|
||
|
|
@instruments_bp.delete("/tunes/<int:tune_id>/instruments/<int:entry_id>/notes/<int:note_id>")
|
||
|
|
@require_api_key
|
||
|
|
def delete_instrument_note(tune_id, entry_id, note_id):
|
||
|
|
note = db.get_or_404(TuneByInstrumentNote, note_id)
|
||
|
|
db.session.delete(note)
|
||
|
|
db.session.commit()
|
||
|
|
return jsonify({"deleted": note_id})
|