refactor: Simplify repository classes by inheriting from BaseRepository and removing redundant methods
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
"""Repository for user OAuth operations."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from sqlmodel import select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
from app.core.logging import get_logger
|
||||
from app.models.user_oauth import UserOauth
|
||||
from app.repositories.base import BaseRepository
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class UserOauthRepository:
|
||||
class UserOauthRepository(BaseRepository[UserOauth]):
|
||||
"""Repository for user OAuth operations."""
|
||||
|
||||
def __init__(self, session: AsyncSession) -> None:
|
||||
"""Initialize repository with database session."""
|
||||
self.session = session
|
||||
super().__init__(UserOauth, session)
|
||||
|
||||
async def get_by_provider_user_id(
|
||||
self,
|
||||
@@ -61,57 +61,3 @@ class UserOauthRepository:
|
||||
else:
|
||||
return result.first()
|
||||
|
||||
async def create(self, oauth_data: dict[str, Any]) -> UserOauth:
|
||||
"""Create a new user OAuth record."""
|
||||
try:
|
||||
oauth = UserOauth(**oauth_data)
|
||||
self.session.add(oauth)
|
||||
await self.session.commit()
|
||||
await self.session.refresh(oauth)
|
||||
logger.info(
|
||||
"Created OAuth link for user %s with provider %s",
|
||||
oauth.user_id,
|
||||
oauth.provider,
|
||||
)
|
||||
except Exception:
|
||||
await self.session.rollback()
|
||||
logger.exception("Failed to create user OAuth")
|
||||
raise
|
||||
else:
|
||||
return oauth
|
||||
|
||||
async def update(self, oauth: UserOauth, update_data: dict[str, Any]) -> UserOauth:
|
||||
"""Update a user OAuth record."""
|
||||
try:
|
||||
for key, value in update_data.items():
|
||||
setattr(oauth, key, value)
|
||||
|
||||
self.session.add(oauth)
|
||||
await self.session.commit()
|
||||
await self.session.refresh(oauth)
|
||||
logger.info(
|
||||
"Updated OAuth link for user %s with provider %s",
|
||||
oauth.user_id,
|
||||
oauth.provider,
|
||||
)
|
||||
except Exception:
|
||||
await self.session.rollback()
|
||||
logger.exception("Failed to update user OAuth")
|
||||
raise
|
||||
else:
|
||||
return oauth
|
||||
|
||||
async def delete(self, oauth: UserOauth) -> None:
|
||||
"""Delete a user OAuth record."""
|
||||
try:
|
||||
await self.session.delete(oauth)
|
||||
await self.session.commit()
|
||||
logger.info(
|
||||
"Deleted OAuth link for user %s with provider %s",
|
||||
oauth.user_id,
|
||||
oauth.provider,
|
||||
)
|
||||
except Exception:
|
||||
await self.session.rollback()
|
||||
logger.exception("Failed to delete user OAuth")
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user