import os
from flask import abort, request
from flask_restful import Resource
from {{ full_model.name.lower() }} import db
{% for mod in full_model.models -%}
from {{ full_model.name.lower() }}.models import {{mod.name}}
{% endfor %}

class {{ model.name }}sController(Resource):
    def get(self):
        return {{model.name}}.query.all()

    def post(self):
        {% for attrib in model.attributs -%}
        {{attrib.name}} = request.args.get("{{attrib.name}}", None)
        {% endfor -%}

        {{model.name.lower()}} = {{model.name}}()
        {% for attrib in model.attributs -%}
        {{model.name.lower()}}.{{attrib.name}} = {{attrib.name}}
        {% endfor -%}

        db.session.add({{model.name.lower()}})
        db.session.commit()


class {{ model.name }}Controller(Resource):
    def get(self, {{model.name.lower()}}_id):
        db_{{model.name.lower()}} = {{model.name}}.query.get({{model.name.lower()}}_id)
        if not db_{{model.name.lower()}}:
            abort(404)
        return db_{{model.name.lower()}}

    def put(self, {{model.name.lower()}}_id):
        db_{{model.name.lower()}} = {{model.name}}.query.get({{model.name.lower()}}_id)
        if not db_{{model.name.lower()}}:
            abort(404)
        {% for attrib in model.attributs -%}
        {{attrib.name}} = request.args.get("{{attrib.name}}", None)
        {% endfor -%}

        {% for attrib in model.attributs -%}
        db_{{model.name.lower()}}.{{attrib.name}} = {{attrib.name}}
        {% endfor -%}

        db.session.commit()

    def delete(self, {{model.name.lower()}}_id):
        db_{{model.name.lower()}} = {{model.name}}.query.get({{model.name.lower()}}_id)
        if not db_{{model.name.lower()}}:
            abort(404)

        db.session.delete(db_{{model.name.lower()}})
        db.session.commit()
