from flask_api import FlaskAPI
from flask_sqlalchemy import SQLAlchemy
from flask import request, jsonify, abort

from instance.config import app_config

# initialize sql-alchemy
db = SQLAlchemy()


def create_app(config_name):
    from app.models import BucketList
    app = FlaskAPI(__name__, instance_relative_config=True)
    app.config.from_object(app_config[config_name])
    app.config.from_pyfile('config.py')
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db.init_app(app)

    @app.route('/bucketlists/', methods=['POST', 'GET'])
    def bucketlists():
        if request.method == "POST":
            name = str(request.data.get('name', ''))
            if name:
                bucketlist = BucketList(name=name)
                bucketlist.save()
                response = jsonify({
                    'id': bucketlist.id,
                    'name': bucketlist.name,
                    'date_created': bucketlist.date_created,
                    'date_modified': bucketlist.date_modified
                })
                response.status_code = 201
                return response
        else:
            # GET
            bucketlists = BucketList.get_all()
            results = []

            for bucketlist in bucketlists:
                obj = {
                    'id': bucketlist.id,
                    'name': bucketlist.name,
                    'date_created': bucketlist.date_created,
                    'date_modified': bucketlist.date_modified
                }
                results.append(obj)
            response = jsonify(results)
            response.status_code = 200
            return response

    @app.route('/bucketlists/<int:id>', methods=['GET', 'PUT', 'DELETE'])
    def bucketlist_manipulation(id, **kwargs):
        # retrieve a buckelist using it's ID
        bucketlist = BucketList.query.filter_by(id=id).first()
        if not bucketlist:
            # Raise an HTTPException with a 404 not found status code
            abort(404)

        if request.method == 'DELETE':
            bucketlist.delete()
            return {
                       "message": "bucketlist {} deleted successfully".format(bucketlist.id)
                   }, 200

        elif request.method == 'PUT':
            name = str(request.data.get('name', ''))
            bucketlist.name = name
            bucketlist.save()
            response = jsonify({
                'id': bucketlist.id,
                'name': bucketlist.name,
                'date_created': bucketlist.date_created,
                'date_modified': bucketlist.date_modified
            })
            response.status_code = 200
            return response
        else:
            # GET
            response = jsonify({
                'id': bucketlist.id,
                'name': bucketlist.name,
                'date_created': bucketlist.date_created,
                'date_modified': bucketlist.date_modified
            })
            response.status_code = 200
            return response

    return app

@@ -258,16 +258,38 @@
 cketList
+, BucketListItem, User
 %0A    app
@@ -521,32 +521,39 @@
    @app.route('/
+api/v1/
 bucketlists/', m
@@ -596,16 +596,23 @@
 etlists(
+user_id
 ):%0A     
@@ -766,16 +766,36 @@
 ame=name
+, created_by=user_id
 )%0A      
@@ -1059,32 +1059,75 @@
 st.date_modified
+,%0A                    'created_by': user_id
 %0A               
@@ -1765,16 +1765,23 @@
 route('/
+api/v1/
 bucketli
@@ -2690,32 +2690,85 @@
 st.date_modified
+,%0A                'created_by': bucketlist.created_by
 %0A            %7D)%0A
@@ -2796,33 +2796,33 @@
 status_code = 20
-0
+1
 %0A            ret
@@ -2858,32 +2858,480 @@
           # GET%0A
+            items = BucketListItem.query.filter_by(bucketlist_id=id)%0A            bucketlist_items = %5B%5D%0A            for item in items:%0A                data = %7B%0A                    %22id%22: item.id,%0A                    %22name%22: item.name,%0A                    %22date_created%22: item.date_created,%0A                    %22date_modified%22: item.date_modified,%0A                    %22done%22: item.done%0A                %7D%0A                bucketlist_items.append(data)%0A%0A
             resp
@@ -3531,32 +3531,129 @@
 st.date_modified
+,%0A                'created_by': bucketlist.created_by,%0A                'items': bucketlist_items%0A
 %0A            %7D)%0A

Update endpoints and routes