Automated Action 16cfcd783e Build Chat Application Backend with FastAPI and SQLite
- Set up project structure with FastAPI and SQLite
- Implement user authentication using JWT
- Create database models for users, conversations, and messages
- Implement API endpoints for user management and chat functionality
- Set up WebSocket for real-time messaging
- Add database migrations with Alembic
- Create health check endpoint
- Update README with comprehensive documentation

generated with BackendIM... (backend.im)
2025-05-12 16:37:35 +00:00

45 lines
1.5 KiB
Python

from typing import Optional
from sqlalchemy.orm import Session
from app.core.security import get_password_hash, verify_password, generate_uuid
from app.db.repositories.base import BaseRepository
from app.models.user import User
from app.schemas.user import UserCreate, UserUpdate
class UserRepository(BaseRepository[User, UserCreate, UserUpdate]):
def get_by_email(self, db: Session, *, email: str) -> Optional[User]:
return db.query(User).filter(User.email == email).first()
def get_by_username(self, db: Session, *, username: str) -> Optional[User]:
return db.query(User).filter(User.username == username).first()
def create(self, db: Session, *, obj_in: UserCreate) -> User:
user_id = generate_uuid()
hashed_password = get_password_hash(obj_in.password)
db_obj = User(
id=user_id,
username=obj_in.username,
email=obj_in.email,
hashed_password=hashed_password,
is_active=obj_in.is_active
)
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
def authenticate(self, db: Session, *, email: str, password: str) -> Optional[User]:
user = self.get_by_email(db, email=email)
if not user:
return None
if not verify_password(password, user.hashed_password):
return None
return user
def is_active(self, user: User) -> bool:
return user.is_active
user_repository = UserRepository(User)