import uuid from sqlalchemy import delete, func, select from sqlalchemy.dialects.postgresql import insert from sqlalchemy.ext.asyncio import AsyncSession from common.database.model.models import MyGak class MyGakCRUD: async def of_user(self, s: AsyncSession, user_id: uuid.UUID) -> list[MyGak]: return list((await s.scalars(select(MyGak).where(MyGak.user_id == user_id).order_by(MyGak.my_gak_id))).all()) async def count(self, s: AsyncSession, user_id: uuid.UUID) -> int: return await s.scalar(select(func.count()).select_from(MyGak).where(MyGak.user_id == user_id)) async def add(self, s: AsyncSession, user_id: uuid.UUID, a: int, b: int) -> MyGak: """같은 조합이 이미 있으면 그것을 돌려준다.""" await s.execute(insert(MyGak).values(user_id=user_id, index_a=a, index_b=b).on_conflict_do_nothing()) return await s.scalar(select(MyGak).where(MyGak.user_id == user_id, MyGak.index_a == a, MyGak.index_b == b)) async def delete(self, s: AsyncSession, user_id: uuid.UUID, my_gak_id: int) -> bool: res = await s.execute(delete(MyGak).where(MyGak.my_gak_id == my_gak_id, MyGak.user_id == user_id).returning(MyGak.my_gak_id)) return res.first() is not None