QuentinL52 commited on
Commit
fe417f6
·
verified ·
1 Parent(s): 3a76ebb

Update src/services/cv_router.py

Browse files
Files changed (1) hide show
  1. src/services/cv_router.py +9 -7
src/services/cv_router.py CHANGED
@@ -23,16 +23,17 @@ class CVResponse(BaseModel):
23
 
24
  class Config:
25
  populate_by_name = True
26
- json_encoders = {ObjectId: str} # MODIFICATION : Ajout de l'encodeur JSON
27
  from_attributes = True
28
 
29
  @router.post("/cvs", response_model=CVResponse)
30
  async def create_cv(cv: CVCreate, db: AsyncIOMotorDatabase = Depends(lambda: mongo_db)):
31
- cv_entry = CVModel(**cv.model_dump(by_alias=True))
32
- cv_id = await CVModel.create(db, CVModel.collection_name, cv_entry.model_dump(exclude_unset=True))
33
- # Note: On retourne un objet pydantic pour que l'encodage se fasse correctement
34
- return CVResponse(**cv_entry.model_dump(), id=cv_id)
35
-
 
36
 
37
  @router.get("/cvs/{cv_id}", response_model=CVResponse)
38
  async def get_cv_by_id(cv_id: str, db: AsyncIOMotorDatabase = Depends(lambda: mongo_db)):
@@ -43,5 +44,6 @@ async def get_cv_by_id(cv_id: str, db: AsyncIOMotorDatabase = Depends(lambda: mo
43
  if cv is None:
44
  raise HTTPException(status_code=404, detail="CV not found")
45
 
46
- # MODIFICATION : Retourne directement l'objet
 
47
  return CVResponse(**cv)
 
23
 
24
  class Config:
25
  populate_by_name = True
26
+ json_encoders = {ObjectId: str}
27
  from_attributes = True
28
 
29
  @router.post("/cvs", response_model=CVResponse)
30
  async def create_cv(cv: CVCreate, db: AsyncIOMotorDatabase = Depends(lambda: mongo_db)):
31
+ cv_entry = cv.model_dump(by_alias=True, exclude_unset=True)
32
+ cv_id = await CVModel.create(db, CVModel.collection_name, cv_entry)
33
+
34
+ # MODIFICATION : Retourne directement l'objet Pydantic pour sérialisation
35
+ cv_entry['id'] = cv_id
36
+ return CVResponse(**cv_entry)
37
 
38
  @router.get("/cvs/{cv_id}", response_model=CVResponse)
39
  async def get_cv_by_id(cv_id: str, db: AsyncIOMotorDatabase = Depends(lambda: mongo_db)):
 
44
  if cv is None:
45
  raise HTTPException(status_code=404, detail="CV not found")
46
 
47
+ # MODIFICATION : Convertit explicitement l'ObjectId en str avant de retourner l'objet
48
+ cv['id'] = str(cv['_id'])
49
  return CVResponse(**cv)