115 lines
3.2 KiB
Python
115 lines
3.2 KiB
Python
from typing import Any, List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud, models
|
|
from app.api.deps import get_current_active_user, get_db
|
|
from app.schemas.policy import Policy, PolicyCreate, PolicyUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/", response_model=List[Policy])
|
|
def read_policies(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
customer_id: Optional[int] = None,
|
|
current_user: models.User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve policies.
|
|
"""
|
|
if customer_id:
|
|
policies = crud.policy.get_by_customer(
|
|
db, customer_id=customer_id, skip=skip, limit=limit
|
|
)
|
|
else:
|
|
policies = crud.policy.get_multi(db, skip=skip, limit=limit)
|
|
return policies
|
|
|
|
@router.post("/", response_model=Policy, status_code=status.HTTP_201_CREATED)
|
|
def create_policy(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
policy_in: PolicyCreate,
|
|
current_user: models.User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Create new policy.
|
|
"""
|
|
# Check if customer exists
|
|
customer = crud.customer.get(db, id=policy_in.customer_id)
|
|
if not customer:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Customer not found",
|
|
)
|
|
|
|
# Check if policy number already exists
|
|
policy = crud.policy.get_by_policy_number(db, policy_number=policy_in.policy_number)
|
|
if policy:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="A policy with this policy number already exists",
|
|
)
|
|
|
|
policy = crud.policy.create(db, obj_in=policy_in)
|
|
return policy
|
|
|
|
@router.get("/{policy_id}", response_model=Policy)
|
|
def read_policy(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
policy_id: int,
|
|
current_user: models.User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get policy by ID.
|
|
"""
|
|
policy = crud.policy.get(db, id=policy_id)
|
|
if not policy:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Policy not found",
|
|
)
|
|
return policy
|
|
|
|
@router.put("/{policy_id}", response_model=Policy)
|
|
def update_policy(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
policy_id: int,
|
|
policy_in: PolicyUpdate,
|
|
current_user: models.User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update a policy.
|
|
"""
|
|
policy = crud.policy.get(db, id=policy_id)
|
|
if not policy:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Policy not found",
|
|
)
|
|
policy = crud.policy.update(db, db_obj=policy, obj_in=policy_in)
|
|
return policy
|
|
|
|
@router.delete("/{policy_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_policy(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
policy_id: int,
|
|
current_user: models.User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Delete a policy.
|
|
"""
|
|
policy = crud.policy.get(db, id=policy_id)
|
|
if not policy:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Policy not found",
|
|
)
|
|
crud.policy.remove(db, id=policy_id)
|
|
return None |