def put(self, name): data = Recipe.parser.parse_args() recipe = RecipeModel.find_by_name(name) if recipe: recipe.instructions = data['instructions'] else: recipe = RecipeModel(name, data['instructions']) recipe.save_to_db() return recipe.json()
def post(self): data = self.parser.parse_args() if RecipeModel.find_by_name(data['name']): return { 'message': "A recipe with name '{}' already exists.".format(data['name']) }, 400 recipe = RecipeModel(**data) try: recipe.save_to_db() except: return {"message": "An error occurred creating the recipe."}, 500 return recipe.json(), 201
def post(self, name): if RecipeModel.find_by_name(name): return { 'message': "A recipe with name '{}' already exists.".format(name) }, 400 data = Recipe.parser.parse_args() recipe = RecipeModel(name, data['instructions'], data['category_id']) try: recipe.save_to_db() except: return {"message": "An error occurred inserting the recipe."}, 500 return recipe.json(), 201
class RecipeModelTest(BaseTest): def setUp(self) -> None: self.new_recipe = RecipeModel("Title1", "Url1", "Ingredients1, test1") self.new_recipe.recipe_id = 1 def test_init(self): self.assertEqual(self.new_recipe.recipe_title, "Title1") self.assertEqual(self.new_recipe.href, "Url1") self.assertEqual(self.new_recipe.recipe_ingredients, "Ingredients1, test1") def test_json(self): expected_output = { "recipe_id": 1, "recipe_title": "Title1", "recipe_link": "Url1", "recipe_ingredients": "Ingredients1, test1" } self.assertDictEqual(self.new_recipe.json(), expected_output)
def post(self, value): recipe_data = Recipe.recipe_parser.parse_args() portion_sum = 0 if RecipeModel.find_by_value(value): return {'message': "Recipe '{}' already exist".format(value)}, 400 recipe = RecipeModel(value, recipe_data['name']) for mixing in iter(recipe_data['ingredients_list']): portion_sum = portion_sum + mixing['portion'] if portion_sum > 200: return {"meessage": "portion over limit"} try: recipe_id = recipe.save_to_db() for mixing in iter(recipe_data['ingredients_list']): mixing_model = MixingModel(mixing['ingredient_id'], recipe_id, mixing['portion']) mixing_model.save_to_db() except: return {'message': 'An error occured during saving to DB'}, 500 return recipe.json(), 201
def put(self, recipe_id): data = NewRecipe.parser.parse_args() recipe = RecipeModel.find_by_id(recipe_id) if recipe: if recipe.chef_id == data['chef_id']: recipe.name = data['name'] recipe.recipe_type = data['recipe_type'] recipe.description = data['description'] recipe.steps = data['steps'] recipe.servings = data['servings'] recipe.prep_min = data['prep_min'] recipe.cook_min = data['cook_min'] else: return { 'message': 'You can not change this recipe as you are not the chef.' }, 401 else: recipe = RecipeModel(**data) recipe.save_to_db() return recipe.json(), 201