11from datetime import datetime
22from flask import Flask , jsonify , request
3- from allocation import exceptions , orm , handlers , unit_of_work
3+ from allocation import events , exceptions , messagebus , orm , unit_of_work
44
55app = Flask (__name__ )
66orm .start_mappers ()
@@ -11,22 +11,21 @@ def add_batch():
1111 eta = request .json ['eta' ]
1212 if eta is not None :
1313 eta = datetime .fromisoformat (eta ).date ()
14- handlers . add_batch (
14+ event = events . BatchCreated (
1515 request .json ['ref' ], request .json ['sku' ], request .json ['qty' ], eta ,
16- unit_of_work .SqlAlchemyUnitOfWork (),
1716 )
17+ messagebus .handle ([event ], unit_of_work .SqlAlchemyUnitOfWork ())
1818 return 'OK' , 201
1919
2020
2121@app .route ("/allocate" , methods = ['POST' ])
2222def allocate_endpoint ():
2323 try :
24- batchref = handlers .allocate (
25- request .json ['orderid' ],
26- request .json ['sku' ],
27- request .json ['qty' ],
28- unit_of_work .SqlAlchemyUnitOfWork (),
24+ event = events .AllocationRequest (
25+ request .json ['orderid' ], request .json ['sku' ], request .json ['qty' ],
2926 )
27+ results = messagebus .handle ([event ], unit_of_work .SqlAlchemyUnitOfWork ())
28+ batchref = results .pop ()
3029 except exceptions .InvalidSku as e :
3130 return jsonify ({'message' : str (e )}), 400
3231
0 commit comments