|
| 1 | +from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status |
| 2 | + |
| 3 | +from app.database.connection import get_db_connection |
| 4 | +from app.schemas.models import JobOfferCreate, JobOfferSummary, JobOfferUpdate, MessageResponse |
| 5 | +from app.utils.job_offers import ( |
| 6 | + add_job_offer, |
| 7 | + delete_job_offer, |
| 8 | + get_active_job_offers, |
| 9 | + get_all_job_offers, |
| 10 | + get_job_offer_by_id, |
| 11 | + update_job_offer, |
| 12 | +) |
| 13 | +from app.core.settings import logger |
| 14 | + |
| 15 | + |
| 16 | +api_router = APIRouter(prefix="/job-offers", tags=["job-offers"]) |
| 17 | + |
| 18 | + |
| 19 | +@api_router.post("/create", response_model=MessageResponse, status_code=status.HTTP_201_CREATED) |
| 20 | +async def create_job_offer( |
| 21 | + job_offer: JobOfferCreate, |
| 22 | + background_tasks: BackgroundTasks, |
| 23 | + db=Depends(get_db_connection), |
| 24 | +): |
| 25 | + """Create a new job offer.""" |
| 26 | + try: |
| 27 | + return await add_job_offer(db, job_offer, background_tasks) |
| 28 | + except Exception as e: |
| 29 | + if isinstance(e, HTTPException): |
| 30 | + raise e |
| 31 | + raise HTTPException(status_code=500, detail="Internal server error") |
| 32 | + |
| 33 | + |
| 34 | +@api_router.get("/list/active", response_model=list[JobOfferSummary]) |
| 35 | +async def list_active_job_offers(db=Depends(get_db_connection)): |
| 36 | + """List all active job offers.""" |
| 37 | + try: |
| 38 | + job_offers = await get_active_job_offers(db) |
| 39 | + if not job_offers: |
| 40 | + raise HTTPException( |
| 41 | + status_code=status.HTTP_404_NOT_FOUND, |
| 42 | + detail="No active job offers found", |
| 43 | + ) |
| 44 | + return job_offers |
| 45 | + except Exception as e: |
| 46 | + logger.error(f"Error listing active job offers: {str(e)}") |
| 47 | + if isinstance(e, HTTPException): |
| 48 | + raise e |
| 49 | + raise HTTPException(status_code=500, detail="Internal server error") |
| 50 | + |
| 51 | + |
| 52 | +@api_router.get("/list", response_model=list[JobOfferSummary]) |
| 53 | +async def list_all_job_offers(db=Depends(get_db_connection)): |
| 54 | + """List all job offers (admin).""" |
| 55 | + try: |
| 56 | + return await get_all_job_offers(db) |
| 57 | + except Exception as e: |
| 58 | + logger.error(f"Error listing all job offers: {str(e)}") |
| 59 | + if isinstance(e, HTTPException): |
| 60 | + raise e |
| 61 | + raise HTTPException(status_code=500, detail="Internal server error") |
| 62 | + |
| 63 | + |
| 64 | +@api_router.get("/{job_offer_id}", response_model=JobOfferSummary) |
| 65 | +async def get_job_offer(job_offer_id: str, db=Depends(get_db_connection)): |
| 66 | + """Retrieve a job offer by its ID.""" |
| 67 | + try: |
| 68 | + return await get_job_offer_by_id(db, job_offer_id) |
| 69 | + except Exception as e: |
| 70 | + if isinstance(e, HTTPException): |
| 71 | + raise e |
| 72 | + raise HTTPException(status_code=500, detail="Internal server error") |
| 73 | + |
| 74 | + |
| 75 | +@api_router.put("/update/{job_offer_id}", response_model=MessageResponse) |
| 76 | +async def update_job_offer_details( |
| 77 | + job_offer_id: str, |
| 78 | + job_offer_update: JobOfferUpdate, |
| 79 | + background_tasks: BackgroundTasks, |
| 80 | + db=Depends(get_db_connection), |
| 81 | +): |
| 82 | + """Update an existing job offer.""" |
| 83 | + try: |
| 84 | + return await update_job_offer(db, job_offer_id, job_offer_update, background_tasks) |
| 85 | + except Exception as e: |
| 86 | + if isinstance(e, HTTPException): |
| 87 | + raise e |
| 88 | + raise HTTPException(status_code=500, detail="Internal server error") |
| 89 | + |
| 90 | + |
| 91 | +@api_router.delete("/delete/{job_offer_id}", response_model=MessageResponse) |
| 92 | +async def delete_job_offer_by_id( |
| 93 | + job_offer_id: str, |
| 94 | + background_tasks: BackgroundTasks, |
| 95 | + db=Depends(get_db_connection), |
| 96 | +): |
| 97 | + """Delete a job offer by its ID.""" |
| 98 | + try: |
| 99 | + return await delete_job_offer(db, job_offer_id, background_tasks) |
| 100 | + except Exception as e: |
| 101 | + if isinstance(e, HTTPException): |
| 102 | + raise e |
| 103 | + raise HTTPException(status_code=500, detail="Internal server error") |
0 commit comments