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.claim import Claim, ClaimCreate, ClaimUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/", response_model=List[Claim])
|
|
def read_claims(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
policy_id: Optional[int] = None,
|
|
current_user: models.User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve claims.
|
|
"""
|
|
if policy_id:
|
|
claims = crud.claim.get_by_policy(
|
|
db, policy_id=policy_id, skip=skip, limit=limit
|
|
)
|
|
else:
|
|
claims = crud.claim.get_multi(db, skip=skip, limit=limit)
|
|
return claims
|
|
|
|
@router.post("/", response_model=Claim, status_code=status.HTTP_201_CREATED)
|
|
def create_claim(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
claim_in: ClaimCreate,
|
|
current_user: models.User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Create new claim.
|
|
"""
|
|
# Check if policy exists
|
|
policy = crud.policy.get(db, id=claim_in.policy_id)
|
|
if not policy:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Policy not found",
|
|
)
|
|
|
|
# Check if claim number already exists
|
|
claim = crud.claim.get_by_claim_number(db, claim_number=claim_in.claim_number)
|
|
if claim:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="A claim with this claim number already exists",
|
|
)
|
|
|
|
claim = crud.claim.create(db, obj_in=claim_in)
|
|
return claim
|
|
|
|
@router.get("/{claim_id}", response_model=Claim)
|
|
def read_claim(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
claim_id: int,
|
|
current_user: models.User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get claim by ID.
|
|
"""
|
|
claim = crud.claim.get(db, id=claim_id)
|
|
if not claim:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Claim not found",
|
|
)
|
|
return claim
|
|
|
|
@router.put("/{claim_id}", response_model=Claim)
|
|
def update_claim(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
claim_id: int,
|
|
claim_in: ClaimUpdate,
|
|
current_user: models.User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update a claim.
|
|
"""
|
|
claim = crud.claim.get(db, id=claim_id)
|
|
if not claim:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Claim not found",
|
|
)
|
|
claim = crud.claim.update(db, db_obj=claim, obj_in=claim_in)
|
|
return claim
|
|
|
|
@router.delete("/{claim_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_claim(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
claim_id: int,
|
|
current_user: models.User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Delete a claim.
|
|
"""
|
|
claim = crud.claim.get(db, id=claim_id)
|
|
if not claim:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Claim not found",
|
|
)
|
|
crud.claim.remove(db, id=claim_id)
|
|
return None |