
- Set up project structure with FastAPI framework - Create database models for users, employees, departments, and job titles - Implement JWT authentication and authorization system - Set up SQLite database with SQLAlchemy ORM - Add Alembic migrations for database versioning - Create CRUD API endpoints for employee management - Implement category-based search functionality - Add OpenAPI documentation and health check endpoint - Update README with comprehensive setup and usage instructions
113 lines
3.3 KiB
Python
113 lines
3.3 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud, models, schemas
|
|
from app.api import deps
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[schemas.JobTitle])
|
|
def read_job_titles(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve job titles.
|
|
"""
|
|
job_titles = crud.job_title.get_multi(db, skip=skip, limit=limit)
|
|
return job_titles
|
|
|
|
|
|
@router.post("/", response_model=schemas.JobTitle)
|
|
def create_job_title(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
job_title_in: schemas.JobTitleCreate,
|
|
current_user: models.User = Depends(deps.get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Create new job title.
|
|
"""
|
|
job_title = crud.job_title.get_by_title(db, title=job_title_in.title)
|
|
if job_title:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="The job title with this name already exists in the system",
|
|
)
|
|
job_title = crud.job_title.create(db, obj_in=job_title_in)
|
|
return job_title
|
|
|
|
|
|
@router.get("/{id}", response_model=schemas.JobTitle)
|
|
def read_job_title(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: str,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get job title by ID.
|
|
"""
|
|
job_title = crud.job_title.get(db, id=id)
|
|
if not job_title:
|
|
raise HTTPException(status_code=404, detail="Job title not found")
|
|
return job_title
|
|
|
|
|
|
@router.put("/{id}", response_model=schemas.JobTitle)
|
|
def update_job_title(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: str,
|
|
job_title_in: schemas.JobTitleUpdate,
|
|
current_user: models.User = Depends(deps.get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Update a job title.
|
|
"""
|
|
job_title = crud.job_title.get(db, id=id)
|
|
if not job_title:
|
|
raise HTTPException(status_code=404, detail="Job title not found")
|
|
|
|
# Check if title is changed and if the new title already exists
|
|
if job_title_in.title and job_title_in.title != job_title.title:
|
|
existing_job_title = crud.job_title.get_by_title(db, title=job_title_in.title)
|
|
if existing_job_title:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="The job title with this name already exists in the system",
|
|
)
|
|
|
|
job_title = crud.job_title.update(db, db_obj=job_title, obj_in=job_title_in)
|
|
return job_title
|
|
|
|
|
|
@router.delete("/{id}", status_code=204, response_model=None)
|
|
def delete_job_title(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: str,
|
|
current_user: models.User = Depends(deps.get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Delete a job title.
|
|
"""
|
|
job_title = crud.job_title.get(db, id=id)
|
|
if not job_title:
|
|
raise HTTPException(status_code=404, detail="Job title not found")
|
|
|
|
# Check if there are employees with this job title
|
|
employees = db.query(models.Employee).filter(models.Employee.job_title_id == id).first()
|
|
if employees:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="Cannot delete job title assigned to employees. Reassign employees first.",
|
|
)
|
|
|
|
crud.job_title.remove(db, id=id)
|
|
return None |