Skip to content

Commit f784d74

Browse files
committed
reset model, orm, repo and services to previous chap version
1 parent dfaa46a commit f784d74

4 files changed

Lines changed: 33 additions & 43 deletions

File tree

‎src/allocation/adapters/orm.py‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,12 @@
1515
Column("orderid", String(255)),
1616
)
1717

18-
products = Table(
19-
"products",
20-
metadata,
21-
Column("sku", String(255), primary_key=True),
22-
Column("version_number", Integer, nullable=False, server_default="0"),
23-
)
24-
2518
batches = Table(
2619
"batches",
2720
metadata,
2821
Column("id", Integer, primary_key=True, autoincrement=True),
2922
Column("reference", String(255)),
30-
Column("sku", ForeignKey("products.sku")),
23+
Column("sku", String(255)),
3124
Column("_purchased_quantity", Integer, nullable=False),
3225
Column("eta", Date, nullable=True),
3326
)
@@ -43,7 +36,7 @@
4336

4437
def start_mappers():
4538
lines_mapper = mapper(model.OrderLine, order_lines)
46-
batches_mapper = mapper(
39+
mapper(
4740
model.Batch,
4841
batches,
4942
properties={
@@ -54,6 +47,3 @@ def start_mappers():
5447
)
5548
},
5649
)
57-
mapper(
58-
model.Product, products, properties={"batches": relationship(batches_mapper)}
59-
)

‎src/allocation/adapters/repository.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33

44

55
class AbstractRepository(abc.ABC):
6-
@abc.abstractmethod
7-
def add(self, product: model.Product):
6+
def add(self, batch: model.Batch):
87
raise NotImplementedError
98

109
@abc.abstractmethod
11-
def get(self, sku) -> model.Product:
10+
def get(self, reference) -> model.Batch:
1211
raise NotImplementedError
1312

1413

1514
class SqlAlchemyRepository(AbstractRepository):
1615
def __init__(self, session):
1716
self.session = session
1817

19-
def add(self, product):
20-
self.session.add(product)
18+
def add(self, batch):
19+
self.session.add(batch)
20+
21+
def get(self, reference):
22+
return self.session.query(model.Batch).filter_by(reference=reference).one()
2123

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

‎src/allocation/domain/model.py‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,13 @@ class OutOfStock(Exception):
88
pass
99

1010

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

2619

2720
@dataclass(unsafe_hash=True)

‎src/allocation/service_layer/services.py‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,33 @@ 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(
15-
ref: str, sku: str, qty: int, eta: Optional[date],
19+
ref: str,
20+
sku: str,
21+
qty: int,
22+
eta: Optional[date],
1623
uow: unit_of_work.AbstractUnitOfWork,
1724
):
1825
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))
26+
uow.batches.add(model.Batch(ref, sku, qty, eta))
2427
uow.commit()
2528

2629

2730
def allocate(
28-
orderid: str, sku: str, qty: int,
31+
orderid: str,
32+
sku: str,
33+
qty: int,
2934
uow: unit_of_work.AbstractUnitOfWork,
3035
) -> str:
3136
line = OrderLine(orderid, sku, qty)
3237
with uow:
33-
product = uow.products.get(sku=line.sku)
34-
if product is None:
38+
batches = uow.batches.list()
39+
if not is_valid_sku(line.sku, batches):
3540
raise InvalidSku(f"Invalid sku {line.sku}")
36-
batchref = product.allocate(line)
41+
batchref = model.allocate(line, batches)
3742
uow.commit()
3843
return batchref

0 commit comments

Comments
 (0)