Initial Commit
slip slop
This commit is contained in:
commit
064e672ab7
61 changed files with 3504 additions and 0 deletions
124
app/routes_sub.py
Normal file
124
app/routes_sub.py
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
"""Routes for notes and references sub-resources."""
|
||||
from flask import Blueprint, jsonify, request
|
||||
from app.extensions import db
|
||||
from app.models import Tune, TuneNote, Reference, Musician
|
||||
from app.auth import require_api_key
|
||||
|
||||
sub_bp = Blueprint("sub", __name__)
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# Notes
|
||||
# ===========================================================================
|
||||
|
||||
@sub_bp.get("/tunes/<int:tune_id>/notes")
|
||||
def list_notes(tune_id):
|
||||
db.get_or_404(Tune, tune_id)
|
||||
notes = db.session.query(TuneNote).filter_by(tune_id=tune_id).all()
|
||||
return jsonify([n.to_dict() for n in notes])
|
||||
|
||||
|
||||
@sub_bp.post("/tunes/<int:tune_id>/notes")
|
||||
@require_api_key
|
||||
def add_note(tune_id):
|
||||
db.get_or_404(Tune, tune_id)
|
||||
data = request.get_json(force=True)
|
||||
note = TuneNote(tune_id=tune_id, note=data.get("note"))
|
||||
db.session.add(note)
|
||||
db.session.commit()
|
||||
return jsonify(note.to_dict()), 201
|
||||
|
||||
|
||||
@sub_bp.patch("/tunes/<int:tune_id>/notes/<int:note_id>")
|
||||
@require_api_key
|
||||
def update_note(tune_id, note_id):
|
||||
note = db.get_or_404(TuneNote, note_id)
|
||||
data = request.get_json(force=True)
|
||||
if "note" in data:
|
||||
note.note = data["note"]
|
||||
db.session.commit()
|
||||
return jsonify(note.to_dict())
|
||||
|
||||
|
||||
@sub_bp.delete("/tunes/<int:tune_id>/notes/<int:note_id>")
|
||||
@require_api_key
|
||||
def delete_note(tune_id, note_id):
|
||||
note = db.get_or_404(TuneNote, note_id)
|
||||
db.session.delete(note)
|
||||
db.session.commit()
|
||||
return jsonify({"deleted": note_id})
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# References
|
||||
# ===========================================================================
|
||||
|
||||
@sub_bp.get("/tunes/<int:tune_id>/references")
|
||||
def list_references(tune_id):
|
||||
db.get_or_404(Tune, tune_id)
|
||||
refs = db.session.query(Reference).filter_by(tune_id=tune_id).all()
|
||||
return jsonify([r.to_dict() for r in refs])
|
||||
|
||||
|
||||
@sub_bp.post("/tunes/<int:tune_id>/references")
|
||||
@require_api_key
|
||||
def add_reference(tune_id):
|
||||
db.get_or_404(Tune, tune_id)
|
||||
data = request.get_json(force=True)
|
||||
ref = Reference(tune_id=tune_id, link=data.get("link"), site=data.get("site"))
|
||||
db.session.add(ref)
|
||||
db.session.flush()
|
||||
for m in data.get("musicians", []):
|
||||
musician = db.session.get(Musician, m["id"])
|
||||
if musician:
|
||||
ref.musicians.append(musician)
|
||||
db.session.commit()
|
||||
return jsonify(ref.to_dict()), 201
|
||||
|
||||
|
||||
@sub_bp.patch("/tunes/<int:tune_id>/references/<int:ref_id>")
|
||||
@require_api_key
|
||||
def update_reference(tune_id, ref_id):
|
||||
ref = db.get_or_404(Reference, ref_id)
|
||||
data = request.get_json(force=True)
|
||||
for field in ("link", "site"):
|
||||
if field in data:
|
||||
setattr(ref, field, data[field])
|
||||
db.session.commit()
|
||||
return jsonify(ref.to_dict())
|
||||
|
||||
|
||||
@sub_bp.delete("/tunes/<int:tune_id>/references/<int:ref_id>")
|
||||
@require_api_key
|
||||
def delete_reference(tune_id, ref_id):
|
||||
ref = db.get_or_404(Reference, ref_id)
|
||||
db.session.delete(ref)
|
||||
db.session.commit()
|
||||
return jsonify({"deleted": ref_id})
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# Musicians on a reference (add/remove via join table)
|
||||
# ===========================================================================
|
||||
|
||||
@sub_bp.post("/tunes/<int:tune_id>/references/<int:ref_id>/musicians")
|
||||
@require_api_key
|
||||
def add_musician_to_reference(tune_id, ref_id):
|
||||
ref = db.get_or_404(Reference, ref_id)
|
||||
data = request.get_json(force=True)
|
||||
musician = db.get_or_404(Musician, data["musician_id"])
|
||||
if musician not in ref.musicians:
|
||||
ref.musicians.append(musician)
|
||||
db.session.commit()
|
||||
return jsonify(ref.to_dict())
|
||||
|
||||
|
||||
@sub_bp.delete("/tunes/<int:tune_id>/references/<int:ref_id>/musicians/<int:musician_id>")
|
||||
@require_api_key
|
||||
def remove_musician_from_reference(tune_id, ref_id, musician_id):
|
||||
ref = db.get_or_404(Reference, ref_id)
|
||||
musician = db.get_or_404(Musician, musician_id)
|
||||
if musician in ref.musicians:
|
||||
ref.musicians.remove(musician)
|
||||
db.session.commit()
|
||||
return jsonify(ref.to_dict())
|
||||
Loading…
Add table
Add a link
Reference in a new issue