from fastapi import Depends, APIRouter, HTTPException, status from sqlalchemy.orm import Session from db.session import get_db from db.repository.maintenancejob import ( create_new_maintenancejob, create_car_part, get_all_maintenance_jobs, get_maintenance_job, change_maintenance_status ) from schemas.maintenancejob import CreateMaintenanceJob from schemas.carpart import CreateCarPart from db.models.user import User from apis.v1.route_auth import get_current_user router = APIRouter() @router.post("/", status_code=status.HTTP_201_CREATED) def create_maintenancejob( maintenancejob: CreateMaintenanceJob, db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ): if current_user.Role != "Maintenance" and current_user.Role != "Admin": raise HTTPException( status_code=403, detail="You are not authorized to perform this action" ) maintenancejob_res = create_new_maintenancejob( maintenancejob=maintenancejob, maintenanceworker=current_user.Id, db=db ) return maintenancejob_res @router.post("/carpart", status_code=status.HTTP_201_CREATED) def create_carpart( car_part: CreateCarPart, db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ): if current_user.Role != "Maintenance" and current_user.Role != "Admin": raise HTTPException( status_code=403, detail="You are not authorized to perform this action" ) car_part_res = create_car_part(car_part=car_part, db=db) return car_part_res @router.get("/", status_code=status.HTTP_200_OK) def get_all_maintenancejobs( db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ): if current_user.Role != "Maintenance" and current_user.Role != "Admin": raise HTTPException( status_code=403, detail="You are not authorized to perform this action" ) maintenancejobs = get_all_maintenance_jobs(db) return maintenancejobs @router.get("/{maintenance_job_id}", status_code=status.HTTP_200_OK) def get_maintenancejob( maintenance_job_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ): if current_user.Role != "Maintenance" and current_user.Role != "Admin": raise HTTPException( status_code=403, detail="You are not authorized to perform this action" ) maintenancejob = get_maintenance_job(maintenance_job_id, db) return maintenancejob @router.patch("/{maintenance_job_id}", status_code=status.HTTP_200_OK) def change_status( maintenance_job_id: int, status: str, db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ): if current_user.Role != "Maintenance" and current_user.Role != "Admin": raise HTTPException( status_code=403, detail="You are not authorized to perform this action" ) result = change_maintenance_status(maintenance_job_id, status, db) if result is None: raise HTTPException( status_code=404, detail="Maintenance job not found" ) return result