File tree Expand file tree Collapse file tree 4 files changed +25
-11
lines changed Expand file tree Collapse file tree 4 files changed +25
-11
lines changed Original file line number Diff line number Diff line change 1
1
import abc
2
+ from typing import Set
2
3
from allocation .domain import model
3
4
4
5
@@ -15,9 +16,14 @@ def get(self, sku) -> model.Product:
15
16
class SqlAlchemyRepository (AbstractRepository ):
16
17
def __init__ (self , session ):
17
18
self .session = session
19
+ self .seen = set () # type: Set[model.Product]
18
20
19
21
def add (self , product ):
22
+ self .seen .add (product )
20
23
self .session .add (product )
21
24
22
25
def get (self , sku ):
23
- return self .session .query (model .Product ).filter_by (sku = sku ).first ()
26
+ product = self .session .query (model .Product ).filter_by (sku = sku ).first ()
27
+ if product :
28
+ self .seen .add (product )
29
+ return product
Original file line number Diff line number Diff line change 4
4
5
5
from allocation .domain import model
6
6
from allocation .domain .model import OrderLine
7
- from . import messagebus
8
7
9
8
if TYPE_CHECKING :
10
9
from . import unit_of_work
@@ -36,9 +35,6 @@ def allocate(
36
35
product = uow .products .get (sku = line .sku )
37
36
if product is None :
38
37
raise InvalidSku (f"Invalid sku { line .sku } " )
39
- try :
40
- batchref = product .allocate (line )
41
- uow .commit ()
42
- return batchref
43
- finally :
44
- messagebus .handle (product .events )
38
+ batchref = product .allocate (line )
39
+ uow .commit ()
40
+ return batchref
Original file line number Diff line number Diff line change 5
5
from sqlalchemy .orm import sessionmaker
6
6
from sqlalchemy .orm .session import Session
7
7
8
+
8
9
from allocation import config
9
10
from allocation .adapters import repository
11
+ from . import messagebus
10
12
11
13
12
14
class AbstractUnitOfWork (abc .ABC ):
@@ -18,8 +20,18 @@ def __enter__(self) -> AbstractUnitOfWork:
18
20
def __exit__ (self , * args ):
19
21
self .rollback ()
20
22
21
- @abc .abstractmethod
22
23
def commit (self ):
24
+ self ._commit ()
25
+ self .publish_events ()
26
+
27
+ def publish_events (self ):
28
+ for product in self .products .seen :
29
+ while product .events :
30
+ event = product .events .pop (0 )
31
+ messagebus .handle (event )
32
+
33
+ @abc .abstractmethod
34
+ def _commit (self ):
23
35
raise NotImplementedError
24
36
25
37
@abc .abstractmethod
@@ -48,7 +60,7 @@ def __exit__(self, *args):
48
60
super ().__exit__ (* args )
49
61
self .session .close ()
50
62
51
- def commit (self ):
63
+ def _commit (self ):
52
64
self .session .commit ()
53
65
54
66
def rollback (self ):
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ def __init__(self):
20
20
self .products = FakeRepository ([])
21
21
self .committed = False
22
22
23
- def commit (self ):
23
+ def _commit (self ):
24
24
self .committed = True
25
25
26
26
def rollback (self ):
You can’t perform that action at this time.
0 commit comments