
- 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.4 KiB
Python
113 lines
3.4 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.Department])
|
|
def read_departments(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve departments.
|
|
"""
|
|
departments = crud.department.get_multi(db, skip=skip, limit=limit)
|
|
return departments
|
|
|
|
|
|
@router.post("/", response_model=schemas.Department)
|
|
def create_department(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
department_in: schemas.DepartmentCreate,
|
|
current_user: models.User = Depends(deps.get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Create new department.
|
|
"""
|
|
department = crud.department.get_by_name(db, name=department_in.name)
|
|
if department:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="The department with this name already exists in the system",
|
|
)
|
|
department = crud.department.create(db, obj_in=department_in)
|
|
return department
|
|
|
|
|
|
@router.get("/{id}", response_model=schemas.Department)
|
|
def read_department(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: str,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get department by ID.
|
|
"""
|
|
department = crud.department.get(db, id=id)
|
|
if not department:
|
|
raise HTTPException(status_code=404, detail="Department not found")
|
|
return department
|
|
|
|
|
|
@router.put("/{id}", response_model=schemas.Department)
|
|
def update_department(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: str,
|
|
department_in: schemas.DepartmentUpdate,
|
|
current_user: models.User = Depends(deps.get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Update a department.
|
|
"""
|
|
department = crud.department.get(db, id=id)
|
|
if not department:
|
|
raise HTTPException(status_code=404, detail="Department not found")
|
|
|
|
# Check if name is changed and if the new name already exists
|
|
if department_in.name and department_in.name != department.name:
|
|
existing_department = crud.department.get_by_name(db, name=department_in.name)
|
|
if existing_department:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="The department with this name already exists in the system",
|
|
)
|
|
|
|
department = crud.department.update(db, db_obj=department, obj_in=department_in)
|
|
return department
|
|
|
|
|
|
@router.delete("/{id}", status_code=204, response_model=None)
|
|
def delete_department(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: str,
|
|
current_user: models.User = Depends(deps.get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Delete a department.
|
|
"""
|
|
department = crud.department.get(db, id=id)
|
|
if not department:
|
|
raise HTTPException(status_code=404, detail="Department not found")
|
|
|
|
# Check if there are employees in this department
|
|
employees = db.query(models.Employee).filter(models.Employee.department_id == id).first()
|
|
if employees:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="Cannot delete department with employees. Reassign employees first.",
|
|
)
|
|
|
|
crud.department.remove(db, id=id)
|
|
return None |