2022-08-04 13:18:23 +00:00
|
|
|
from typing import cast, List
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-08-04 13:18:23 +00:00
|
|
|
from sqlalchemy import select
|
2022-12-25 07:56:52 +00:00
|
|
|
from sqlalchemy.exc import NoResultFound
|
2022-08-04 13:18:23 +00:00
|
|
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
from core.base.mysql import MySQL
|
|
|
|
from utils.error import RegionNotFoundError
|
|
|
|
from utils.models.base import RegionEnum
|
|
|
|
from .error import CookiesNotFoundError
|
2022-08-04 13:18:23 +00:00
|
|
|
from .models import HyperionCookie, HoyolabCookie, Cookies
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CookiesRepository:
|
|
|
|
def __init__(self, mysql: MySQL):
|
|
|
|
self.mysql = mysql
|
|
|
|
|
2022-08-05 12:11:46 +00:00
|
|
|
async def add_cookies(self, user_id: int, cookies: dict, region: RegionEnum):
|
2022-08-04 13:18:23 +00:00
|
|
|
async with self.mysql.Session() as session:
|
|
|
|
session = cast(AsyncSession, session)
|
|
|
|
if region == RegionEnum.HYPERION:
|
2022-08-05 14:45:54 +00:00
|
|
|
db_data = HyperionCookie(user_id=user_id, cookies=cookies)
|
2022-08-04 13:18:23 +00:00
|
|
|
elif region == RegionEnum.HOYOLAB:
|
2022-08-05 14:45:54 +00:00
|
|
|
db_data = HoyolabCookie(user_id=user_id, cookies=cookies)
|
2022-08-04 13:18:23 +00:00
|
|
|
else:
|
|
|
|
raise RegionNotFoundError(region.name)
|
2022-08-05 14:45:54 +00:00
|
|
|
session.add(db_data)
|
2022-08-05 12:05:33 +00:00
|
|
|
await session.commit()
|
2022-08-04 13:18:23 +00:00
|
|
|
|
2022-08-05 12:11:46 +00:00
|
|
|
async def update_cookies(self, user_id: int, cookies: dict, region: RegionEnum):
|
2022-08-04 13:18:23 +00:00
|
|
|
async with self.mysql.Session() as session:
|
|
|
|
session = cast(AsyncSession, session)
|
|
|
|
if region == RegionEnum.HYPERION:
|
|
|
|
statement = select(HyperionCookie).where(HyperionCookie.user_id == user_id)
|
|
|
|
elif region == RegionEnum.HOYOLAB:
|
2022-08-05 14:45:54 +00:00
|
|
|
statement = select(HoyolabCookie).where(HoyolabCookie.user_id == user_id)
|
2022-08-04 13:18:23 +00:00
|
|
|
else:
|
|
|
|
raise RegionNotFoundError(region.name)
|
2022-08-05 14:45:54 +00:00
|
|
|
results = await session.exec(statement)
|
2022-09-09 10:10:55 +00:00
|
|
|
db_cookies = results.first()
|
2022-08-05 14:45:54 +00:00
|
|
|
if db_cookies is None:
|
|
|
|
raise CookiesNotFoundError(user_id)
|
|
|
|
db_cookies = db_cookies[0]
|
|
|
|
db_cookies.cookies = cookies
|
|
|
|
session.add(db_cookies)
|
|
|
|
await session.commit()
|
|
|
|
await session.refresh(db_cookies)
|
2022-08-04 13:18:23 +00:00
|
|
|
|
|
|
|
async def update_cookies_ex(self, cookies: Cookies, region: RegionEnum):
|
|
|
|
async with self.mysql.Session() as session:
|
|
|
|
session = cast(AsyncSession, session)
|
2022-09-08 01:08:37 +00:00
|
|
|
if region not in [RegionEnum.HYPERION, RegionEnum.HOYOLAB]:
|
2022-08-04 13:18:23 +00:00
|
|
|
raise RegionNotFoundError(region.name)
|
2022-09-08 01:08:37 +00:00
|
|
|
session.add(cookies)
|
|
|
|
await session.commit()
|
|
|
|
await session.refresh(cookies)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-08-04 13:18:23 +00:00
|
|
|
async def get_cookies(self, user_id, region: RegionEnum) -> Cookies:
|
|
|
|
async with self.mysql.Session() as session:
|
|
|
|
session = cast(AsyncSession, session)
|
|
|
|
if region == RegionEnum.HYPERION:
|
|
|
|
statement = select(HyperionCookie).where(HyperionCookie.user_id == user_id)
|
|
|
|
results = await session.exec(statement)
|
|
|
|
db_cookies = results.first()
|
|
|
|
if db_cookies is None:
|
|
|
|
raise CookiesNotFoundError(user_id)
|
|
|
|
return db_cookies[0]
|
|
|
|
elif region == RegionEnum.HOYOLAB:
|
|
|
|
statement = select(HoyolabCookie).where(HoyolabCookie.user_id == user_id)
|
|
|
|
results = await session.exec(statement)
|
|
|
|
db_cookies = results.first()
|
|
|
|
if db_cookies is None:
|
|
|
|
raise CookiesNotFoundError(user_id)
|
|
|
|
return db_cookies[0]
|
|
|
|
else:
|
|
|
|
raise RegionNotFoundError(region.name)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-08-04 13:18:23 +00:00
|
|
|
async def get_all_cookies(self, region: RegionEnum) -> List[Cookies]:
|
|
|
|
async with self.mysql.Session() as session:
|
|
|
|
session = cast(AsyncSession, session)
|
|
|
|
if region == RegionEnum.HYPERION:
|
|
|
|
statement = select(HyperionCookie)
|
|
|
|
results = await session.exec(statement)
|
|
|
|
db_cookies = results.all()
|
|
|
|
return [cookies[0] for cookies in db_cookies]
|
|
|
|
elif region == RegionEnum.HOYOLAB:
|
|
|
|
statement = select(HoyolabCookie)
|
|
|
|
results = await session.exec(statement)
|
|
|
|
db_cookies = results.all()
|
|
|
|
return [cookies[0] for cookies in db_cookies]
|
|
|
|
else:
|
2022-10-07 10:20:08 +00:00
|
|
|
raise RegionNotFoundError(region.name)
|
2022-11-30 03:25:19 +00:00
|
|
|
|
|
|
|
async def del_cookies(self, user_id, region: RegionEnum):
|
|
|
|
async with self.mysql.Session() as session:
|
|
|
|
session = cast(AsyncSession, session)
|
|
|
|
if region == RegionEnum.HYPERION:
|
|
|
|
statement = select(HyperionCookie).where(HyperionCookie.user_id == user_id)
|
|
|
|
elif region == RegionEnum.HOYOLAB:
|
|
|
|
statement = select(HoyolabCookie).where(HoyolabCookie.user_id == user_id)
|
|
|
|
else:
|
|
|
|
raise RegionNotFoundError(region.name)
|
|
|
|
results = await session.execute(statement)
|
2022-12-25 07:56:52 +00:00
|
|
|
try:
|
|
|
|
db_cookies = results.unique().scalar_one()
|
|
|
|
except NoResultFound as exc:
|
|
|
|
raise CookiesNotFoundError(user_id) from exc
|
|
|
|
await session.delete(db_cookies)
|
|
|
|
await session.commit()
|