|
3 | 3 | from datetime import UTC, datetime |
4 | 4 |
|
5 | 5 | from sqlmodel import Session, select |
| 6 | +from sqlmodel.ext.asyncio.session import AsyncSession |
6 | 7 |
|
7 | 8 | from app.core.security import get_password_hash |
8 | 9 | from app.models.user import User, UserCreate |
@@ -78,3 +79,77 @@ def find_existing_emails_with_prefix(self, prefix: str, domain: str) -> list[str |
78 | 79 | statement = select(User.email).where(User.email.like(pattern)) |
79 | 80 | result = self.db.exec(statement) |
80 | 81 | return list(result.all()) |
| 82 | + |
| 83 | + |
| 84 | +class AsyncUserService: |
| 85 | + """Async service for managing users with non-blocking database operations.""" |
| 86 | + |
| 87 | + def __init__(self, db: AsyncSession): |
| 88 | + self.db = db |
| 89 | + |
| 90 | + async def create_user(self, user_data: UserCreate) -> User: |
| 91 | + """Create a new user asynchronously.""" |
| 92 | + # Hash the password |
| 93 | + hashed_password = get_password_hash(user_data.password) |
| 94 | + |
| 95 | + # Create user object |
| 96 | + user = User( |
| 97 | + email=user_data.email, |
| 98 | + full_name=user_data.full_name, |
| 99 | + hashed_password=hashed_password, |
| 100 | + is_active=user_data.is_active, |
| 101 | + created_at=datetime.now(UTC), |
| 102 | + ) |
| 103 | + |
| 104 | + # Save to database |
| 105 | + self.db.add(user) |
| 106 | + await self.db.commit() |
| 107 | + await self.db.refresh(user) |
| 108 | + |
| 109 | + return user |
| 110 | + |
| 111 | + async def get_user_by_email(self, email: str) -> User | None: |
| 112 | + """Get user by email address asynchronously.""" |
| 113 | + statement = select(User).where(User.email == email) |
| 114 | + result = await self.db.exec(statement) |
| 115 | + return result.first() |
| 116 | + |
| 117 | + async def get_user_by_id(self, user_id: int) -> User | None: |
| 118 | + """Get user by ID asynchronously.""" |
| 119 | + return await self.db.get(User, user_id) |
| 120 | + |
| 121 | + async def update_user(self, user_id: int, **updates) -> User | None: |
| 122 | + """Update user data asynchronously.""" |
| 123 | + user = await self.get_user_by_id(user_id) |
| 124 | + if not user: |
| 125 | + return None |
| 126 | + |
| 127 | + for field, value in updates.items(): |
| 128 | + if hasattr(user, field): |
| 129 | + setattr(user, field, value) |
| 130 | + |
| 131 | + user.updated_at = datetime.now(UTC) |
| 132 | + self.db.add(user) |
| 133 | + await self.db.commit() |
| 134 | + await self.db.refresh(user) |
| 135 | + |
| 136 | + return user |
| 137 | + |
| 138 | + async def deactivate_user(self, user_id: int) -> User | None: |
| 139 | + """Deactivate a user account asynchronously.""" |
| 140 | + return await self.update_user(user_id, is_active=False) |
| 141 | + |
| 142 | + async def list_users(self) -> list[User]: |
| 143 | + """List all users in the system asynchronously.""" |
| 144 | + statement = select(User).order_by(User.created_at.desc()) |
| 145 | + result = await self.db.exec(statement) |
| 146 | + return list(result.all()) |
| 147 | + |
| 148 | + async def find_existing_emails_with_prefix( |
| 149 | + self, prefix: str, domain: str |
| 150 | + ) -> list[str]: |
| 151 | + """Find existing emails that match the pattern prefix{number}@domain async.""" |
| 152 | + pattern = f"{prefix}%@{domain}" |
| 153 | + statement = select(User.email).where(User.email.like(pattern)) |
| 154 | + result = await self.db.exec(statement) |
| 155 | + return list(result.all()) |
0 commit comments