
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
59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
from sqlalchemy.orm import Session
|
|
from app.models.customer import Customer
|
|
from app.schemas.customer import CustomerCreate, CustomerUpdate
|
|
from typing import List, Optional
|
|
|
|
|
|
class CustomerService:
|
|
def __init__(self, db: Session):
|
|
self.db = db
|
|
|
|
def get_customers(
|
|
self, user_id: int, skip: int = 0, limit: int = 100
|
|
) -> List[Customer]:
|
|
return (
|
|
self.db.query(Customer)
|
|
.filter(Customer.user_id == user_id)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
def get_customer(self, customer_id: int, user_id: int) -> Optional[Customer]:
|
|
return (
|
|
self.db.query(Customer)
|
|
.filter(Customer.id == customer_id, Customer.user_id == user_id)
|
|
.first()
|
|
)
|
|
|
|
def create_customer(self, customer_data: CustomerCreate, user_id: int) -> Customer:
|
|
customer = Customer(**customer_data.dict(), user_id=user_id)
|
|
self.db.add(customer)
|
|
self.db.commit()
|
|
self.db.refresh(customer)
|
|
return customer
|
|
|
|
def update_customer(
|
|
self, customer_id: int, customer_data: CustomerUpdate, user_id: int
|
|
) -> Optional[Customer]:
|
|
customer = self.get_customer(customer_id, user_id)
|
|
if not customer:
|
|
return None
|
|
|
|
update_data = customer_data.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(customer, field, value)
|
|
|
|
self.db.commit()
|
|
self.db.refresh(customer)
|
|
return customer
|
|
|
|
def delete_customer(self, customer_id: int, user_id: int) -> bool:
|
|
customer = self.get_customer(customer_id, user_id)
|
|
if not customer:
|
|
return False
|
|
|
|
self.db.delete(customer)
|
|
self.db.commit()
|
|
return True
|