Skip to content

Commit

Permalink
fix bugs in favourites repo & change specific func to be more generic…
Browse files Browse the repository at this point in the history
… fetch_one
  • Loading branch information
cmyui committed Feb 15, 2024
1 parent d76f443 commit 5466c12
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/api/domains/osu.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ async def osuAddFavourite(
map_set_id: int = Query(..., alias="a"),
) -> Response:
# check if they already have this favourited.
if await favourites_repo.is_already_favourite(player.id, map_set_id):
if await favourites_repo.fetch_one(player.id, map_set_id):
return Response(b"You've already favourited this beatmap!")

# add favourite
Expand Down
24 changes: 13 additions & 11 deletions app/repositories/favourites.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

import app.state.services

# +--------------+------------------------+------+-----+---------+----------------+
# | Field | Type | Null | Key | Default | Extra |
# +--------------+------------------------+------+-----+---------+----------------+
# | userid | int | NO | PRI | NULL | |
# | setid | int | NO | | NULL | |
# | created_at | int | NO | | 0 | |
# +--------------+------------------------+------+-----+---------+----------------+
# +------------+------+------+-----+---------+-------+
# | Field | Type | Null | Key | Default | Extra |
# +------------+------+------+-----+---------+-------+
# | userid | int | NO | PRI | NULL | |
# | setid | int | NO | PRI | NULL | |
# | created_at | int | NO | | 0 | |
# +------------+------+------+-----+---------+-------+

READ_PARAMS = textwrap.dedent(
"""\
Expand Down Expand Up @@ -48,9 +48,11 @@ async def create(
SELECT {READ_PARAMS}
FROM favourites
WHERE userid = :userid
AND setid = :setid
"""
params = {
"userid": rec_id,
"setid": setid,
}
favourite = await app.state.services.database.fetch_one(query, params)

Expand All @@ -73,10 +75,10 @@ async def fetch_all(userid: int) -> list[Favourite]:
return cast(list[Favourite], [dict(f._mapping) for f in favourites])


async def is_already_favourite(userid: int, setid: int) -> bool:
async def fetch_one(userid: int, setid: int) -> Favourite | None:
"""Check if a mapset is already a favourite."""
query = f"""\
SELECT 1
SELECT {READ_PARAMS}
FROM favourites
WHERE userid = :userid
AND setid = :setid
Expand All @@ -86,5 +88,5 @@ async def is_already_favourite(userid: int, setid: int) -> bool:
"setid": setid,
}

is_already_favourite = await app.state.services.database.fetch_one(query, params)
return bool(is_already_favourite)
favourite = await app.state.services.database.fetch_one(query, params)
return cast(Favourite, dict(favourite._mapping)) if favourite else None

0 comments on commit 5466c12

Please sign in to comment.