@@ -7,6 +7,7 @@ from apis.v1 import ( | |||||
route_auth, | route_auth, | ||||
route_task, | route_task, | ||||
route_maintenancejob, | route_maintenancejob, | ||||
route_fuelingtask | |||||
) | ) | ||||
api_router = APIRouter() | api_router = APIRouter() | ||||
@@ -17,3 +18,4 @@ api_router.include_router(route_task.router, prefix="/task", tags=["task"]) | |||||
api_router.include_router( | api_router.include_router( | ||||
route_maintenancejob.router, prefix="/maintenancejob", tags=["maintenancejob"] | route_maintenancejob.router, prefix="/maintenancejob", tags=["maintenancejob"] | ||||
) | ) | ||||
api_router.include_router(route_fuelingtask.router, prefix="/fuel", tags=["fueltasks"]) |
@@ -0,0 +1,71 @@ | |||||
from fastapi import Depends, APIRouter, HTTPException, status | |||||
from sqlalchemy.orm import Session | |||||
from db.session import get_db | |||||
from db.repository.fuelingtask import ( | |||||
create_fueling_task, | |||||
delete_fueling_task, | |||||
get_fueling_task_by_id | |||||
) | |||||
from schemas.fuelingtask import CreateFuelingTask | |||||
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_fuelingtask( | |||||
fuelingtask: CreateFuelingTask, | |||||
db: Session = Depends(get_db), | |||||
current_user: User = Depends(get_current_user), | |||||
): | |||||
if current_user.Role != "FuelingPerson" and current_user.Role != "Admin": | |||||
raise HTTPException( | |||||
status_code=403, detail="You are not authorized to perform this action" | |||||
) | |||||
fuelingtask_res = create_fueling_task( | |||||
fueling_task=fuelingtask, current_user=current_user.Id, db=db | |||||
) | |||||
if fuelingtask_res == "nodriver": | |||||
raise HTTPException( | |||||
status_code=404, detail="Driver ID not found" | |||||
) | |||||
if fuelingtask_res == "novehicle": | |||||
raise HTTPException( | |||||
status_code=404, detail="Vehicle ID not found" | |||||
) | |||||
return fuelingtask_res | |||||
@router.get("/{fueling_task_id}", status_code=status.HTTP_200_OK) | |||||
def get_fuelingtask( | |||||
fueling_task_id: int, | |||||
db: Session = Depends(get_db), | |||||
current_user: User = Depends(get_current_user), | |||||
): | |||||
if current_user.Role != "FuelingPerson" and current_user.Role != "Admin": | |||||
raise HTTPException( | |||||
status_code=403, detail="You are not authorized to perform this action" | |||||
) | |||||
fuelingtask = get_fueling_task_by_id(fueling_task_id, db) | |||||
return fuelingtask | |||||
@router.delete("/{fueling_task_id}", status_code=status.HTTP_204_NO_CONTENT) | |||||
def delete_fuelingtask( | |||||
fueling_task_id: int, | |||||
db: Session = Depends(get_db), | |||||
current_user: User = Depends(get_current_user), | |||||
): | |||||
if current_user.Role != "FuelingPerson" and current_user.Role != "Admin": | |||||
raise HTTPException( | |||||
status_code=403, detail="You are not authorized to perform this action" | |||||
) | |||||
if not delete_fueling_task(fueling_task_id, db): | |||||
raise HTTPException( | |||||
status_code=404, detail="Fueling task not found" | |||||
) |
@@ -6,3 +6,4 @@ from db.models.carpart import CarPart | |||||
from db.models.maintenancejob import MaintenanceJob | from db.models.maintenancejob import MaintenanceJob | ||||
from db.models.drivetask import DriveTask | from db.models.drivetask import DriveTask | ||||
from db.models.auction import Auction | from db.models.auction import Auction | ||||
from db.models.fuelingtask import FuelingTask |
@@ -0,0 +1,17 @@ | |||||
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime | |||||
from sqlalchemy.orm import relationship | |||||
from db.base import Base | |||||
class FuelingTask(Base): | |||||
Id = Column(Integer, primary_key=True, index=True) | |||||
VehicleId = Column(ForeignKey("vehicle.Id"), nullable=False) | |||||
DriverId = Column(ForeignKey("user.Id"), nullable=False) | |||||
CreatedById = Column(ForeignKey("user.Id"), nullable=False) | |||||
Date = Column(DateTime, nullable=False) | |||||
Description = Column(String, nullable=True) | |||||
Cost = Column(Integer, nullable=False) | |||||
FuelRefilled = Column(Integer, nullable=False) | |||||
GasStationName = Column(String, nullable=False) | |||||
ImageBefore = Column(String, nullable=False) | |||||
ImageAfter = Column(String, nullable=False) | |||||
CreatedBy = relationship("User", back_populates="fuelingTasks", foreign_keys="FuelingTask.CreatedById") |
@@ -27,5 +27,5 @@ class User(Base): | |||||
#MaintenancePerson-specific relationships | #MaintenancePerson-specific relationships | ||||
maintenanceJobs = relationship("MaintenanceJob", back_populates="CreatedBy") | maintenanceJobs = relationship("MaintenanceJob", back_populates="CreatedBy") | ||||
#FuelingPerson-specific relationships | #FuelingPerson-specific relationships | ||||
#fuelingJobs = relationship("FuelingJob", back_populates="CreatedBy") | |||||
fuelingTasks = relationship("FuelingTask", back_populates="CreatedBy", foreign_keys="FuelingTask.CreatedById") | |||||
@@ -0,0 +1,49 @@ | |||||
from sqlalchemy.orm import Session | |||||
from schemas.fuelingtask import CreateFuelingTask | |||||
from db.models.fuelingtask import FuelingTask | |||||
from db.repository.user import get_car_driver, get_user_by_id | |||||
from db.repository.vehicle import get_vehicle_by_id | |||||
def create_fueling_task(fueling_task: CreateFuelingTask, current_user: int, db: Session): | |||||
if not get_vehicle_by_id(fueling_task.VehicleId, db=db): | |||||
return "novehicle" | |||||
driver = get_car_driver(fueling_task.VehicleId, db=db) | |||||
if not driver: | |||||
return "nodriver" | |||||
fueling_task_object = FuelingTask( | |||||
DriverId=driver.Id, | |||||
VehicleId=fueling_task.VehicleId, | |||||
CreatedById=current_user, | |||||
Description=fueling_task.Description, | |||||
Date=fueling_task.Date, | |||||
Cost=fueling_task.Cost, | |||||
FuelRefilled=fueling_task.FuelRefilled, | |||||
GasStationName=fueling_task.GasStationName, | |||||
ImageBefore=fueling_task.ImageBefore, | |||||
ImageAfter=fueling_task.ImageAfter, | |||||
) | |||||
db.add(fueling_task_object) | |||||
db.commit() | |||||
db.refresh(fueling_task_object) | |||||
return fueling_task_object | |||||
def delete_fueling_task(fueling_task_id: int, db: Session): | |||||
fueling_task = db.query(FuelingTask).filter(FuelingTask.Id == fueling_task_id).first() | |||||
if fueling_task: | |||||
db.delete(fueling_task) | |||||
db.commit() | |||||
return True | |||||
return False | |||||
def get_fueling_task_by_id(fuel_task_id: int, db: Session): | |||||
fuel_task = db.query(FuelingTask).filter(FuelingTask.Id == fuel_task_id).first() | |||||
res = fuel_task.__dict__ | |||||
res["Driver"] = get_user_by_id(fuel_task.DriverId, db=db) | |||||
return fuel_task |
@@ -0,0 +1 @@ | |||||
/usr/local/bin/python3.10/usr/local/bin/python |
@@ -0,0 +1,13 @@ | |||||
from pydantic import BaseModel, Field | |||||
from datetime import datetime | |||||
class CreateFuelingTask(BaseModel): | |||||
VehicleId: str = Field(...) | |||||
Description: str = Field(...) | |||||
Date: datetime = Field(...) | |||||
Cost: int = Field(...) | |||||
FuelRefilled: int = Field(...) | |||||
GasStationName: str = Field(...) | |||||
ImageBefore: str = Field(...) | |||||
ImageAfter: str = Field(...) |