
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
84 lines
2.5 KiB
Python
84 lines
2.5 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
from app.db.session import get_db
|
|
from app.services.customer_service import CustomerService
|
|
from app.schemas.customer import Customer, CustomerCreate, CustomerUpdate
|
|
from app.core.deps import get_current_user
|
|
from app.models.user import User
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Customer])
|
|
def get_customers(
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(100, ge=1, le=100),
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
customer_service = CustomerService(db)
|
|
return customer_service.get_customers(current_user.id, skip, limit)
|
|
|
|
|
|
@router.post("/", response_model=Customer, status_code=status.HTTP_201_CREATED)
|
|
def create_customer(
|
|
customer_data: CustomerCreate,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
customer_service = CustomerService(db)
|
|
return customer_service.create_customer(customer_data, current_user.id)
|
|
|
|
|
|
@router.get("/{customer_id}", response_model=Customer)
|
|
def get_customer(
|
|
customer_id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
customer_service = CustomerService(db)
|
|
customer = customer_service.get_customer(customer_id, current_user.id)
|
|
|
|
if not customer:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Customer not found"
|
|
)
|
|
|
|
return customer
|
|
|
|
|
|
@router.put("/{customer_id}", response_model=Customer)
|
|
def update_customer(
|
|
customer_id: int,
|
|
customer_data: CustomerUpdate,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
customer_service = CustomerService(db)
|
|
customer = customer_service.update_customer(
|
|
customer_id, customer_data, current_user.id
|
|
)
|
|
|
|
if not customer:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Customer not found"
|
|
)
|
|
|
|
return customer
|
|
|
|
|
|
@router.delete("/{customer_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_customer(
|
|
customer_id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
customer_service = CustomerService(db)
|
|
success = customer_service.delete_customer(customer_id, current_user.id)
|
|
|
|
if not success:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Customer not found"
|
|
)
|