11import abc
22from domain import model
3+ from typing import Iterable
34
45
56class AbstractRepository (abc .ABC ):
@@ -14,10 +15,41 @@ def get(self, reference) -> model.Batch:
1415
1516class SqlAlchemyRepository (AbstractRepository ):
1617 def __init__ (self , session ):
17- self .session = session
18-
19- def add (self , batch ):
20- self .session .add (batch )
18+ self ._session = session
19+
20+ async def add (self , batch ):
21+ batch_id = await self ._add_batch (batch )
22+ await self ._add_allocations (batch_id , batch ._allocations )
23+
24+ async def _add_batch (self , batch : model .Batch ):
25+ async with self ._session as session :
26+ async with session .begin ():
27+ result = await session .execute (
28+ """
29+ INSERT INTO batches (ref, sku, _purchased_quantity, eta)
30+ VALUES ( :ref, :sku, :_purchased_quantity, :eta )
31+ RETURNING id
32+ """ ,
33+ dict (ref = batch .ref , sku = batch .sku , _purchased_quantity = batch ._purchased_quantity , eta = batch .eta ),
34+
35+ )
36+ [batch_id ] = result
37+ return batch_id
38+
39+ async def _add_allocations (self , batch_id : int , allocations : Iterable [model .OrderLine ]):
40+ return
41+ # for line in allocations:
42+ # async with self._session as session:
43+ # await session.execute(
44+ # """
45+ # INSERT INTO order_lines (orderline_id, batch_id)
46+ # VALUES ( %{orderline_id}d, %{batch_id}d )
47+ # ON CONFLICT DO UPDATE SET
48+ # orderline_id=%{orderline_id}d,
49+ # batchid=%{batch_id}d,
50+ # """,
51+ # dict(batch_id=batch_id, orderline_id=
52+ # )
2153
2254 def get (self , reference ):
2355 return self .session .query (model .Batch ).filter_by (reference = reference ).one ()
0 commit comments