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, 12, 5, 44), 'flask.Blueprint', 'Blueprint', ({(5, 22, 5, 33): '"""app.users"""', (5, 35, 5, 43): '__name__'}, {}), "('app.users', __name__)", False, 'from flask import Blueprint, request, jsonify\n'), ((15, 11, 15, 37), 'app.domains.users.actions.get_user_by_id', 'get_user_by_id', (), '', False, 'from app.domains.users.actions import get_all_users, insert_user, get_user_by_id, update_user, delete_user\n'), ((21, 14, 21, 32), 'flask.request.get_json', 'request.get_json', ({}, {}), '()', False, 'from flask import Blueprint, request, jsonify\n'), ((22, 11, 22, 31), 'app.domains.users.actions.insert_user', 'insert_user', ({(22, 23, 22, 30): 'payload'}, {}), '(payload)', False, 'from app.domains.users.actions import get_all_users, insert_user, get_user_by_id, update_user, delete_user\n'), ((28, 14, 28, 32), 'flask.request.get_json', 'request.get_json', ({}, {}), '()', False, 'from flask import Blueprint, request, jsonify\n'), ((29, 11, 29, 48), 'app.domains.users.actions.update_user', 'update_user', (), '', False, 'from app.domains.users.actions import get_all_users, insert_user, get_user_by_id, update_user, delete_user\n'), ((35, 4, 35, 27), 'app.domains.users.actions.delete_user', 'delete_user', (), '', False, 'from app.domains.users.actions import get_all_users, insert_user, get_user_by_id, update_user, delete_user\n'), ((36, 11, 36, 47), 'flask.jsonify', 'jsonify', ({(36, 19, 36, 46): "{'message': 'user deleted'}"}, {}), "({'message': 'user deleted'})", False, 'from flask import Blueprint, request, jsonify\n'), ((10, 49, 10, 64), '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')]