Skip to content

Commit c114941

Browse files
committed
reset model, orm, repo and services to previous chap version
1 parent 6571c4d commit c114941

File tree

4 files changed

+29
-43
lines changed

4 files changed

+29
-43
lines changed

src/allocation/adapters/orm.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,11 @@
1717
Column('orderid', String(255)),
1818
)
1919

20-
products = Table(
21-
'products', metadata,
22-
Column('sku', String(255), primary_key=True),
23-
Column('version_number', Integer, nullable=False, server_default='0'),
24-
)
25-
2620
batches = Table(
2721
'batches', metadata,
2822
Column('id', Integer, primary_key=True, autoincrement=True),
2923
Column('reference', String(255)),
30-
Column('sku', ForeignKey('products.sku')),
24+
Column('sku', String(255)),
3125
Column('_purchased_quantity', Integer, nullable=False),
3226
Column('eta', Date, nullable=True),
3327
)
@@ -42,13 +36,10 @@
4236

4337
def start_mappers():
4438
lines_mapper = mapper(model.OrderLine, order_lines)
45-
batches_mapper = mapper(model.Batch, batches, properties={
39+
mapper(model.Batch, batches, properties={
4640
'_allocations': relationship(
4741
lines_mapper,
4842
secondary=allocations,
4943
collection_class=set,
5044
)
5145
})
52-
mapper(model.Product, products, properties={
53-
'batches': relationship(batches_mapper)
54-
})
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import abc
22
from allocation.domain import model
33

4+
45
class AbstractRepository(abc.ABC):
56

6-
@abc.abstractmethod
7-
def add(self, product: model.Product):
7+
def add(self, batch: model.Batch):
88
raise NotImplementedError
99

1010
@abc.abstractmethod
11-
def get(self, sku) -> model.Product:
11+
def get(self, reference) -> model.Batch:
1212
raise NotImplementedError
1313

1414

@@ -18,8 +18,11 @@ class SqlAlchemyRepository(AbstractRepository):
1818
def __init__(self, session):
1919
self.session = session
2020

21-
def add(self, product):
22-
self.session.add(product)
21+
def add(self, batch):
22+
self.session.add(batch)
23+
24+
def get(self, reference):
25+
return self.session.query(model.Batch).filter_by(reference=reference).one()
2326

24-
def get(self, sku):
25-
return self.session.query(model.Product).filter_by(sku=sku).first()
27+
def list(self):
28+
return self.session.query(model.Batch).all()

src/allocation/domain/model.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,15 @@ class OutOfStock(Exception):
88
pass
99

1010

11-
class Product:
12-
13-
def __init__(self, sku: str, batches: List[Batch], version_number: int = 0):
14-
self.sku = sku
15-
self.batches = batches
16-
self.version_number = version_number
17-
18-
def allocate(self, line: OrderLine) -> str:
19-
try:
20-
batch = next(
21-
b for b in sorted(self.batches) if b.can_allocate(line)
22-
)
23-
batch.allocate(line)
24-
self.version_number += 1
25-
return batch.reference
26-
except StopIteration:
27-
raise OutOfStock(f'Out of stock for sku {line.sku}')
11+
def allocate(line: OrderLine, batches: List[Batch]) -> str:
12+
try:
13+
batch = next(
14+
b for b in sorted(batches) if b.can_allocate(line)
15+
)
16+
batch.allocate(line)
17+
return batch.reference
18+
except StopIteration:
19+
raise OutOfStock(f'Out of stock for sku {line.sku}')
2820

2921

3022
@dataclass(unsafe_hash=True)

src/allocation/service_layer/services.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ class InvalidSku(Exception):
1111
pass
1212

1313

14+
def is_valid_sku(sku, batches):
15+
return sku in {b.sku for b in batches}
16+
17+
1418
def add_batch(
1519
ref: str, sku: str, qty: int, eta: Optional[date],
1620
uow: unit_of_work.AbstractUnitOfWork
1721
):
1822
with uow:
19-
product = uow.products.get(sku=sku)
20-
if product is None:
21-
product = model.Product(sku, batches=[])
22-
uow.products.add(product)
23-
product.batches.append(model.Batch(ref, sku, qty, eta))
23+
uow.batches.add(model.Batch(ref, sku, qty, eta))
2424
uow.commit()
2525

2626

@@ -30,9 +30,9 @@ def allocate(
3030
) -> str:
3131
line = OrderLine(orderid, sku, qty)
3232
with uow:
33-
product = uow.products.get(sku=line.sku)
34-
if product is None:
33+
batches = uow.batches.list()
34+
if not is_valid_sku(line.sku, batches):
3535
raise InvalidSku(f'Invalid sku {line.sku}')
36-
batchref = product.allocate(line)
36+
batchref = model.allocate(line, batches)
3737
uow.commit()
3838
return batchref

0 commit comments

Comments
 (0)