import uuid from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select from ..database import get_db from ..models.jd import JobDescription from ..schemas.jd import JDCreate, JDResponse, JDListItem from ..workers.ingest import ingest_jd router = APIRouter() @router.post("", response_model=JDResponse, status_code=201) async def create_jd(payload: JDCreate, db: AsyncSession = Depends(get_db)): jd = JobDescription( id=uuid.uuid4(), title=payload.title, raw_text=payload.raw_text, session_id=payload.session_id, status="processing", ) db.add(jd) await db.commit() await db.refresh(jd) ingest_jd.delay(str(jd.id), payload.raw_text, payload.title) return jd @router.get("", response_model=list[JDListItem]) async def list_jds(session_id: uuid.UUID | None = None, db: AsyncSession = Depends(get_db)): stmt = select(JobDescription).order_by(JobDescription.created_at.desc()) if session_id: stmt = stmt.where(JobDescription.session_id == session_id) result = await db.execute(stmt.limit(50)) return result.scalars().all() @router.get("/{jd_id}", response_model=JDResponse) async def get_jd(jd_id: uuid.UUID, db: AsyncSession = Depends(get_db)): result = await db.execute(select(JobDescription).where(JobDescription.id == jd_id)) jd = result.scalar_one_or_none() if not jd: raise HTTPException(status_code=404, detail="JD not found") return jd from pydantic import BaseModel class JDWeightsUpdate(BaseModel): weights: dict[str, float] @router.patch("/{jd_id}/weights", response_model=JDResponse) async def update_jd_weights(jd_id: uuid.UUID, payload: JDWeightsUpdate, db: AsyncSession = Depends(get_db)): result = await db.execute(select(JobDescription).where(JobDescription.id == jd_id)) jd = result.scalar_one_or_none() if not jd: raise HTTPException(status_code=404, detail="JD not found") jd.custom_weights = payload.weights await db.commit() await db.refresh(jd) return jd