Ejemplo n.º 1
0
    def post(cls):
        data = cls.parser.parse_args()

        ingredient = IngredientsModel(data['id'],
                                      data['amount'],
                                      data['ingredient'],
                                      data['measurement'],
                                      )
        ingredient.save_to_db()

        return {"message": "Ingredient created successfully."}, 201
    def post(cls):
        data = cls.parser.parse_args()
        recipe = RecipeModel(data['user_id'], data['cuisine_id'], data['name'],
                             data['description'], data['image_path'],
                             data['total_time'], data['prep_time'],
                             data['cook_time'], data['level'], data['source'])
        categories = data['category']
        if categories is not None:
            for cat in categories:
                category_json = ast.literal_eval(cat)
                category = CategoriesModel(category_json['id'],
                                           category_json['name'],
                                           category_json['description'])
                recipe.save_recipe_to_db(category)

        allergens = data['allergens']
        if allergens is not None:
            for aller in allergens:
                allergens_json = ast.literal_eval(aller)
                allergen = AllergensModel(
                    allergens_json['id'],
                    allergens_json['name'],
                )
                recipe.save_allergen_to_db(allergen)

        courses = data['courses']
        if courses is not None:
            for cour in courses:
                courses_json = ast.literal_eval(cour)
                course = AllergensModel(
                    courses_json['id'],
                    courses_json['name'],
                )
                recipe.save_course_to_db(course)

        steps = data['steps']
        if steps is not None:
            for step in steps:
                steps_json = ast.literal_eval(step)
                step = StepsModel(
                    steps_json['id'],
                    steps_json['step_number'],
                    steps_json['instructions'],
                )
                recipe.save_step_to_db(step)

        ingredients = data['ingredients']
        if ingredients is not None:
            for ingredient in ingredients:
                ingredients_json = ast.literal_eval(ingredient)
                ingredient = IngredientsModel(ingredients_json['id'],
                                              ingredients_json['amount'],
                                              ingredients_json['ingredient'],
                                              ingredients_json['measurement'])
                recipe.save_ingredient_to_db(ingredient)

        return {"message": "Recipe created successfully."}, 201
Ejemplo n.º 3
0
    def put(cls, id):
        data = cls.parser.parse_args()
        ingredient = IngredientsModel.find_by_id(id)

        if ingredient:
            ingredient.id = data['id']
            ingredient.amount = data['amount']
            ingredient.ingredient = data['ingredient']
            ingredient.measurement = data['measurement']
        else:
            ingredient = IngredientsModel(**data)

        ingredient.save_to_db()

        return ingredient.json()
    def put(cls, id):
        data = cls.parser.parse_args()
        recipe = RecipeModel.find_by_id(id)

        if recipe:
            recipe.user_id = data['user_id']
            recipe.cuisine_id = data['cuisine_id']
            recipe.name = data['name']
            recipe.description = data['description']
            recipe.image_path = data['image_path']
            recipe.total_time = data['total_time']
            recipe.prep_time = data['prep_time']
            recipe.cook_time = data['cook_time']
            recipe.level = data['level']
            recipe.source = data['source']
        else:
            recipe = RecipeModel(**data)

        recipe_categories = data['category']
        for cat in recipe_categories:
            category = RecipeCategory.find_by_id(id)
            if category:
                recipe.delete_category_from_db(category)

        for cat in recipe_categories:
            category_json = ast.literal_eval(cat)
            category = CategoriesModel(category_json['id'],
                                       category_json['name'],
                                       category_json['description'])
            recipe.save_recipe_to_db(category)

        allergens = data['allergens']
        if allergens:
            for aller in allergens:
                allergen = RecipeAllergens.find_by_id(id)
                if allergen:
                    recipe.delete_allergen_from_db(allergen)
        else:
            allergen = RecipeAllergens.find_by_id(id)
            if allergen:
                recipe.delete_allergen_from_db(allergen)

        if allergens:
            for aller in allergens:
                allergens_json = ast.literal_eval(aller)
                allergen = AllergensModel(
                    allergens_json['id'],
                    allergens_json['name'],
                )
                recipe.save_allergen_to_db(allergen)

        courses = data['courses']
        for cour in courses:
            course = RecipeCourses.find_by_id(id)
            if course:
                recipe.delete_course_from_db(course)
        for cour in courses:
            courses_json = ast.literal_eval(cour)
            course = CoursesModel(
                courses_json['id'],
                courses_json['name'],
            )
            recipe.save_course_to_db(course)

        steps = data['steps']
        for step in steps:
            steps_json = ast.literal_eval(step)
            step = StepsModel.find_by_id(steps_json['id'])
            if step:
                step.step_number = steps_json['step_number']
                step.instructions = steps_json['instructions']
            else:
                step = StepsModel(
                    steps_json['id'],
                    steps_json['step_number'],
                    steps_json['instructions'],
                )
            recipe.save_step_to_db(step)

        ingredients = data['ingredients']
        for ingredient in ingredients:
            ingredients_json = ast.literal_eval(ingredient)
            ingredient = IngredientsModel.find_by_id(ingredients_json['id'])
            if ingredient:
                ingredient.amount = ingredients_json['amount']
                ingredient.ingredient = ingredients_json['ingredient']
                ingredient.measurement = ingredients_json['measurement']
            else:
                ingredient = IngredientsModel(ingredients_json['id'],
                                              ingredients_json['amount'],
                                              ingredients_json['ingredient'],
                                              ingredients_json['measurement'])
            recipe.save_ingredient_to_db(ingredient)

        return recipe.json()
Ejemplo n.º 5
0
 def get(cls):
     return {'ingredients': [ingredient.json() for ingredient in IngredientsModel.find_all()]}
Ejemplo n.º 6
0
 def delete(cls, id: int):
     ingredient = IngredientsModel.find_by_id(id)
     if not ingredient:
         return {'message': 'Ingredient Not Found'}, 404
     ingredient.delete_from_db()
     return {'message': 'Ingredient deleted.'}, 200
Ejemplo n.º 7
0
 def get(cls, id: int):
     ingredient = IngredientsModel.find_by_id(id)
     if not ingredient:
         return {'message': 'Ingredient Not Found'}, 404
     return ingredient.json(), 200