Add term column
This commit is contained in:
parent
b5e2e8ab2a
commit
377462ed20
5 changed files with 100 additions and 1 deletions
43
app/routes_terms.py
Normal file
43
app/routes_terms.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
"""Routes for terms lookup table."""
|
||||
from flask import Blueprint, jsonify, request
|
||||
from app.extensions import db
|
||||
from app.models import Term
|
||||
from app.auth import require_api_key
|
||||
|
||||
terms_bp = Blueprint("terms", __name__, url_prefix="/terms")
|
||||
|
||||
|
||||
@terms_bp.get("/")
|
||||
def list_terms():
|
||||
terms = db.session.query(Term).order_by(Term.name).all()
|
||||
return jsonify([t.to_dict() for t in terms])
|
||||
|
||||
|
||||
@terms_bp.post("/")
|
||||
@require_api_key
|
||||
def create_term():
|
||||
data = request.get_json(force=True)
|
||||
term = Term(name=data["name"])
|
||||
db.session.add(term)
|
||||
db.session.commit()
|
||||
return jsonify(term.to_dict()), 201
|
||||
|
||||
|
||||
@terms_bp.patch("/<int:term_id>")
|
||||
@require_api_key
|
||||
def update_term(term_id):
|
||||
term = db.get_or_404(Term, term_id)
|
||||
data = request.get_json(force=True)
|
||||
if "name" in data:
|
||||
term.name = data["name"]
|
||||
db.session.commit()
|
||||
return jsonify(term.to_dict())
|
||||
|
||||
|
||||
@terms_bp.delete("/<int:term_id>")
|
||||
@require_api_key
|
||||
def delete_term(term_id):
|
||||
term = db.get_or_404(Term, term_id)
|
||||
db.session.delete(term)
|
||||
db.session.commit()
|
||||
return jsonify({"deleted": term_id})
|
||||
Loading…
Add table
Add a link
Reference in a new issue