Skip to content

Commit

Permalink
Do not insert try to insert duplicate guesses
Browse files Browse the repository at this point in the history
  • Loading branch information
ishefi committed Mar 5, 2024
1 parent 818d2bd commit 07a8d97
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions logic/user_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,7 @@ def __init__(
):
self.session = session
self.user = user
self.dt = date # TODO: use this
self.date = str(date)

@property
def projection(self) -> dict[str, str]:
return {"history": f"$history.{self.date}"}

@property
def user_filter(self) -> dict[str, str]:
return {"email": self.user.email}
self.date = date # TODO: use this

async def update_and_get_history(
self, guess: schemas.DistanceResponse
Expand All @@ -142,17 +133,27 @@ async def update_and_get_history(
if guess.similarity is not None:
history.append(guess)
with hs_transaction(self.session) as session:
session.add(
tables.UserHistory(
user_id=self.user.id,
guess=guess.guess,
similarity=guess.similarity,
distance=guess.distance,
egg=guess.egg,
game_date=self.dt,
solver_count=guess.solver_count,
)
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:
session.add(
tables.UserHistory(
user_id=self.user.id,
guess=guess.guess,
similarity=guess.similarity,
distance=guess.distance,
egg=guess.egg,
game_date=self.date,
solver_count=guess.solver_count,
)
)
return history
else:
return [guess] + history
Expand Down

0 comments on commit 07a8d97

Please sign in to comment.