diff --git a/db.py b/db.py index 6164f10..c80dfa6 100644 --- a/db.py +++ b/db.py @@ -11,6 +11,12 @@ async def create(): import recipe.db as recipe_db await recipe_db.create(conn) + import person.db as person_db + await person_db.create(conn) + + import meals.db as meals_db + await meals_db.create(conn) + await conn.commit() await conn.close() diff --git a/meals/__init__.py b/meals/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/meals/db.py b/meals/db.py new file mode 100644 index 0000000..9ba0754 --- /dev/null +++ b/meals/db.py @@ -0,0 +1,63 @@ +from typing import List, ClassVar +from pydantic import BaseModel +from person import Person + +import datetime + +class Meal(BaseModel): + KEYS: ClassVar[List[str]] = ['id', 'date'] + id: int + date: datetime.datetime + chef: List[Person] = [] + cleanup: List[Person] = [] + consumers: List[Person] = [] + recipes: List[Person] = [] + +async def create(conn): + await conn.execute(''' + CREATE TABLE IF NOT EXISTS Meal ( + id INTEGER PRIMARY KEY, + date TEXT UNIQUE + );''') + + await conn.execute(''' + CREATE TABLE IF NOT EXISTS MealParticipant ( + meal_id INTEGER, + person_id INTEGER, + role TEXT, + FOREIGN KEY(meal_id) REFERENCES Meal(id), + FOREIGN KEY(person_id) REFERENCES Person(id) + );''') + + await conn.execute(''' + CREATE TABLE IF NOT EXISTS MealRecipe ( + meal_id INTEGER, + recipe_id INTEGER, + FOREIGN KEY(meal_id) REFERENCES Meal(id), + FOREIGN KEY(recipe_id) REFERENCES Recipe(id) + );''') + +async def insert_meal(conn, meal: Meal): + async with conn.execute(''' + INSERT INTO Meal (date) + VALUES (?) + ''', (meal.date,)) as cursor: + return cursor.lastrowid + +async def find_meal_by_id(conn, meal_id: int) -> Meal: + async with conn.execute(f''' + SELECT {','.join(Meal.KEYS)} FROM Meal + WHERE id = ? + LIMIT 1 + ''', (meal_id,)) as cursor: + async for row in cursor: + return Meal(**{k:v for k,v in zip(Meal.KEYS, row)}) + +async def find_meal_by_date(conn, date: datetime) -> Meal: + async with conn.execute(f''' + SELECT {','.join(Meal.KEYS)} FROM Meal + WHERE date = ? + LIMIT 1 + ''', (date,)) as cursor: + async for row in cursor: + return Meal(**{k:v for k,v in zip(Meal.KEYS, row)}) \ No newline at end of file diff --git a/person/__init__.py b/person/__init__.py new file mode 100644 index 0000000..875c8f6 --- /dev/null +++ b/person/__init__.py @@ -0,0 +1 @@ +from person.db import Person \ No newline at end of file diff --git a/person/db.py b/person/db.py new file mode 100644 index 0000000..c22241a --- /dev/null +++ b/person/db.py @@ -0,0 +1,12 @@ +from pydantic import BaseModel +from typing import List, ClassVar + +class Person(BaseModel): + id: int + name: str + +async def create(conn): + return None + +def get(conn, id: int) -> Person: + return Person(id=id, name='test') \ No newline at end of file