from fastapi import Depends, APIRouter, File, Form, HTTPException, UploadFile, 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 typing import List from schemas.maintenancejob import CreateMaintenanceJob, OutputMaintenanceJob from schemas.carpart import CreateCarPart, ShowCarPart 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", response_model=ShowCarPart, status_code=status.HTTP_201_CREATED ) def create_carpart( car_part: CreateCarPart = Depends(), 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" ) print("So it begins...") car_part_res = create_car_part(car_part=car_part, db=db) print("So it ends...") return car_part_res @router.get( "/", response_model=List[OutputMaintenanceJob], 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}", response_model=OutputMaintenanceJob, 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) if maintenancejob is None: raise HTTPException( status_code=404, detail="Maintenance job with this id does not exist" ) print(maintenancejob) 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