
- Set up FastAPI application with SQLite database - Implement User and Item models with relationships - Add CRUD operations for users and items - Configure Alembic for database migrations - Include API documentation at /docs and /redoc - Add health check endpoint at /health - Enable CORS for all origins - Structure code with proper separation of concerns
38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
from typing import Optional
|
|
from sqlalchemy.orm import Session
|
|
from passlib.context import CryptContext
|
|
from app.crud.base import CRUDBase
|
|
from app.models.user import User
|
|
from app.schemas.user import UserCreate, UserUpdate
|
|
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
|
class CRUDUser(CRUDBase[User, UserCreate, UserUpdate]):
|
|
def get_by_email(self, db: Session, *, email: str) -> Optional[User]:
|
|
return db.query(User).filter(User.email == email).first()
|
|
|
|
def create(self, db: Session, *, obj_in: UserCreate) -> User:
|
|
hashed_password = pwd_context.hash(obj_in.password)
|
|
db_obj = User(
|
|
email=obj_in.email,
|
|
hashed_password=hashed_password,
|
|
full_name=obj_in.full_name,
|
|
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 pwd_context.verify(password, user.hashed_password):
|
|
return None
|
|
return user
|
|
|
|
def is_active(self, user: User) -> bool:
|
|
return user.is_active
|
|
|
|
user = CRUDUser(User) |