Ejemplo n.º 1
0
def delete_user(id):
    user = User.query.get(id)

    db.session.delete(user)
    db.session.commit()

    return jsonify(user_schema.dump(user))
Ejemplo n.º 2
0
def get_user_by_id(user_id: int):
    user = User.query.filter(User.id == user_id).first()
    if user:
        result = user_schema.dump(user)
        result.pop('password')
        return jsonify(user=result)
    else:
        return jsonify(message=f"No User Found"), 404
Ejemplo n.º 3
0
def update_user(id):
    request_data = request.json
    user = User.query.get(id)
    user.first_name = request_data["first_name"]
    user.last_name = request_data["last_name"]
    user.email = request_data["email"]

    db.session.add(user)
    db.session.commit()

    return jsonify(user_schema.dump(user))
Ejemplo n.º 4
0
def create_user():
    request_data = request.json
    first_name = request_data["last_name"]
    last_name = request_data["last_name"]
    email = request_data["email"]
    new_user = User(first_name=first_name, last_name=last_name, email=email)

    db.session.add(new_user)
    db.session.commit()

    return jsonify(user_schema.dump(new_user))
Ejemplo n.º 5
0
def get_user_by_email():
    user_email = request.args.get("email")
    if user_email:
        user = User.query.filter(User.email_address == user_email).first()
        if user:
            result = user_schema.dump(user)
            result.pop('password')
            return jsonify(user=result)
        else:
            return jsonify(message=f"No User Found"), 404
    else:
        return jsonify(message="No Email Provided"), 400
Ejemplo n.º 6
0
def register():
    """
    View function to register new user
    """
    try:
        user = user_schema.load(request.get_json())
        db_session.add(user)
        db_session.commit()
        return (json.dumps(user_schema.dump(user)), 200, {
            'content-type': 'application/json'
        })
    except ValidationError as err:
        return make_response(jsonify({'message': err.messages}), 422)
Ejemplo n.º 7
0
def get(user_id):
    """
    View function to GET user
    """
    user = db_session.query(User).get(user_id)

    if not user:
        error = "User id {0} doesn't exist.".format(user_id)
        return (json.dumps({'message': error}), 404, {
            'content-type': 'application/json'
        })

    return (json.dumps(user_schema.dump(user)), 200, {
        'content-type': 'application/json'
    })
Ejemplo n.º 8
0
def get_user_by_id(id):
    user = User.query.get(id)
    result = user_schema.dump(user)

    return jsonify(result)