|
1 | | -import abc |
2 | | -from typing import Set |
| 1 | +from typing import Set, Protocol |
3 | 2 | from allocation.domain import model |
4 | 3 |
|
5 | 4 |
|
6 | | -class AbstractRepository(abc.ABC): |
7 | 5 |
|
8 | | - def __init__(self): |
| 6 | +class AbstractRepository(Protocol): |
| 7 | + |
| 8 | + def add(self, product: model.Product): |
| 9 | + ... |
| 10 | + |
| 11 | + def get(self, sku) -> model.Product: |
| 12 | + ... |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +class TrackingRepository: |
| 17 | + seen: Set[model.Product] |
| 18 | + |
| 19 | + def __init__(self, repo: AbstractRepository): |
9 | 20 | self.seen = set() # type: Set[model.Product] |
| 21 | + self._repo = repo |
10 | 22 |
|
11 | 23 | def add(self, product: model.Product): |
12 | | - self._add(product) |
| 24 | + self._repo.add(product) |
13 | 25 | self.seen.add(product) |
14 | 26 |
|
15 | 27 | def get(self, sku) -> model.Product: |
16 | | - product = self._get(sku) |
| 28 | + product = self._repo.get(sku) |
17 | 29 | if product: |
18 | 30 | self.seen.add(product) |
19 | 31 | return product |
20 | 32 |
|
21 | | - @abc.abstractmethod |
22 | | - def _add(self, product: model.Product): |
23 | | - raise NotImplementedError |
24 | | - |
25 | | - @abc.abstractmethod |
26 | | - def _get(self, sku) -> model.Product: |
27 | | - raise NotImplementedError |
28 | | - |
29 | 33 |
|
30 | 34 |
|
31 | | -class SqlAlchemyRepository(AbstractRepository): |
| 35 | +class SqlAlchemyRepository: |
32 | 36 |
|
33 | 37 | def __init__(self, session): |
34 | | - super().__init__() |
35 | 38 | self.session = session |
36 | 39 |
|
37 | | - def _add(self, product): |
| 40 | + def add(self, product): |
38 | 41 | self.session.add(product) |
39 | 42 |
|
40 | | - def _get(self, sku): |
| 43 | + def get(self, sku): |
41 | 44 | return self.session.query(model.Product).filter_by(sku=sku).first() |
0 commit comments