From 4cd45c69abb4af21652f604f3bcf80971055c634 Mon Sep 17 00:00:00 2001 From: Itamar Shefi Date: Tue, 5 Mar 2024 09:54:40 +0200 Subject: [PATCH] Refactor: get rid of one query --- logic/user_logic.py | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/logic/user_logic.py b/logic/user_logic.py index d7f6149..3e22a67 100644 --- a/logic/user_logic.py +++ b/logic/user_logic.py @@ -129,20 +129,10 @@ def __init__( async def update_and_get_history( self, guess: schemas.DistanceResponse ) -> list[schemas.DistanceResponse]: - history = await self.get_history() if guess.similarity is not None: - history.append(guess) with hs_transaction(self.session) as session: - count_query = select(func.count()) - count_query = count_query.select_from(tables.UserHistory) - count_query = count_query.where( - tables.UserHistory.user_id == self.user.id - ) - count_query = count_query.where(tables.UserHistory.guess == guess.guess) - count_query = count_query.where( - tables.UserHistory.game_date == self.date - ) - if session.exec(count_query).one() == 0: + history = await self._begun_get_history(session) + if guess.guess not in [h.guess for h in history]: session.add( tables.UserHistory( user_id=self.user.id, @@ -154,21 +144,23 @@ async def update_and_get_history( solver_count=guess.solver_count, ) ) + history.append(guess) return history else: - return [guess] + history + return [guess] + await self.get_history() async def get_history(self) -> list[schemas.DistanceResponse]: with hs_transaction(self.session, expire_on_commit=False) as session: - history_query = select(tables.UserHistory) - history_query = history_query.where( - tables.UserHistory.user_id == self.user.id - ) - history_query = history_query.where( - tables.UserHistory.game_date == self.date - ) - history_query = history_query.order_by(col(tables.UserHistory.id)) - history = session.exec(history_query).all() + return await self._begun_get_history(session=session) + + async def _begun_get_history( + self, session: Session + ) -> list[schemas.DistanceResponse]: + history_query = select(tables.UserHistory) + history_query = history_query.where(tables.UserHistory.user_id == self.user.id) + history_query = history_query.where(tables.UserHistory.game_date == self.date) + history_query = history_query.order_by(col(tables.UserHistory.id)) + history = session.exec(history_query).all() return [ schemas.DistanceResponse( guess=historia.guess,