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