-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepositories.py
81 lines (62 loc) · 2.38 KB
/
repositories.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import json
from typing import Any
from create_recipes import (
Ingredient,
IngredientId,
MacroNutrients,
Recipe,
RecipeId,
IngredientRepository,
RecipeRepository,
)
from create_menu import Menu, MenuId, MenuRepository
def create_ingredient(ingredient_info: dict[str, Any]) -> Ingredient:
return Ingredient(
id=ingredient_info["id"],
macronutrients=MacroNutrients(
carbohydrates=ingredient_info["macronutrients"]["carbohydrates"],
proteins=ingredient_info["macronutrients"]["proteins"],
fats=ingredient_info["macronutrients"]["fats"],
),
kilocalories=ingredient_info["kilocalories"],
)
def create_recipe(recipe_info: dict[str, Any]) -> Recipe:
return Recipe(
id=recipe_info["id"],
name=recipe_info["name"],
yield_=recipe_info["yield"],
ingredients=[
(i[0], create_ingredient(i[1])) for i in recipe_info["ingredients"]
],
)
class InMemoryIngredientRepository(IngredientRepository):
def __init__(self, ingredients: list[dict[str, Any]]):
self.ingredients = {i["id"]: create_ingredient(i) for i in ingredients}
@staticmethod
def from_file(path: str) -> "InMemoryIngredientRepository":
with open(path) as f:
ingredients = json.loads(f.read())
return InMemoryIngredientRepository(ingredients)
def find(self, ingredient_id: IngredientId) -> Ingredient | None:
return self.ingredients.get(ingredient_id)
class InMemoryRecipeRepository(RecipeRepository):
def __init__(self, recipes: list[dict[str, Any]]):
self.recipes = {i["id"]: create_recipe(i) for i in recipes}
@staticmethod
def from_file(path: str) -> "InMemoryRecipeRepository":
with open(path) as f:
recipes = json.loads(f.read())
return InMemoryRecipeRepository(recipes)
def find(self, recipe_id: RecipeId) -> Recipe | None:
return self.recipes.get(str(recipe_id))
def all(self) -> list[Recipe]:
return list(self.recipes.values())
def add(self, recipe: Recipe):
self.recipes[str(recipe.id)] = recipe
class InMemoryMenuRepository(MenuRepository):
def __init__(self):
self.menus = {}
def find(self, menu_id: MenuId) -> Menu | None:
return self.menus.get(str(menu_id))
def add(self, menu: Menu):
self.menus[str(menu.id)] = menu