
- Set up project structure with FastAPI - Implement user and account management - Add send and receive money functionality - Set up transaction processing system - Add JWT authentication - Configure SQLAlchemy with SQLite - Set up Alembic for database migrations - Create comprehensive API documentation
85 lines
2.0 KiB
Python
85 lines
2.0 KiB
Python
from typing import Optional, List
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.security import get_password_hash, verify_password
|
|
from app.models.user import User
|
|
from app.schemas.user import UserCreate, UserUpdate
|
|
|
|
|
|
def get_user_by_id(db: Session, id: int) -> Optional[User]:
|
|
"""
|
|
Get a user by ID
|
|
"""
|
|
return db.query(User).filter(User.id == id).first()
|
|
|
|
|
|
def get_user_by_email(db: Session, email: str) -> Optional[User]:
|
|
"""
|
|
Get a user by email
|
|
"""
|
|
return db.query(User).filter(User.email == email).first()
|
|
|
|
|
|
def get_users(db: Session, skip: int = 0, limit: int = 100) -> List[User]:
|
|
"""
|
|
Get a list of users
|
|
"""
|
|
return db.query(User).offset(skip).limit(limit).all()
|
|
|
|
|
|
def create_user(db: Session, user_in: UserCreate) -> User:
|
|
"""
|
|
Create a new user
|
|
"""
|
|
db_user = User(
|
|
email=user_in.email,
|
|
first_name=user_in.first_name,
|
|
last_name=user_in.last_name,
|
|
hashed_password=get_password_hash(user_in.password),
|
|
is_active=user_in.is_active,
|
|
)
|
|
db.add(db_user)
|
|
db.commit()
|
|
db.refresh(db_user)
|
|
return db_user
|
|
|
|
|
|
def update_user(db: Session, user: User, user_in: UserUpdate) -> User:
|
|
"""
|
|
Update a user
|
|
"""
|
|
update_data = user_in.dict(exclude_unset=True)
|
|
if update_data.get("password"):
|
|
hashed_password = get_password_hash(update_data["password"])
|
|
del update_data["password"]
|
|
update_data["hashed_password"] = hashed_password
|
|
|
|
for field, value in update_data.items():
|
|
setattr(user, field, value)
|
|
|
|
db.add(user)
|
|
db.commit()
|
|
db.refresh(user)
|
|
return user
|
|
|
|
|
|
def delete_user(db: Session, user: User) -> User:
|
|
"""
|
|
Delete a user
|
|
"""
|
|
db.delete(user)
|
|
db.commit()
|
|
return user
|
|
|
|
|
|
def authenticate(db: Session, email: str, password: str) -> Optional[User]:
|
|
"""
|
|
Authenticate a user
|
|
"""
|
|
user = get_user_by_email(db, email=email)
|
|
if not user:
|
|
return None
|
|
if not verify_password(password, user.hashed_password):
|
|
return None
|
|
return user |