add claude
This commit is contained in:
1
api/app/repositories/__init__.py
Normal file
1
api/app/repositories/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# Repositories Package
|
65
api/app/repositories/base.py
Normal file
65
api/app/repositories/base.py
Normal file
@ -0,0 +1,65 @@
|
||||
"""Base repository class"""
|
||||
from typing import Generic, Type, TypeVar, Optional, List
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, update, delete
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from app.db.base import Base
|
||||
|
||||
ModelType = TypeVar("ModelType", bound=Base)
|
||||
|
||||
|
||||
class BaseRepository(Generic[ModelType]):
|
||||
"""Base repository with common CRUD operations"""
|
||||
|
||||
def __init__(self, model: Type[ModelType], session: AsyncSession):
|
||||
self.model = model
|
||||
self.session = session
|
||||
|
||||
async def create(self, **kwargs) -> ModelType:
|
||||
"""Create a new record"""
|
||||
instance = self.model(**kwargs)
|
||||
self.session.add(instance)
|
||||
await self.session.flush()
|
||||
return instance
|
||||
|
||||
async def get(self, id: int) -> Optional[ModelType]:
|
||||
"""Get a record by ID"""
|
||||
result = await self.session.execute(
|
||||
select(self.model).where(self.model.id == id)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
async def get_multi(
|
||||
self,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
**filters
|
||||
) -> List[ModelType]:
|
||||
"""Get multiple records with pagination"""
|
||||
query = select(self.model)
|
||||
|
||||
# Apply filters
|
||||
for key, value in filters.items():
|
||||
if hasattr(self.model, key):
|
||||
query = query.where(getattr(self.model, key) == value)
|
||||
|
||||
query = query.offset(skip).limit(limit)
|
||||
result = await self.session.execute(query)
|
||||
return result.scalars().all()
|
||||
|
||||
async def update(self, id: int, **kwargs) -> Optional[ModelType]:
|
||||
"""Update a record"""
|
||||
await self.session.execute(
|
||||
update(self.model)
|
||||
.where(self.model.id == id)
|
||||
.values(**kwargs)
|
||||
)
|
||||
return await self.get(id)
|
||||
|
||||
async def delete(self, id: int) -> bool:
|
||||
"""Delete a record"""
|
||||
result = await self.session.execute(
|
||||
delete(self.model).where(self.model.id == id)
|
||||
)
|
||||
return result.rowcount > 0
|
136
api/app/repositories/card.py
Normal file
136
api/app/repositories/card.py
Normal file
@ -0,0 +1,136 @@
|
||||
"""Card repository"""
|
||||
from typing import List, Optional
|
||||
from datetime import datetime
|
||||
from sqlalchemy import select, and_, func
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
import uuid
|
||||
|
||||
from app.repositories.base import BaseRepository
|
||||
from app.db.models import UserCard, UniqueCardRegistry, CardMaster
|
||||
from app.models.card import CardRarity
|
||||
|
||||
|
||||
class CardRepository(BaseRepository[UserCard]):
|
||||
"""Card repository with custom methods"""
|
||||
|
||||
def __init__(self, session: AsyncSession):
|
||||
super().__init__(UserCard, session)
|
||||
|
||||
async def get_user_cards(
|
||||
self,
|
||||
user_id: int,
|
||||
skip: int = 0,
|
||||
limit: int = 100
|
||||
) -> List[UserCard]:
|
||||
"""Get all cards for a user"""
|
||||
result = await self.session.execute(
|
||||
select(UserCard)
|
||||
.options(selectinload(UserCard.card_info))
|
||||
.where(UserCard.user_id == user_id)
|
||||
.order_by(UserCard.obtained_at.desc())
|
||||
.offset(skip)
|
||||
.limit(limit)
|
||||
)
|
||||
return result.scalars().all()
|
||||
|
||||
async def count_user_cards(self, user_id: int, card_id: int) -> int:
|
||||
"""Count how many of a specific card a user has"""
|
||||
result = await self.session.execute(
|
||||
select(func.count(UserCard.id))
|
||||
.where(
|
||||
and_(
|
||||
UserCard.user_id == user_id,
|
||||
UserCard.card_id == card_id
|
||||
)
|
||||
)
|
||||
)
|
||||
return result.scalar() or 0
|
||||
|
||||
async def create_user_card(
|
||||
self,
|
||||
user_id: int,
|
||||
card_id: int,
|
||||
cp: int,
|
||||
status: CardRarity,
|
||||
skill: Optional[str] = None,
|
||||
is_unique: bool = False
|
||||
) -> UserCard:
|
||||
"""Create a new user card"""
|
||||
unique_id = None
|
||||
if is_unique:
|
||||
unique_id = uuid.uuid4()
|
||||
|
||||
card = await self.create(
|
||||
user_id=user_id,
|
||||
card_id=card_id,
|
||||
cp=cp,
|
||||
status=status,
|
||||
skill=skill,
|
||||
is_unique=is_unique,
|
||||
unique_id=unique_id
|
||||
)
|
||||
|
||||
# If unique, register it globally
|
||||
if is_unique:
|
||||
await self._register_unique_card(card)
|
||||
|
||||
return card
|
||||
|
||||
async def _register_unique_card(self, card: UserCard):
|
||||
"""Register a unique card in the global registry"""
|
||||
# Get user DID
|
||||
user_did = await self.session.execute(
|
||||
select(User.did).where(User.id == card.user_id)
|
||||
)
|
||||
user_did = user_did.scalar()
|
||||
|
||||
registry = UniqueCardRegistry(
|
||||
unique_id=card.unique_id,
|
||||
card_id=card.card_id,
|
||||
owner_did=user_did,
|
||||
obtained_at=card.obtained_at
|
||||
)
|
||||
self.session.add(registry)
|
||||
|
||||
|
||||
class UniqueCardRepository(BaseRepository[UniqueCardRegistry]):
|
||||
"""Unique card registry repository"""
|
||||
|
||||
def __init__(self, session: AsyncSession):
|
||||
super().__init__(UniqueCardRegistry, session)
|
||||
|
||||
async def is_card_available(self, card_id: int) -> bool:
|
||||
"""Check if a unique card is still available"""
|
||||
result = await self.session.execute(
|
||||
select(func.count(UniqueCardRegistry.id))
|
||||
.where(UniqueCardRegistry.card_id == card_id)
|
||||
)
|
||||
count = result.scalar() or 0
|
||||
return count == 0
|
||||
|
||||
async def get_all_unique_cards(self) -> List[UniqueCardRegistry]:
|
||||
"""Get all registered unique cards"""
|
||||
result = await self.session.execute(
|
||||
select(UniqueCardRegistry)
|
||||
.order_by(UniqueCardRegistry.obtained_at.desc())
|
||||
)
|
||||
return result.scalars().all()
|
||||
|
||||
async def get_available_unique_cards(self) -> List[int]:
|
||||
"""Get list of card IDs that are still available as unique"""
|
||||
# Get all card IDs
|
||||
all_card_ids = set(range(16))
|
||||
|
||||
# Get taken card IDs
|
||||
result = await self.session.execute(
|
||||
select(UniqueCardRegistry.card_id).distinct()
|
||||
)
|
||||
taken_ids = set(result.scalars().all())
|
||||
|
||||
# Return available IDs
|
||||
return list(all_card_ids - taken_ids)
|
||||
|
||||
|
||||
# Import User model here to avoid circular import
|
||||
from app.db.models import User
|
38
api/app/repositories/user.py
Normal file
38
api/app/repositories/user.py
Normal file
@ -0,0 +1,38 @@
|
||||
"""User repository"""
|
||||
from typing import Optional
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from app.repositories.base import BaseRepository
|
||||
from app.db.models import User
|
||||
|
||||
|
||||
class UserRepository(BaseRepository[User]):
|
||||
"""User repository with custom methods"""
|
||||
|
||||
def __init__(self, session: AsyncSession):
|
||||
super().__init__(User, session)
|
||||
|
||||
async def get_by_did(self, did: str) -> Optional[User]:
|
||||
"""Get user by DID"""
|
||||
result = await self.session.execute(
|
||||
select(User).where(User.did == did)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
async def get_or_create(self, did: str, handle: Optional[str] = None) -> User:
|
||||
"""Get existing user or create new one"""
|
||||
user = await self.get_by_did(did)
|
||||
if not user:
|
||||
user = await self.create(did=did, handle=handle)
|
||||
return user
|
||||
|
||||
async def get_with_cards(self, user_id: int) -> Optional[User]:
|
||||
"""Get user with all their cards"""
|
||||
result = await self.session.execute(
|
||||
select(User)
|
||||
.options(selectinload(User.cards))
|
||||
.where(User.id == user_id)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
Reference in New Issue
Block a user