Meals and persons
This commit is contained in:
parent
a873dbb661
commit
671db1b4eb
5 changed files with 82 additions and 0 deletions
6
db.py
6
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()
|
||||
|
||||
|
|
|
|||
0
meals/__init__.py
Normal file
0
meals/__init__.py
Normal file
63
meals/db.py
Normal file
63
meals/db.py
Normal file
|
|
@ -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)})
|
||||
1
person/__init__.py
Normal file
1
person/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from person.db import Person
|
||||
12
person/db.py
Normal file
12
person/db.py
Normal file
|
|
@ -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')
|
||||
Loading…
Reference in a new issue