Exemplo n.º 1
0
def handle_fav():
    # Access the identity of the current user with get_jwt_identity
    current_user_id = get_jwt_identity()

    #----------------------------CREATE POST
    if request.method == 'POST':
        body = request.get_json()
        if body is None:
            return "The request body is null", 400
        if 'name' not in body:
            return "You need to specify the name", 400

        fav = Favorites()
        fav.name = body['name']
        fav.user_id = current_user_id

        db.session.add(fav)  # agrega el usuario a la base de datos
        db.session.commit()  # guarda los cambios

        response_body = {"msg": "You POST a favorite from an user"}

        return jsonify(response_body), 200

    #----------------------------CREATE GET
    elif request.method == 'GET':
        getUserFav = Favorites.query.filter_by(user_id=current_user_id)
        getUserFav = list(map(lambda x: x.serialize(), getUserFav))

        return jsonify(getUserFav), 200

    #----------------------------CREATE DELETE
    elif request.method == 'DELETE':
        body = request.get_json()
        if body is None:
            return "The request body is null", 400
        if 'name' not in body:
            return "You need to specify the name", 400

        name = request.json.get("name", None)
        fav = Favorites.query.filter_by(name=name).first()

        db.session.delete(fav)  # agrega el usuario a la base de datos
        db.session.commit()  # guarda los cambios

        response_body = {"msg": "You DELETE a favorite from an user"}

        return jsonify(response_body), 200
Exemplo n.º 2
0
def create_fav():
    current_user_id = get_jwt_identity()

    body = request.get_json()
    if body is None:
        return "The request body is null", 400
    if 'name' not in body:
        return "You need to specify the name", 400

    fav = Favorites()
    fav.name = body['name']
    fav.user_id = current_user_id

    db.session.add(fav)  # agrega el usuario a la base de datos
    db.session.commit()  # guarda los cambios

    response_body = {"msg": "Well done. Your Post in Favorites is working"}

    return jsonify(response_body), 200
Exemplo n.º 3
0
def create_favorite():
    current_user_id = get_jwt_identity()

    body = request.get_json()  # get the request body content
    if body is None:
        return "The request body is null", 400
    if 'name' not in body:
        return 'You need to specify the favorite name', 400

    favorites = Favorites()
    favorites.user_id = current_user_id
    favorites.name = body['name']

    #agrega user a la base de datos
    db.session.add(favorites)
    #guarda los cambios
    db.session.commit()

    getfavs = Favorites.query.filter_by(user_id=current_user_id)
    getfavs = list(map(lambda x: x.serialize(), getfavs))

    return jsonify(getfavs), 200