Skip to content

Commit

Permalink
refactor: refactor with dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
takahiromitsui committed Apr 19, 2024
1 parent b8f46f1 commit 7449d19
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 75 deletions.
107 changes: 33 additions & 74 deletions src/service_layer/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,48 @@
logger = logging.getLogger(__name__)


ENTITY_HANDLERS = {
Entity.PARTY.value: utils.prepare_party_data,
Entity.PARLIAMENT.value: utils.prepare_parliament_data,
Entity.PARLIAMENT_PERIOD.value: utils.prepare_parliament_period_data,
Entity.TOPIC.value: utils.prepare_topic_data,
Entity.COMMITTEE.value: utils.prepare_committee_data,
Entity.POLL.value: utils.prepare_poll_data,
}

DATA_HANDLERS = {
Entity.PARTY.value: [Entity.PARTY_STYLE.value, Entity.PARTY.value],
Entity.PARLIAMENT.value: [Entity.PARLIAMENT.value],
Entity.PARLIAMENT_PERIOD.value: [Entity.PARLIAMENT_PERIOD.value],
Entity.TOPIC.value: [Entity.TOPIC.value],
Entity.COMMITTEE.value: [Entity.COMMITTEE.value, Entity.COMMITTEE_HAS_TOPIC.value],
Entity.POLL.value: [Entity.POLL.value],
}


# Step 1
def fetch_missing_entity(command: commands.FetchMissingEntity) -> List[Any]:
repo = None
if command.entity == Entity.PARTY.value:
repo = repository.SqlAlchemyFactory(command.session).create_party_repository()
elif command.entity == Entity.PARLIAMENT.value:
repo = repository.SqlAlchemyFactory(
command.session
).create_parliament_repository()
elif command.entity == Entity.PARLIAMENT_PERIOD.value:
repo = repository.SqlAlchemyFactory(
command.session
).create_parliament_period_repository()
elif command.entity == Entity.TOPIC.value:
repo = repository.SqlAlchemyFactory(command.session).create_topic_repository()
elif command.entity == Entity.COMMITTEE.value:
repo = repository.SqlAlchemyFactory(
command.session
).create_committee_repository()
elif command.entity == Entity.POLL.value:
repo = repository.SqlAlchemyFactory(command.session).create_poll_repository()
repo = utils.get_repository(
repository.SqlAlchemyFactory(command.session), command.entity
)

if repo is not None:
missing_entity_data = utils.FetchMissingEntity(
command.entity, repo
).fetch_missing_entities()
return missing_entity_data

return []


# Step 2
def prepare_update_data(command: commands.PrepareUpdateData):
if command.entity == Entity.PARTY.value:
party_styles, parties = utils.prepare_party_data(command.data)
return {
"entities": [Entity.PARTY_STYLE.value, Entity.PARTY.value],
"data": [party_styles, parties],
}
elif command.entity == Entity.PARLIAMENT.value:
parliaments = utils.prepare_parliament_data(command.data)
return {"entities": [Entity.PARLIAMENT.value], "data": [parliaments]}
elif command.entity == Entity.PARLIAMENT_PERIOD.value:
parliament_periods = utils.prepare_parliament_period_data(command.data)
return {
"entities": [Entity.PARLIAMENT_PERIOD.value],
"data": [parliament_periods],
}
elif command.entity == Entity.TOPIC.value:
topics = utils.prepare_topic_data(command.data)
return {"entities": [Entity.TOPIC.value], "data": [topics]}
elif command.entity == Entity.COMMITTEE.value:
committees = utils.prepare_committee_data(command.data)
committee_has_topics = utils.prepare_committee_has_topic_data(command.data)
return {
"entities": [Entity.COMMITTEE.value, Entity.COMMITTEE_HAS_TOPIC.value],
"data": [committees, committee_has_topics],
}
elif command.entity == Entity.POLL.value:
polls = utils.prepare_poll_data(command.data)
return {"entities": [Entity.POLL.value], "data": [polls]}
entity = command.entity
data = command.data

if entity in ENTITY_HANDLERS:
entity_data = ENTITY_HANDLERS[entity](data)
return {"entities": DATA_HANDLERS[entity], "data": [entity_data]}
else:
return {"entities": [], "data": []}

Expand All @@ -84,34 +65,12 @@ def prepare_update_data(command: commands.PrepareUpdateData):
def update_table(command: commands.UpdateTable):
factory = repository.SqlAlchemyFactory(command.session)

for entity in command.entities:
if entity == Entity.PARTY_STYLE.value:
party_style_repo = factory.create_party_style_repository()
party_style_repo.add_or_update_list(command.data[0])
elif entity == Entity.PARTY.value:
party_repo = factory.create_party_repository()
party_repo.add_or_update_list(command.data[1])
elif entity == Entity.PARLIAMENT.value:
parliament_repo = factory.create_parliament_repository()
parliament_repo.add_or_update_list(command.data[0])
elif entity == Entity.PARLIAMENT_PERIOD.value:
parliament_period_repo = factory.create_parliament_period_repository()
parliament_period_repo.add_or_update_list(command.data[0])
elif entity == Entity.TOPIC.value:
topic_repo = factory.create_topic_repository()
topic_repo.add_or_update_list(command.data[0])
elif entity == Entity.COMMITTEE.value:
committee_repo = factory.create_committee_repository()
committee_repo.add_or_update_list(command.data[0])
elif entity == Entity.COMMITTEE_HAS_TOPIC.value:
committee_has_topic_repo = factory.create_committee_has_topic_repository()
committee_has_topic_repo.add_or_update_list(command.data[1])
elif entity == Entity.POLL.value:
poll_repo = factory.create_poll_repository()
poll_repo.add_or_update_list(command.data[0])

for entity, data in zip(command.entities, command.data):
repo = utils.get_repository(factory, entity)
if repo is not None:
repo.add_or_update_list(data)
else:
raise ValueError(f"Unknown entity: {entity}")
logger.error(f"Repository for entity {entity} not found.")


def publish_missing_entity_fetched_event(
Expand Down
18 changes: 17 additions & 1 deletion src/service_layer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from typing import List

# local
from src.domain.entities import Entity
from src.logging_config import configure_logging
from src.api.repository import SqlAlchemyBaseRepository
from src.api.repository import SqlAlchemyBaseRepository, SqlAlchemyFactory
import src.db.models as models


Expand Down Expand Up @@ -97,6 +98,21 @@ def fetch_missing_entities(self) -> List[models.Party]:
return data_list


def get_repository(factory: SqlAlchemyFactory, entity: str):
entity_factory_map = {
Entity.PARTY.value: factory.create_party_repository,
Entity.PARLIAMENT.value: factory.create_parliament_repository,
Entity.PARLIAMENT_PERIOD.value: factory.create_parliament_period_repository,
Entity.TOPIC.value: factory.create_topic_repository,
Entity.COMMITTEE.value: factory.create_committee_repository,
Entity.POLL.value: factory.create_poll_repository,
}
if entity in entity_factory_map:
return entity_factory_map[entity]()
else:
return None


def prepare_party_data(api_parties):
party_styles = [
{
Expand Down

0 comments on commit 7449d19

Please sign in to comment.