114 lines
3.0 KiB
Python
114 lines
3.0 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.auth import get_current_active_user
|
|
from app.crud import customer as crud_customer
|
|
from app.db.session import get_db
|
|
from app.models.user import User
|
|
from app.schemas.customer import Customer, CustomerCreate, CustomerUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=List[Customer])
|
|
def read_customers(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(get_current_active_user)
|
|
) -> Any:
|
|
"""
|
|
Retrieve customers for the current user.
|
|
"""
|
|
customers = crud_customer.get_customers(
|
|
db, user_id=current_user.id, skip=skip, limit=limit
|
|
)
|
|
return customers
|
|
|
|
|
|
@router.post("", response_model=Customer, status_code=status.HTTP_201_CREATED)
|
|
def create_customer(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
customer_in: CustomerCreate,
|
|
current_user: User = Depends(get_current_active_user)
|
|
) -> Any:
|
|
"""
|
|
Create new customer for the current user.
|
|
"""
|
|
customer = crud_customer.create_customer(
|
|
db, user_id=current_user.id, customer_in=customer_in
|
|
)
|
|
return customer
|
|
|
|
|
|
@router.get("/{customer_id}", response_model=Customer)
|
|
def read_customer(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
customer_id: int,
|
|
current_user: User = Depends(get_current_active_user)
|
|
) -> Any:
|
|
"""
|
|
Get customer by ID.
|
|
"""
|
|
customer = crud_customer.get_customer(
|
|
db, user_id=current_user.id, customer_id=customer_id
|
|
)
|
|
if not customer:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Customer not found",
|
|
)
|
|
return customer
|
|
|
|
|
|
@router.patch("/{customer_id}", response_model=Customer)
|
|
def update_customer(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
customer_id: int,
|
|
customer_in: CustomerUpdate,
|
|
current_user: User = Depends(get_current_active_user)
|
|
) -> Any:
|
|
"""
|
|
Update a customer.
|
|
"""
|
|
customer = crud_customer.get_customer(
|
|
db, user_id=current_user.id, customer_id=customer_id
|
|
)
|
|
if not customer:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Customer not found",
|
|
)
|
|
customer = crud_customer.update_customer(
|
|
db, user_id=current_user.id, db_customer=customer, customer_in=customer_in
|
|
)
|
|
return customer
|
|
|
|
|
|
@router.delete("/{customer_id}", response_model=Customer)
|
|
def delete_customer(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
customer_id: int,
|
|
current_user: User = Depends(get_current_active_user)
|
|
) -> Any:
|
|
"""
|
|
Delete a customer.
|
|
"""
|
|
customer = crud_customer.get_customer(
|
|
db, user_id=current_user.id, customer_id=customer_id
|
|
)
|
|
if not customer:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Customer not found",
|
|
)
|
|
customer = crud_customer.delete_customer(
|
|
db, user_id=current_user.id, customer_id=customer_id
|
|
)
|
|
return customer |