from flask import Blueprint, request, jsonify
from app.domains.users.actions import get_all_users, insert_user, get_user_by_id, update_user, delete_user


app_users = Blueprint('app.users', __name__)


@app_users.route('/users', methods=['GET'])
def get_users():
    return jsonify([user.serialize() for user in get_all_users()]), 200


@app_users.route('/users/<id>', methods=["GET"])
def get_by_id(id: str):
    user = get_user_by_id(id_user=id)
    return jsonify(user.serialize()), 200


@app_users.route('/users', methods=["POST"])
def post_user():
    payload = request.get_json()
    user = insert_user(payload)
    return jsonify(user.serialize()), 201


@app_users.route('/users/<id>', methods=["PUT"])
def update(id: str):
    payload = request.get_json()
    user = update_user(id_user=id, data=payload)
    return jsonify(user.serialize()), 200


@app_users.route('/users/<id>', methods=["DELETE"])
def delete(id: str):
    delete_user(id_user=id)
    return jsonify({"message": "user deleted"}), 200

[(5, 'flask.Blueprint', 'Blueprint', (['"""app.users"""', '__name__'], {}), False, 'from flask import Blueprint, request, jsonify\n'), (15, 'app.domains.users.actions.get_user_by_id', 'get_user_by_id', ([], {'id_user': 'id'}), False, 'from app.domains.users.actions import get_all_users, insert_user, get_user_by_id, update_user, delete_user\n'), (21, 'flask.request.get_json', 'request.get_json', ([], {}), False, 'from flask import Blueprint, request, jsonify\n'), (22, 'app.domains.users.actions.insert_user', 'insert_user', (['payload'], {}), False, 'from app.domains.users.actions import get_all_users, insert_user, get_user_by_id, update_user, delete_user\n'), (28, 'flask.request.get_json', 'request.get_json', ([], {}), False, 'from flask import Blueprint, request, jsonify\n'), (29, 'app.domains.users.actions.update_user', 'update_user', ([], {'id_user': 'id', 'data': 'payload'}), False, 'from app.domains.users.actions import get_all_users, insert_user, get_user_by_id, update_user, delete_user\n'), (35, 'app.domains.users.actions.delete_user', 'delete_user', ([], {'id_user': 'id'}), False, 'from app.domains.users.actions import get_all_users, insert_user, get_user_by_id, update_user, delete_user\n'), (36, 'flask.jsonify', 'jsonify', (["{'message': 'user deleted'}"], {}), False, 'from flask import Blueprint, request, jsonify\n'), (10, 'app.domains.users.actions.get_all_users', 'get_all_users', ([], {}), False, 'from app.domains.users.actions import get_all_users, insert_user, get_user_by_id, update_user, delete_user\n')]