File size: 3,980 Bytes
9df97a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""Educations API routes - ÉTAPE 2 COMPLÉTION"""
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import List

from app.core.dependencies import get_db, get_current_user
from app.models.models import Education, Candidate, User
from app.schemas.education import EducationResponse, EducationCreate, EducationUpdate

router = APIRouter(prefix="/api/candidates/{candidate_id}/educations", tags=["educations"])


@router.get("/", response_model=List[EducationResponse])
def list_educations(
    candidate_id: int,
    skip: int = 0,
    limit: int = 100,
    db: Session = Depends(get_db)
):
    """List all educations for a candidate"""
    # Verify candidate exists
    candidate = db.query(Candidate).filter(Candidate.id == candidate_id).first()
    if not candidate:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Candidate not found"
        )
    
    educations = db.query(Education).filter(
        Education.candidate_id == candidate_id
    ).offset(skip).limit(limit).all()
    return educations


@router.get("/{education_id}", response_model=EducationResponse)
def get_education(
    candidate_id: int,
    education_id: int,
    db: Session = Depends(get_db)
):
    """Get a specific education"""
    education = db.query(Education).filter(
        Education.id == education_id,
        Education.candidate_id == candidate_id
    ).first()
    
    if not education:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Education not found"
        )
    return education


@router.post("/", response_model=EducationResponse, status_code=status.HTTP_201_CREATED)
def create_education(
    candidate_id: int,
    education: EducationCreate,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """Create a new education for a candidate"""
    # Verify candidate exists
    candidate = db.query(Candidate).filter(Candidate.id == candidate_id).first()
    if not candidate:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Candidate not found"
        )
    
    db_education = Education(
        candidate_id=candidate_id,
        degree=education.degree,
        institution=education.institution,
        field=education.field,
        year=education.year
    )
    db.add(db_education)
    db.commit()
    db.refresh(db_education)
    return db_education


@router.put("/{education_id}", response_model=EducationResponse)
def update_education(
    candidate_id: int,
    education_id: int,
    education: EducationUpdate,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """Update an education"""
    db_education = db.query(Education).filter(
        Education.id == education_id,
        Education.candidate_id == candidate_id
    ).first()
    
    if not db_education:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Education not found"
        )
    
    # Update only provided fields
    update_data = education.dict(exclude_unset=True)
    for key, value in update_data.items():
        setattr(db_education, key, value)
    
    db.commit()
    db.refresh(db_education)
    return db_education


@router.delete("/{education_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_education(
    candidate_id: int,
    education_id: int,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """Delete an education"""
    db_education = db.query(Education).filter(
        Education.id == education_id,
        Education.candidate_id == candidate_id
    ).first()
    
    if not db_education:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Education not found"
        )
    
    db.delete(db_education)
    db.commit()