
Features: - JWT authentication with user registration and login - Customer management with full CRUD operations - Invoice management with automatic calculations - Multi-tenant data isolation by user - SQLite database with Alembic migrations - RESTful API with comprehensive documentation - Tax calculations and invoice status tracking - Code formatted with Ruff linting
49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
from sqlalchemy.orm import Session
|
|
from app.models.user import User
|
|
from app.schemas.user import UserCreate, UserUpdate
|
|
from app.core.security import get_password_hash, verify_password
|
|
from typing import Optional
|
|
|
|
|
|
class UserService:
|
|
def __init__(self, db: Session):
|
|
self.db = db
|
|
|
|
def get_user_by_email(self, email: str) -> Optional[User]:
|
|
return self.db.query(User).filter(User.email == email).first()
|
|
|
|
def get_user_by_id(self, user_id: int) -> Optional[User]:
|
|
return self.db.query(User).filter(User.id == user_id).first()
|
|
|
|
def create_user(self, user_data: UserCreate) -> User:
|
|
hashed_password = get_password_hash(user_data.password)
|
|
user = User(
|
|
email=user_data.email,
|
|
hashed_password=hashed_password,
|
|
full_name=user_data.full_name,
|
|
company_name=user_data.company_name,
|
|
)
|
|
self.db.add(user)
|
|
self.db.commit()
|
|
self.db.refresh(user)
|
|
return user
|
|
|
|
def authenticate_user(self, email: str, password: str) -> Optional[User]:
|
|
user = self.get_user_by_email(email)
|
|
if not user or not verify_password(password, user.hashed_password):
|
|
return None
|
|
return user
|
|
|
|
def update_user(self, user_id: int, user_data: UserUpdate) -> Optional[User]:
|
|
user = self.get_user_by_id(user_id)
|
|
if not user:
|
|
return None
|
|
|
|
update_data = user_data.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(user, field, value)
|
|
|
|
self.db.commit()
|
|
self.db.refresh(user)
|
|
return user
|