Beispiel #1
0
def update_recipe(recipe_id: str, data: dict):

    try:
        recipe_schema = RecipeSchema()
        validated_data = recipe_schema.load(data, partial=True)
        recipe = Recipe.get_by_id(recipe_id)
        for attribute in validated_data:
            setattr(recipe, attribute, validated_data[attribute])
        db.session.add(recipe)
        db.session.commit()
        return recipe_schema.dump(recipe), None
    except ValidationError:
        log.error(
            f'there was an error while parsing data for recipe update [id:{recipe_id}]'
        )
        traceback.print_exc()
        return None, {'msg': 'invalid data for recipe', 'status_code': 400}
    except SQLAlchemyError:
        log.error(
            f'there was a database error while updating recipe [id:{recipe_id}]'
        )
        traceback.print_exc()
        return None, {
            'msg': 'there was an error while updating recipe',
            'status_code': 500
        }
Beispiel #2
0
def get_recipe_by_id(recipe_id: str, params: 'SearchQueryParam'):

    try:
        recipe = Recipe.get_by_id(recipe_id, params.fields)
        return RecipeSchema().dump(recipe), None
    except SQLAlchemyError:
        log.error(
            f'there was a database error while getting recipe by id [recipe_id:{recipe_id}]'
        )
        traceback.print_exc()
        return None, {
            'msg': 'there was an error getting recipe',
            'status_code': 500
        }
Beispiel #3
0
def get_recipe_with_ingredients(recipe_id, params):

    try:
        recipe = Recipe.get_by_id(recipe_id, params.fields)
    except SQLAlchemyError:
        log.error(
            f'there was a database error while looking recipe with ingredients [recipe:{recipe_id}]'
        )
        traceback.print_exc()
        return None, {
            'msg': 'there was and error while looking for recipe ingredients',
            'status_code': 500
        }

    if recipe is None:
        return None, {
            'msg': f'recipe with \'id\' {recipe_id} does not exits',
            'status_code': 404
        }

    try:
        ingredients = RecipeIngredient.get_ingredients(recipe_id)
    except SQLAlchemyError:
        log.error(
            f'there was a database error while looking ingredients for recipe with ingredients [recipe:{recipe_id}]'
        )
        traceback.print_exc()
        return None, {
            'msg': 'there was and error while looking for recipe ingredients',
            'status_code': 500
        }

    response = RecipeSchema(only=params.fields).dump(recipe)
    response['ingredients'] = RecipeIngredientResponseSchema(
        many=True).dump(ingredients)

    return response, None