Skip to content

Commit d0593d3

Browse files
committed
Use the ORM instead [view_using_orm]
1 parent c579683 commit d0593d3

File tree

3 files changed

+7
-20
lines changed

3 files changed

+7
-20
lines changed

src/allocation/adapters/repository.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,3 @@ def _get_by_batchref(self, batchref):
5757
)
5858
.first()
5959
)
60-
61-
def for_order(self, orderid):
62-
order_lines = self.session.query(model.OrderLine).filter_by(orderid=orderid)
63-
skus = {l.sku for l in order_lines}
64-
return (
65-
self.session.query(model.Product)
66-
.join(model.Batch)
67-
.filter(model.Batch.sku.in_(skus))
68-
)

src/allocation/domain/model.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ def __gt__(self, other):
7171
return True
7272
return self.eta > other.eta
7373

74-
@property
75-
def orderids(self):
76-
return {l.orderid for l in self._allocations}
77-
7874
def allocate(self, line: OrderLine):
7975
if self.can_allocate(line):
8076
self._allocations.add(line)

src/allocation/views.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
from allocation.domain import model
12
from allocation.service_layer import unit_of_work
23

34

45
def allocations(orderid: str, uow: unit_of_work.SqlAlchemyUnitOfWork):
56
with uow:
6-
products = uow.products.for_order(orderid=orderid)
7-
batches = [b for p in products for b in p.batches]
8-
return [
9-
{"sku": b.sku, "batchref": b.reference}
10-
for b in batches
11-
if orderid in b.orderids
12-
]
7+
batches = (
8+
uow.session.query(model.Batch)
9+
.join(model.OrderLine, model.Batch._allocations)
10+
.filter(model.OrderLine.orderid == orderid)
11+
)
12+
return [{"sku": b.sku, "batchref": b.reference} for b in batches]

0 commit comments

Comments
 (0)