Skip to content

Commit

Permalink
refactor: refactor other tables with enum
Browse files Browse the repository at this point in the history
  • Loading branch information
takahiromitsui committed Apr 19, 2024
1 parent 1d82b38 commit b8f46f1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 83 deletions.
8 changes: 4 additions & 4 deletions src/entrypoints/redis_scheduled_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ def publish_entity():
entity_list = [
Entity.PARTY,
Entity.PARLIAMENT,
# Entity.PARLIAMENT_PERIOD,
# Entity.TOPIC,
# Entity.COMMITTEE,
# Entity.POLL,
Entity.PARLIAMENT_PERIOD,
Entity.TOPIC,
Entity.COMMITTEE,
Entity.POLL,
]

for entity in entity_list:
Expand Down
125 changes: 46 additions & 79 deletions src/service_layer/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ def fetch_missing_entity(command: commands.FetchMissingEntity) -> List[Any]:
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()

if repo is not None:
missing_entity_data = utils.FetchMissingEntity(
Expand All @@ -32,52 +44,6 @@ def fetch_missing_entity(command: commands.FetchMissingEntity) -> List[Any]:
return missing_entity_data
return []

# if command.entity == Entity.PARTY:
# repo = repository.SqlAlchemyFactory(command.session).create_party_repository()
# missing_party_data = utils.FetchMissingEntity(
# Entity.PARTY, repo
# ).fetch_missing_entities()
# return missing_party_data

# if command.entity == Entity.PARLIAMENT:
# repo = repository.SqlAlchemyFactory(
# command.session
# ).create_parliament_repository()
# missing_parliament_data = utils.FetchMissingEntity(
# Entity.PARLIAMENT, repo
# ).fetch_missing_entities()
# return missing_parliament_data

# if command.entity == "parliament-period":
# repo = repository.SqlAlchemyFactory(
# command.session
# ).create_parliament_period_repository()
# missing_parliament_period_data = utils.FetchMissingEntity(
# "parliament-periods", repo
# ).fetch_missing_entities()
# return missing_parliament_period_data
# if command.entity == "topic":
# repo = repository.SqlAlchemyFactory(command.session).create_topic_repository()
# missing_topic_data = utils.FetchMissingEntity(
# "topics", repo
# ).fetch_missing_entities()
# return missing_topic_data
# if command.entity == "committee":
# repo = repository.SqlAlchemyFactory(
# command.session
# ).create_committee_repository()
# missing_committee_data = utils.FetchMissingEntity(
# "committees", repo
# ).fetch_missing_entities()
# return missing_committee_data
# if command.entity == "poll":
# repo = repository.SqlAlchemyFactory(command.session).create_poll_repository()
# missing_poll_data = utils.FetchMissingEntity(
# "polls", repo
# ).fetch_missing_entities()
# return missing_poll_data
# return []


# Step 2
def prepare_update_data(command: commands.PrepareUpdateData):
Expand All @@ -87,26 +53,31 @@ def prepare_update_data(command: commands.PrepareUpdateData):
"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]}

else:
return {"entities": [], "data": []}
# if command.entity == "parliament-period":
# parliament_periods = utils.prepare_parliament_period_data(command.data)
# return {"entities": ["parliament_period"], "data": [parliament_periods]}
# if command.entity == "topic":
# topics = utils.prepare_topic_data(command.data)
# return {"entities": ["topic"], "data": [topics]}
# if command.entity == "committee":
# committees = utils.prepare_committee_data(command.data)
# committee_has_topics = utils.prepare_committee_has_topic_data(command.data)
# return {"entities": ["committee"], "data": [committees, committee_has_topics]}
# if command.entity == "poll":
# polls = utils.prepare_poll_data(command.data)
# return {"entities": ["poll"], "data": [polls]}


# Step 3
Expand All @@ -117,35 +88,31 @@ def update_table(command: commands.UpdateTable):
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])

else:
raise ValueError(f"Unknown entity: {entity}")

# if command.entities == ["parliament_period"]:
# parliament_period_repo = factory.create_parliament_period_repository()
# parliament_period_repo.add_or_update_list(command.data[0])
# if command.entities == ["topic"]:
# topic_repo = factory.create_topic_repository()
# topic_repo.add_or_update_list(command.data[0])
# if command.entities == ["committee"]:
# committee_repo = factory.create_committee_repository()
# committee_repo.add_or_update_list(command.data[0])
# committee_has_topic_repo = factory.create_committee_has_topic_repository()
# committee_has_topic_repo.add_or_update_list(command.data[1])
# if command.entities == ["poll"]:
# poll_repo = factory.create_poll_repository()
# poll_repo.add_or_update_list(command.data[0])
# # add poll has topic
# # add poll has field_link


def publish_missing_entity_fetched_event(
event: events.MissingEntityFetched,
Expand Down

0 comments on commit b8f46f1

Please sign in to comment.