|
| 1 | +from app.schemas.base import ResponseBase |
| 2 | +from app.schemas.comprehensive_evaluation import ( |
| 3 | + ComprehensiveEvaluationResponse, |
| 4 | + ComprehensiveEvaluationCreate, |
| 5 | + ComprehensiveEvaluationUpdate, |
| 6 | +) |
| 7 | +from app.services.comprehensive_evaluation import ( |
| 8 | + select_comprehensive_evaluation, |
| 9 | + select_comprehensive_evaluations, |
| 10 | + add_comprehensive_evaluation, |
| 11 | + update_comprehensive_evaluation_service, |
| 12 | + delete_comprehensive_evaluation_service, |
| 13 | +) |
| 14 | +from fastapi import APIRouter, Depends |
| 15 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 16 | +from app.db.session import get_db |
| 17 | +from uuid import UUID |
| 18 | +from typing import List |
| 19 | +from fastapi import HTTPException |
| 20 | +from fastapi import status |
| 21 | + |
| 22 | +router = APIRouter() |
| 23 | + |
| 24 | + |
| 25 | +@router.get( |
| 26 | + "/", |
| 27 | + response_model=ResponseBase[List[ComprehensiveEvaluationResponse]], |
| 28 | +) |
| 29 | +async def get_comprehensive_evaluations( |
| 30 | + user_id: UUID, |
| 31 | + db: AsyncSession = Depends(get_db), |
| 32 | +) -> ResponseBase[List[ComprehensiveEvaluationResponse]]: |
| 33 | + |
| 34 | + try: |
| 35 | + comprehensive_evaluations = await select_comprehensive_evaluations(db, user_id) |
| 36 | + return ResponseBase( |
| 37 | + status_code=status.HTTP_200_OK, data=comprehensive_evaluations |
| 38 | + ) |
| 39 | + except HTTPException as e: |
| 40 | + return ResponseBase(status_code=e.status_code, error=e.detail) |
| 41 | + except Exception as e: |
| 42 | + return ResponseBase( |
| 43 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, error=str(e) |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +@router.get( |
| 48 | + "/{comprehensive_evaluation_id}", |
| 49 | + response_model=ResponseBase[ComprehensiveEvaluationResponse], |
| 50 | +) |
| 51 | +async def get_comprehensive_evaluation( |
| 52 | + user_id: UUID, |
| 53 | + comprehensive_evaluation_id: UUID, |
| 54 | + db: AsyncSession = Depends(get_db), |
| 55 | +) -> ResponseBase[ComprehensiveEvaluationResponse]: |
| 56 | + try: |
| 57 | + comprehensive_evaluation = await select_comprehensive_evaluation( |
| 58 | + db, user_id, comprehensive_evaluation_id |
| 59 | + ) |
| 60 | + return ResponseBase( |
| 61 | + status_code=status.HTTP_200_OK, data=comprehensive_evaluation |
| 62 | + ) |
| 63 | + except HTTPException as e: |
| 64 | + return ResponseBase(status_code=e.status_code, error=e.detail) |
| 65 | + except Exception as e: |
| 66 | + return ResponseBase( |
| 67 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, error=str(e) |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +@router.post( |
| 72 | + "/", |
| 73 | + response_model=ResponseBase[ComprehensiveEvaluationResponse], |
| 74 | +) |
| 75 | +async def post_comprehensive_evaluation( |
| 76 | + comprehensive_evaluation: ComprehensiveEvaluationCreate, |
| 77 | + user_id: UUID, |
| 78 | + db: AsyncSession = Depends(get_db), |
| 79 | +) -> ResponseBase[ComprehensiveEvaluationResponse]: |
| 80 | + try: |
| 81 | + comprehensive_evaluation = await add_comprehensive_evaluation( |
| 82 | + db, user_id, comprehensive_evaluation |
| 83 | + ) |
| 84 | + return ResponseBase( |
| 85 | + status_code=status.HTTP_201_CREATED, data=comprehensive_evaluation |
| 86 | + ) |
| 87 | + except HTTPException as e: |
| 88 | + return ResponseBase(status_code=e.status_code, error=e.detail) |
| 89 | + except Exception as e: |
| 90 | + return ResponseBase( |
| 91 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, error=str(e) |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +@router.put( |
| 96 | + "/{comprehensive_evaluation_id}", |
| 97 | + response_model=ResponseBase[ComprehensiveEvaluationResponse], |
| 98 | +) |
| 99 | +async def put_comprehensive_evaluation( |
| 100 | + user_id: UUID, |
| 101 | + comprehensive_evaluation_id: UUID, |
| 102 | + comprehensive_evaluation: ComprehensiveEvaluationUpdate, |
| 103 | + db: AsyncSession = Depends(get_db), |
| 104 | +) -> ResponseBase[ComprehensiveEvaluationResponse]: |
| 105 | + try: |
| 106 | + comprehensive_evaluation = await update_comprehensive_evaluation_service( |
| 107 | + db, user_id, comprehensive_evaluation_id, comprehensive_evaluation |
| 108 | + ) |
| 109 | + return ResponseBase( |
| 110 | + status_code=status.HTTP_200_OK, data=comprehensive_evaluation |
| 111 | + ) |
| 112 | + except HTTPException as e: |
| 113 | + return ResponseBase(status_code=e.status_code, error=e.detail) |
| 114 | + except Exception as e: |
| 115 | + return ResponseBase( |
| 116 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, error=str(e) |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +@router.delete("/{comprehensive_evaluation_id}", response_model=ResponseBase[bool]) |
| 121 | +async def delete_comprehensive_evaluation( |
| 122 | + user_id: UUID, |
| 123 | + comprehensive_evaluation_id: UUID, |
| 124 | + db: AsyncSession = Depends(get_db), |
| 125 | +) -> ResponseBase[bool]: |
| 126 | + try: |
| 127 | + result = await delete_comprehensive_evaluation_service( |
| 128 | + db, user_id, comprehensive_evaluation_id |
| 129 | + ) |
| 130 | + return ResponseBase(status_code=status.HTTP_200_OK, data=result) |
| 131 | + except HTTPException as e: |
| 132 | + return ResponseBase(status_code=e.status_code, error=e.detail) |
| 133 | + except Exception as e: |
| 134 | + return ResponseBase( |
| 135 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, error=str(e) |
| 136 | + ) |
0 commit comments