From 617167200b2cf92ffcaa5250956cb3fb8efe782e Mon Sep 17 00:00:00 2001 From: Saruar Date: Sun, 22 Oct 2023 15:55:22 +0600 Subject: [PATCH] Fuel Task Added --- app/apis/base.py | 2 + app/apis/v1/route_fuelingtask.py | 71 ++++++++++++++++++++++++++++++++ app/db/base.py | 1 + app/db/models/fuelingtask.py | 17 ++++++++ app/db/models/user.py | 2 +- app/db/repository/fuelingtask.py | 49 ++++++++++++++++++++++ app/python | 1 + app/schemas/fuelingtask.py | 13 ++++++ 8 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 app/apis/v1/route_fuelingtask.py create mode 100644 app/db/repository/fuelingtask.py create mode 120000 app/python create mode 100644 app/schemas/fuelingtask.py diff --git a/app/apis/base.py b/app/apis/base.py index 29a1f3a..6bd9d88 100644 --- a/app/apis/base.py +++ b/app/apis/base.py @@ -7,6 +7,7 @@ from apis.v1 import ( route_auth, route_task, route_maintenancejob, + route_fuelingtask ) api_router = APIRouter() @@ -17,3 +18,4 @@ api_router.include_router(route_task.router, prefix="/task", tags=["task"]) api_router.include_router( route_maintenancejob.router, prefix="/maintenancejob", tags=["maintenancejob"] ) +api_router.include_router(route_fuelingtask.router, prefix="/fuel", tags=["fueltasks"]) diff --git a/app/apis/v1/route_fuelingtask.py b/app/apis/v1/route_fuelingtask.py new file mode 100644 index 0000000..df4b163 --- /dev/null +++ b/app/apis/v1/route_fuelingtask.py @@ -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" + ) \ No newline at end of file diff --git a/app/db/base.py b/app/db/base.py index e2eb3d3..2980c8a 100644 --- a/app/db/base.py +++ b/app/db/base.py @@ -6,3 +6,4 @@ from db.models.carpart import CarPart from db.models.maintenancejob import MaintenanceJob from db.models.drivetask import DriveTask from db.models.auction import Auction +from db.models.fuelingtask import FuelingTask diff --git a/app/db/models/fuelingtask.py b/app/db/models/fuelingtask.py index e69de29..4949c08 100644 --- a/app/db/models/fuelingtask.py +++ b/app/db/models/fuelingtask.py @@ -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") \ No newline at end of file diff --git a/app/db/models/user.py b/app/db/models/user.py index 9363676..42fdbeb 100644 --- a/app/db/models/user.py +++ b/app/db/models/user.py @@ -27,5 +27,5 @@ class User(Base): #MaintenancePerson-specific relationships maintenanceJobs = relationship("MaintenanceJob", back_populates="CreatedBy") #FuelingPerson-specific relationships - #fuelingJobs = relationship("FuelingJob", back_populates="CreatedBy") + fuelingTasks = relationship("FuelingTask", back_populates="CreatedBy", foreign_keys="FuelingTask.CreatedById") diff --git a/app/db/repository/fuelingtask.py b/app/db/repository/fuelingtask.py new file mode 100644 index 0000000..7051ba5 --- /dev/null +++ b/app/db/repository/fuelingtask.py @@ -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 \ No newline at end of file diff --git a/app/python b/app/python new file mode 120000 index 0000000..0356cdd --- /dev/null +++ b/app/python @@ -0,0 +1 @@ +/usr/local/bin/python3.10/usr/local/bin/python \ No newline at end of file diff --git a/app/schemas/fuelingtask.py b/app/schemas/fuelingtask.py new file mode 100644 index 0000000..4c1d3cc --- /dev/null +++ b/app/schemas/fuelingtask.py @@ -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(...) \ No newline at end of file