from flask import Blueprint, jsonify
from webargs.flaskparser import use_kwargs

from hobbit_core.flask_hobbit.pagination import PageParams, pagination

from app import models
from app import schemas

bp = Blueprint('example', __name__)


@bp.route('/ping/', methods=['GET'])
def ping():
    return 'ok'


@bp.route('/users/', methods=['GET'])
@use_kwargs(PageParams)
def list_users(page, page_size, order_by):
    qexp = models.User.query
    paged_ret = pagination(models.User, page, page_size, order_by, qexp)
    return jsonify(schemas.paged_user_schemas.dump(paged_ret).data)


@bp.route('/users/<int:pk>/', methods=['GET'])
def retrieve_user(pk):
    user = models.User.query.filter(models.User.id == pk).one()
    return jsonify(schemas.user_schemas.dump(user).data)

