11from typing import Set
22import abc
3- from allocation import model
3+ from allocation import model , orm
4+
45
56
67class AbstractRepository (abc .ABC ):
@@ -18,6 +19,12 @@ def get(self, sku) -> model.Product:
1819 self .seen .add (product )
1920 return product
2021
22+ def get_by_batchref (self , batchref ) -> model .Product :
23+ product = self ._get_by_batchref (batchref )
24+ if product :
25+ self .seen .add (product )
26+ return product
27+
2128 @abc .abstractmethod
2229 def _add (self , product : model .Product ):
2330 raise NotImplementedError
@@ -26,6 +33,11 @@ def _add(self, product: model.Product):
2633 def _get (self , sku ) -> model .Product :
2734 raise NotImplementedError
2835
36+ @abc .abstractmethod
37+ def _get_by_batchref (self , batchref ) -> model .Product :
38+ raise NotImplementedError
39+
40+
2941
3042
3143class SqlAlchemyRepository (AbstractRepository ):
@@ -39,3 +51,8 @@ def _add(self, product):
3951
4052 def _get (self , sku ):
4153 return self .session .query (model .Product ).filter_by (sku = sku ).first ()
54+
55+ def _get_by_batchref (self , batchref ):
56+ return self .session .query (model .Product ).join (model .Batch ).filter (
57+ orm .batches .c .reference == batchref ,
58+ ).first ()
0 commit comments