Esempio n. 1
0
def add_favorites():
    if not request.is_json:
        return jsonify({"msg": "El body o contenido esta vacio"}), 400

    favorites = Favorites()
    favorites.user_id = request.json.get("user_id", None)
    favorites.movie_id = request.json.get("movie_id", None)
    favorites.year = request.json.get("year", None)
    favorites.poster = request.json.get("poster", None)
    favorites.title = request.json.get("title", None)

    db.session.add(favorites)
    db.session.commit()

    return jsonify({"msg": "Agregado a favoritos exitosamente"})
Esempio n. 2
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
Esempio n. 3
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
Esempio n. 4
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