コード例 #1
0
ファイル: main.py プロジェクト: TAPIAR7/StarWars_Blog_API
def add_new_favorite_by_user(position):
    # Collect data
    planet_idPostIncoming = request.json.get("planet_id", None)
    people_idPostIncoming = request.json.get("people_id", None)
    user_idPostIncoming = position

    # Validate data

    # Validate if user exist
    if user_idPostIncoming is None:
        raise APIException('User not found', status_code=404)

    # Validate if planet and people id are both defined. Just can be define one.
    if (planet_idPostIncoming is not None) and (people_idPostIncoming
                                                is not None):
        raise APIException('Data given is invalid', status_code=404)

    # Validate if planet and people id are both None.
    if (planet_idPostIncoming is None) and (people_idPostIncoming is None):
        raise APIException('Data given is invalid', status_code=404)

    if planet_idPostIncoming != people_idPostIncoming:
        # Validate if people id exists.
        if planet_idPostIncoming is None:
            if people_idPostIncoming is None:
                raise APIException('Character not found', status_code=404)
        # Validate if planet id exists.
        if people_idPostIncoming is None:
            if planet_idPostIncoming is None:
                raise APIException('Planet not found', status_code=404)
    else:
        raise APIException('Data given is invalid', status_code=404)

    # Verify if favorite already exist
    favorite_exist = Favorites.query.filter_by(
        user_id=user_idPostIncoming,
        planet_id=planet_idPostIncoming,
        people_id=people_idPostIncoming).first()
    if favorite_exist is not None:
        raise APIException('Favorite already exists.', status_code=404)

    # Create instance to the model
    newFavorite = Favorites(planet_id=planet_idPostIncoming,
                            people_id=people_idPostIncoming,
                            user_id=user_idPostIncoming)

    # Add it to database session
    db.session.add(newFavorite)
    db.session.commit()
    return jsonify(newFavorite.serialize()), 200
コード例 #2
0
 def add_favorites(user, id):
     body=request.get_json()
     favorite_exists = Favorites.query.filter_by(user_id = user['user']['id'], recipe_id = id).first()
     if favorite_exists is not None: 
         if favorite_exists.is_active == False :
             favorite_exists.is_active = True
             db.session.commit()
             return jsonify(favorite_exists.serialize()),200
         return jsonify("favorite already exists"),409
     else:
         new_favorite=Favorites(user_id = user['user']['id'], recipe_id = id, title = body['title'], image = body['image'])
         db.session.add(new_favorite)
         db.session.commit()
         return jsonify(new_favorite.serialize()),201
コード例 #3
0
def create_favorites():

    body = request.get_json()
    item = body["item"]
    print(body)
    if body is None:
        raise APIException("Invalid Body", status_code=400)
    new_favorites = Favorites(
        name=item["name"],
        price=item["price"],
        img=item["img"],
        continent=item["continent"],
        country=item["country"],
    )

    db.session.add(new_favorites)
    db.session.commit()
    return jsonify(new_favorites.serialize()), 200