def bootstrap( start_orm: bool = True, uwo: unit_of_work.AbstractUnitOfWork = unit_of_work.SqlAlchemyUnitOfWork(), notifications: notifications.AbstractNotifications = notifications. EmailNotification(), publish: Callable = redis_eventpublisher.publish, ) -> messagebus.MessageBus: if start_orm: orm.start_mappers() dependencies = {'uow': uow, 'send_mail': send_mail, 'publish': publish} injected_event_handlers = { event_type: [ inject_dependencies(handler, dependencies) for handler in event_handlers ] for event_type, event_handlers in handlers.EVENT_HANDLERS.items() } injected_command_hanlers = { command_type: inject_dependencies(handler, dependencies) for command_type, handler in handlers.COMMAND_HANLDERS.items() } return messagebus.MessageBus( uow=uow, event_handlers=injected_event_handlers, command_handlers=injected_command_hanlers, )
def bootstrap( start_orm: bool = True, uow: unit_of_work.AbstractUnitOfWork = unit_of_work.SqlAlchemyUnitOfWork(), notifications: AbstractNotifications = None, publish: Callable = redis_eventpublisher.publish, ) -> messagebus.MessageBus: if notifications is None: notifications = EmailNotifications() if start_orm: orm.start_mappers() dependencies = { "uow": uow, "notifications": notifications, "publish": publish } injected_event_handlers = { event_type: [ inject_dependices(handler, dependencies) for handler in event_handlers ] for event_type, event_handlers in handlers.EVENT_HANDLERS.items() } injected_event_commands = { command_type: inject_dependices(handler, dependencies) for command_type, handler in handlers.COMMAND_HANDLERS.items() } return messagebus.MessageBus( uow=uow, event_handlers=injected_event_handlers, command_handlers=injected_event_commands, )
def main(): orm.start_mappers() pubsub = r.pubsub(ignore_subscribe_messages=True) pubsub.subscribe('change_batch_quantity') for m in pubsub.listen(): handle_change_batch_quantity(m)
def main(): orm.start_mappers() pubsub = r.pubsub(ignore_subscribe_messages=True) pubsub.subscribe('line_allocated') logging.info(" message:" + str(pubsub.get_message())) for m in pubsub.listen(): handle_change_batch_quantity(m)
def bootstrap(start_orm: bool = True, uow: unit_of_work.AbstractUnitOfWork = unit_of_work.ProductSqlAlchemyUnitOfWork(), notifications: AbstractNotifications = EmailNotification(), publish: Callable = redis_eventpublisher.publish, ) -> MessageBus: if start_orm: print('vai gerar a base de dados: ', config.get_postgres_uri()) orm.start_mappers() dependencies = {'uow': uow, 'send_mail': notifications, 'publish': publish} inject_event_handlers = { event_type: [ inject_dependencies(handler, dependencies) for handler in event_handlers ] for event_type, event_handlers in handlers.EVENT_HANDLERS.items() } inject_command_handlers = { command_type: inject_dependencies(handler, dependencies) for command_type, handler in handlers.COMMAND_HANDLERS.items() } """ inject_event_handlers = { events.Allocated: [ lambda e: handlers.publish_allocated_event(e, publish), lambda e: handlers.add_allocation_to_read_model(e, uow), ], events.Deallocated: [ lambda e: handlers.remove_allocation_from_read_model(e, uow), lambda e: handlers.reallocate(e, uow), ], events.OutOfStock: [ lambda e: handlers.send_out_of_stock_notification(e, send_mail), ] } inject_command_handlers = { commands.Allocate: lambda c: handlers.allocate(c, uow), commands.CreateBatch: lambda c: handlers.add_batch(c, uow), commands.ChangeBatchQuantity: lambda c: handlers.change_batch_quantity(c, uow), } """ return MessageBus( uow=uow, event_handlers=inject_event_handlers, command_handlers=inject_command_handlers, )
def inicializar_bd(): engine = create_engine( 'postgres://*****:*****@tuffi.db.elephantsql.com:5432/edhyrpuf', ) # enlaza clases con tablas start_mappers() # crea las tablas en la base de datos (no importa el start mapper! metadata.create_all(engine) # enlaza la sesion con la base de datos Session = sessionmaker(bind=engine) # crea una sesion return Session()
from datetime import datetime from flask import Flask, jsonify, request from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker import config from domain import model from adapters import orm, repository from service_layer import services orm.start_mappers() get_session = sessionmaker(bind=create_engine(config.get_postgres_uri())) app = Flask(__name__) @app.route("/add_batch", methods=['POST']) def add_batch(): session = get_session() repo = repository.SqlAlchemyRepository(session) eta = request.json['eta'] if eta is not None: eta = datetime.fromisoformat(eta).date() services.add_batch( request.json['ref'], request.json['sku'], request.json['qty'], eta, repo, session ) return 'OK', 201 @app.route("/allocate", methods=['POST']) def allocate_endpoint():
def mappers(): start_mappers() yield clear_mappers()
def session(in_memory_db): start_mappers() yield sessionmaker(bind=in_memory_db)() clear_mappers()
def postgres_session(postgres_db): start_mappers() yield sessionmaker(bind=postgres_db)() clear_mappers()
def postgres_session_factory(postgres_db): start_mappers() yield sessionmaker(bind=postgres_db) clear_mappers()
def session_factory_real_db(in_real_db): start_mappers() yield sessionmaker(bind=in_real_db) clear_mappers()