DispelEvilBot/common/repositories/shop_repo.py

38 lines
1.6 KiB
Python

"""Доступ к данным магазина (звания, донаты)."""
from __future__ import annotations
from typing import Any
from common.db.pool import get_pool
async def get_titles_list() -> list[dict[str, Any]]:
"""Доступные к покупке звания (id_title, title, price)."""
async with get_pool().connection() as conn, conn.cursor() as cur:
await cur.execute("SELECT * FROM bot.get_titles_list()")
return await cur.fetchall()
async def get_donats_list() -> list[dict[str, Any]]:
"""Доступные к покупке донаты (id_donat, title, price)."""
async with get_pool().connection() as conn, conn.cursor() as cur:
await cur.execute("SELECT * FROM bot.get_donats_list()")
return await cur.fetchall()
async def buy_title(id_user: int, id_title: int) -> int:
"""Купить звание. Возвращает 1 при успехе, -1 если не хватает денег."""
async with get_pool().connection() as conn, conn.cursor() as cur:
await cur.execute("SELECT bot.buy_title(%s, %s) AS res", (id_user, id_title))
row = await cur.fetchone()
return int(row["res"]) if row else -1
async def buy_donat(id_user: int, id_donat: int) -> str:
"""Купить донат. Возвращает промокод или '-1' при неуспехе."""
async with get_pool().connection() as conn, conn.cursor() as cur:
await cur.execute("SELECT bot.buy_donat(%s, %s) AS res", (id_user, id_donat))
row = await cur.fetchone()
return str(row["res"]) if row else "-1"