@@ -7,7 +7,8 @@ from apis.v1 import ( | |||||
route_auth, | route_auth, | ||||
route_task, | route_task, | ||||
route_maintenancejob, | route_maintenancejob, | ||||
route_fuelingtask | |||||
route_fuelingtask, | |||||
route_auction, | |||||
) | ) | ||||
api_router = APIRouter() | api_router = APIRouter() | ||||
@@ -19,3 +20,4 @@ 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"]) | api_router.include_router(route_fuelingtask.router, prefix="/fuel", tags=["fueltasks"]) | ||||
api_router.include_router(route_auction.router, prefix="/auction", tags=["auctions"]) |
@@ -0,0 +1,94 @@ | |||||
from fastapi import Depends, APIRouter, HTTPException, status | |||||
from sqlalchemy.orm import Session | |||||
from db.session import get_db | |||||
from schemas.auction import ShowAuction, CreateAuction | |||||
from typing import List | |||||
from db.repository.auction import ( | |||||
get_all_auctions, | |||||
get_auction_by_id, | |||||
create_new_auction, | |||||
edit_auction_by_id, | |||||
delete_auction_by_id, | |||||
) | |||||
from db.models.user import User | |||||
from apis.v1.route_auth import get_current_user | |||||
router = APIRouter() | |||||
@router.get("/", response_model=List[ShowAuction], status_code=status.HTTP_200_OK) | |||||
def getAllAuctions( | |||||
db: Session = Depends(get_db), | |||||
current_user: User = Depends(get_current_user), | |||||
): | |||||
auctions = get_all_auctions(db) | |||||
return auctions | |||||
@router.get("/{id}", response_model=ShowAuction, status_code=status.HTTP_200_OK) | |||||
def getAuction( | |||||
id: int, | |||||
db: Session = Depends(get_db), | |||||
current_user: User = Depends(get_current_user), | |||||
): | |||||
auction = get_auction_by_id(id, db) | |||||
return auction | |||||
@router.post("/", status_code=status.HTTP_201_CREATED) | |||||
def create_auction( | |||||
auction: CreateAuction, | |||||
db: Session = Depends(get_db), | |||||
current_user: User = Depends(get_current_user), | |||||
): | |||||
if current_user.Role != "Admin": | |||||
raise HTTPException( | |||||
status_code=403, detail="You are not authorized to perform this action" | |||||
) | |||||
auction_res = create_new_auction(auction=auction, createId=current_user.Id, db=db) | |||||
if auction_res == "novehicle": | |||||
raise HTTPException( | |||||
status_code=404, detail="Vehicle with this ID does not exist" | |||||
) | |||||
elif auction_res == "vehicleunavailable": | |||||
raise HTTPException( | |||||
status_code=404, detail="Vehicle with this ID is currently not up for sale" | |||||
) | |||||
return auction_res | |||||
@router.patch("/{auc_id}", status_code=status.HTTP_200_OK) | |||||
def editAuction( | |||||
auc_id: int, | |||||
auction: CreateAuction, | |||||
db: Session = Depends(get_db), | |||||
current_user: User = Depends(get_current_user), | |||||
): | |||||
if current_user.Role != "Admin": | |||||
raise HTTPException( | |||||
status_code=403, detail="You are not authorized to perform this action" | |||||
) | |||||
auction_res = edit_auction_by_id(id=auc_id, auction=auction, db=db) | |||||
if auction_res == "noauction": | |||||
raise HTTPException( | |||||
status_code=404, detail="Auction with this ID does not exist" | |||||
) | |||||
return auction_res | |||||
@router.delete("/{auc_id}", status_code=status.HTTP_200_OK) | |||||
def deleteAuction( | |||||
auc_id: int, | |||||
db: Session = Depends(get_db), | |||||
current_user: User = Depends(get_current_user), | |||||
): | |||||
if current_user.Role != "Admin": | |||||
raise HTTPException( | |||||
status_code=403, detail="You are not authorized to perform this action" | |||||
) | |||||
auction_res = delete_auction_by_id(id=auc_id, db=db) | |||||
if auction_res == "noauction": | |||||
raise HTTPException( | |||||
status_code=404, detail="Auction with this ID does not exist" | |||||
) | |||||
return auction_res |
@@ -6,9 +6,10 @@ from db.repository.maintenancejob import ( | |||||
create_car_part, | create_car_part, | ||||
get_all_maintenance_jobs, | get_all_maintenance_jobs, | ||||
get_maintenance_job, | get_maintenance_job, | ||||
change_maintenance_status | |||||
change_maintenance_status, | |||||
) | ) | ||||
from schemas.maintenancejob import CreateMaintenanceJob | |||||
from typing import List | |||||
from schemas.maintenancejob import CreateMaintenanceJob, OutputMaintenanceJob | |||||
from schemas.carpart import CreateCarPart | from schemas.carpart import CreateCarPart | ||||
from db.models.user import User | from db.models.user import User | ||||
from apis.v1.route_auth import get_current_user | from apis.v1.route_auth import get_current_user | ||||
@@ -48,7 +49,9 @@ def create_carpart( | |||||
return car_part_res | return car_part_res | ||||
@router.get("/", status_code=status.HTTP_200_OK) | |||||
@router.get( | |||||
"/", response_model=List[OutputMaintenanceJob], status_code=status.HTTP_200_OK | |||||
) | |||||
def get_all_maintenancejobs( | def get_all_maintenancejobs( | ||||
db: Session = Depends(get_db), | db: Session = Depends(get_db), | ||||
current_user: User = Depends(get_current_user), | current_user: User = Depends(get_current_user), | ||||
@@ -62,7 +65,11 @@ def get_all_maintenancejobs( | |||||
return maintenancejobs | return maintenancejobs | ||||
@router.get("/{maintenance_job_id}", status_code=status.HTTP_200_OK) | |||||
@router.get( | |||||
"/{maintenance_job_id}", | |||||
response_model=OutputMaintenanceJob, | |||||
status_code=status.HTTP_200_OK, | |||||
) | |||||
def get_maintenancejob( | def get_maintenancejob( | ||||
maintenance_job_id: int, | maintenance_job_id: int, | ||||
db: Session = Depends(get_db), | db: Session = Depends(get_db), | ||||
@@ -74,8 +81,10 @@ def get_maintenancejob( | |||||
) | ) | ||||
maintenancejob = get_maintenance_job(maintenance_job_id, db) | maintenancejob = get_maintenance_job(maintenance_job_id, db) | ||||
print(maintenancejob) | |||||
return maintenancejob | return maintenancejob | ||||
@router.patch("/{maintenance_job_id}", status_code=status.HTTP_200_OK) | @router.patch("/{maintenance_job_id}", status_code=status.HTTP_200_OK) | ||||
def change_status( | def change_status( | ||||
maintenance_job_id: int, | maintenance_job_id: int, | ||||
@@ -89,7 +98,5 @@ def change_status( | |||||
) | ) | ||||
result = change_maintenance_status(maintenance_job_id, status, db) | result = change_maintenance_status(maintenance_job_id, status, db) | ||||
if result is None: | if result is None: | ||||
raise HTTPException( | |||||
status_code=404, detail="Maintenance job not found" | |||||
) | |||||
return result | |||||
raise HTTPException(status_code=404, detail="Maintenance job not found") | |||||
return result |
@@ -9,11 +9,14 @@ from db.repository.drivetask import ( | |||||
get_all_tasks, | get_all_tasks, | ||||
get_task_by_id, | get_task_by_id, | ||||
get_tasks_by_driver, | get_tasks_by_driver, | ||||
edit_task | |||||
edit_task, | |||||
get_active_route_by_driver, | |||||
get_my_routes, | |||||
) | ) | ||||
from schemas.drivetask import CreateTask | |||||
from schemas.drivetask import CreateTask, ShowTask | |||||
from db.models.user import User | from db.models.user import User | ||||
from apis.v1.route_auth import get_current_user | from apis.v1.route_auth import get_current_user | ||||
from typing import List | |||||
router = APIRouter() | router = APIRouter() | ||||
@@ -68,13 +71,14 @@ def changeStatus( | |||||
) | ) | ||||
@router.get("/", status_code=status.HTTP_200_OK) | |||||
@router.get("/", response_model=List[ShowTask], status_code=status.HTTP_200_OK) | |||||
def getAllTasks( | def getAllTasks( | ||||
status: str = "Any", | |||||
db: Session = Depends(get_db), | db: Session = Depends(get_db), | ||||
current_user: User = Depends(get_current_user), | current_user: User = Depends(get_current_user), | ||||
): | ): | ||||
if current_user.Role == "Admin": | if current_user.Role == "Admin": | ||||
tasks = get_all_tasks(db) | |||||
tasks = get_all_tasks(status, db) | |||||
return tasks | return tasks | ||||
else: | else: | ||||
raise HTTPException( | raise HTTPException( | ||||
@@ -82,7 +86,7 @@ def getAllTasks( | |||||
) | ) | ||||
@router.get("/{task_id}", status_code=status.HTTP_200_OK) | |||||
@router.get("/{task_id}", response_model=ShowTask, status_code=status.HTTP_200_OK) | |||||
def getTaskById( | def getTaskById( | ||||
task_id: int, | task_id: int, | ||||
db: Session = Depends(get_db), | db: Session = Depends(get_db), | ||||
@@ -120,6 +124,7 @@ def getTasksByDriver( | |||||
) | ) | ||||
return tasks | return tasks | ||||
@router.put("/{task_id}", status_code=status.HTTP_200_OK) | @router.put("/{task_id}", status_code=status.HTTP_200_OK) | ||||
def updateTask( | def updateTask( | ||||
task_id: int, | task_id: int, | ||||
@@ -133,7 +138,48 @@ def updateTask( | |||||
) | ) | ||||
task = edit_task(task_id, task, db) | task = edit_task(task_id, task, db) | ||||
if task == "notaskfound": | if task == "notaskfound": | ||||
raise HTTPException(status_code=404, detail=f"Task with id {task_id} not found") | |||||
return task | |||||
@router.get("/active/{driver_id}", status_code=status.HTTP_200_OK) | |||||
def getActiveRoute( | |||||
driver_id: int, | |||||
db: Session = Depends(get_db), | |||||
current_user: User = Depends(get_current_user), | |||||
): | |||||
if current_user.Role != "Admin" and current_user.Id != driver_id: | |||||
raise HTTPException( | |||||
status_code=403, detail="You are not authorized to perform this action" | |||||
) | |||||
route = get_active_route_by_driver(driver_id, db) | |||||
if route == "notdriver": | |||||
raise HTTPException( | |||||
status_code=404, detail=f"Driver with id {driver_id} not found" | |||||
) | |||||
if route == "noroute": | |||||
raise HTTPException( | |||||
status_code=404, detail=f"Driver with id {driver_id} has no active route" | |||||
) | |||||
return route | |||||
@router.get("/myroutes", status_code=status.HTTP_200_OK) | |||||
def getMyRoutes( | |||||
db: Session = Depends(get_db), | |||||
current_user: User = Depends(get_current_user), | |||||
): | |||||
if current_user.Role != "Driver": | |||||
raise HTTPException( | |||||
status_code=403, detail="You are not authorized to perform this action" | |||||
) | |||||
routes = get_my_routes(current_user.Id, db) | |||||
if routes == "notdriver": | |||||
raise HTTPException( | |||||
status_code=404, detail=f"Driver with id {current_user.Id} not found" | |||||
) | |||||
if not routes: | |||||
raise HTTPException( | raise HTTPException( | ||||
status_code=404, detail=f"Task with id {task_id} not found" | |||||
status_code=404, detail=f"Driver with id {current_user.Id} has no routes" | |||||
) | ) | ||||
return task | |||||
return routes |
@@ -7,7 +7,14 @@ from apis.v1.route_auth import get_current_user | |||||
from db.models.user import User | from db.models.user import User | ||||
from schemas.user import UserCreate, ShowUser, ShowDriver, DriverCreate | from schemas.user import UserCreate, ShowUser, ShowDriver, DriverCreate | ||||
from db.session import get_db | from db.session import get_db | ||||
from db.repository.user import create_new_user, list_users, get_user_by_id, replace_user_data, create_new_driver | |||||
from db.repository.user import ( | |||||
create_new_user, | |||||
list_users, | |||||
get_user_by_id, | |||||
replace_user_data, | |||||
create_new_driver, | |||||
delete_user_data, | |||||
) | |||||
router = APIRouter() | router = APIRouter() | ||||
@@ -19,24 +26,30 @@ def create_user( | |||||
db: Session = Depends(get_db), | db: Session = Depends(get_db), | ||||
current_user: User = Depends(get_current_user), | current_user: User = Depends(get_current_user), | ||||
): | ): | ||||
if (current_user.Role != "Admin"): | |||||
raise HTTPException(status_code=403, detail="You are not authorized to perform this action") | |||||
if current_user.Role != "Admin": | |||||
raise HTTPException( | |||||
status_code=403, detail="You are not authorized to perform this action" | |||||
) | |||||
# if current_user.Role != "Admin": | # if current_user.Role != "Admin": | ||||
# raise HTTPException(status_code=403, detail="You are not authorized to perform this action") | # raise HTTPException(status_code=403, detail="You are not authorized to perform this action") | ||||
user = create_new_user(user=user, db=db) | user = create_new_user(user=user, db=db) | ||||
return user | return user | ||||
@router.post("/driver", response_model=ShowDriver, status_code=status.HTTP_201_CREATED) | @router.post("/driver", response_model=ShowDriver, status_code=status.HTTP_201_CREATED) | ||||
def create_driver( | def create_driver( | ||||
driver: DriverCreate, | driver: DriverCreate, | ||||
db: Session = Depends(get_db), | db: Session = Depends(get_db), | ||||
current_user: User = Depends(get_current_user), | current_user: User = Depends(get_current_user), | ||||
): | ): | ||||
if (current_user.Role != "Admin"): | |||||
raise HTTPException(status_code=403, detail="You are not authorized to perform this action") | |||||
if current_user.Role != "Admin": | |||||
raise HTTPException( | |||||
status_code=403, detail="You are not authorized to perform this action" | |||||
) | |||||
driver = create_new_driver(driver=driver, db=db) | driver = create_new_driver(driver=driver, db=db) | ||||
return driver | return driver | ||||
@router.get("/", response_model=List[ShowUser], status_code=status.HTTP_200_OK) | @router.get("/", response_model=List[ShowUser], status_code=status.HTTP_200_OK) | ||||
def get_all_users(db: Session = Depends(get_db), role: str = None): | def get_all_users(db: Session = Depends(get_db), role: str = None): | ||||
if role is None: | if role is None: | ||||
@@ -45,6 +58,7 @@ def get_all_users(db: Session = Depends(get_db), role: str = None): | |||||
users = list_users(db=db, role=role) | users = list_users(db=db, role=role) | ||||
return users | return users | ||||
@router.put("/{user_id}", response_model=ShowUser, status_code=status.HTTP_202_ACCEPTED) | @router.put("/{user_id}", response_model=ShowUser, status_code=status.HTTP_202_ACCEPTED) | ||||
def update_user( | def update_user( | ||||
user_id: int, | user_id: int, | ||||
@@ -53,7 +67,9 @@ def update_user( | |||||
current_user: User = Depends(get_current_user), | current_user: User = Depends(get_current_user), | ||||
): | ): | ||||
if current_user.Role != "Admin": | if current_user.Role != "Admin": | ||||
raise HTTPException(status_code=403, detail="You are not authorized to perform this action") | |||||
raise HTTPException( | |||||
status_code=403, detail="You are not authorized to perform this action" | |||||
) | |||||
user = replace_user_data(user_id=user_id, user=user, db=db) | user = replace_user_data(user_id=user_id, user=user, db=db) | ||||
return user | return user | ||||
@@ -75,11 +91,30 @@ def get_user(user_id: int, db: Session = Depends(get_db)): | |||||
return user | return user | ||||
@router.get("/driver/{driver_id}", response_model=ShowDriver, status_code=status.HTTP_200_OK) | |||||
@router.get( | |||||
"/driver/{driver_id}", response_model=ShowDriver, status_code=status.HTTP_200_OK | |||||
) | |||||
def get_driver(driver_id: int, db: Session = Depends(get_db)): | def get_driver(driver_id: int, db: Session = Depends(get_db)): | ||||
driver = get_user_by_id(user_id=driver_id, role = "Driver", db=db) | |||||
res = driver.__dict__ | |||||
res["AssignedVehicle"] = driver.vehicle | |||||
driver = get_user_by_id(user_id=driver_id, role="Driver", db=db) | |||||
if not driver: | if not driver: | ||||
raise HTTPException(status_code=404, detail="Driver not found") | raise HTTPException(status_code=404, detail="Driver not found") | ||||
return driver | |||||
res = driver.__dict__ | |||||
res["AssignedVehicle"] = driver.vehicle | |||||
return driver | |||||
@router.delete("/{user_id}", status_code=status.HTTP_200_OK) | |||||
def delete_user( | |||||
user_id: int, | |||||
db: Session = Depends(get_db), | |||||
current_user: User = Depends(get_current_user), | |||||
): | |||||
if current_user.Role != "Admin": | |||||
raise HTTPException( | |||||
status_code=403, detail="You are not authorized to perform this action" | |||||
) | |||||
result = delete_user_data(id=user_id, db=db) | |||||
if result == "userNotFound": | |||||
raise HTTPException(status_code=404, detail="User not found") | |||||
return result |
@@ -87,6 +87,24 @@ async def assign_driver( | |||||
return vehicle | return vehicle | ||||
@router.patch("/{vehicle_id}/driver", status_code=status.HTTP_200_OK) | |||||
async def unassign_driver( | |||||
vehicle_id: int, | |||||
db: Session = Depends(get_db), | |||||
current_user: User = Depends(get_current_user), | |||||
): | |||||
if current_user.Role != "Admin": | |||||
raise HTTPException( | |||||
status_code=403, detail="You are not authorized to perform this action" | |||||
) | |||||
vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=None, db=db) | |||||
if vehicle == "novehicle": | |||||
raise HTTPException( | |||||
status_code=404, detail=f"Vehicle with id {vehicle_id} not found" | |||||
) | |||||
return {"msg": "Driver unassigned successfully"} | |||||
@router.get("/", response_model=List[OutputVehicle], status_code=status.HTTP_200_OK) | @router.get("/", response_model=List[OutputVehicle], status_code=status.HTTP_200_OK) | ||||
async def get_all_vehicles(db: Session = Depends(get_db)): | async def get_all_vehicles(db: Session = Depends(get_db)): | ||||
vehicles = list_vehicles(db=db) | vehicles = list_vehicles(db=db) | ||||
@@ -1,11 +1,11 @@ | |||||
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime | |||||
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime | |||||
from sqlalchemy.orm import relationship | from sqlalchemy.orm import relationship | ||||
from db.base import Base | from db.base import Base | ||||
class Auction(Base): | class Auction(Base): | ||||
Id = Column(Integer, primary_key=True, index=True) | Id = Column(Integer, primary_key=True, index=True) | ||||
initialPrice = Column(Integer, nullable=False) | initialPrice = Column(Integer, nullable=False) | ||||
minimalBet = Column(Integer, nullable=False) | minimalBet = Column(Integer, nullable=False) | ||||
carID = Column(Integer, ForeignKey("vehicle.Id"), nullable=False) | carID = Column(Integer, ForeignKey("vehicle.Id"), nullable=False) | ||||
@@ -14,4 +14,3 @@ class Auction(Base): | |||||
CreatedBy = relationship("User", back_populates="auctions") | CreatedBy = relationship("User", back_populates="auctions") | ||||
dateStart = Column(DateTime, nullable=False) | dateStart = Column(DateTime, nullable=False) | ||||
dateEnd = Column(DateTime, nullable=False) | dateEnd = Column(DateTime, nullable=False) | ||||
@@ -1,4 +1,4 @@ | |||||
from sqlalchemy import Column, Integer, String, ForeignKey, ARRAY | |||||
from sqlalchemy import Column, Integer, String, ForeignKey, ARRAY, DateTime | |||||
from db.base import Base | from db.base import Base | ||||
from sqlalchemy.orm import relationship | from sqlalchemy.orm import relationship | ||||
@@ -11,3 +11,4 @@ class DriveTask(Base): | |||||
Status = Column(String, nullable=False) | Status = Column(String, nullable=False) | ||||
StartLocation = Column(ARRAY(String), nullable=False) | StartLocation = Column(ARRAY(String), nullable=False) | ||||
EndLocation = Column(ARRAY(String), nullable=False) | EndLocation = Column(ARRAY(String), nullable=False) | ||||
StartDateTime = Column(DateTime, nullable=False) |
@@ -2,6 +2,7 @@ from sqlalchemy import Column, Integer, String, ForeignKey, DateTime | |||||
from sqlalchemy.orm import relationship | from sqlalchemy.orm import relationship | ||||
from db.base import Base | from db.base import Base | ||||
class FuelingTask(Base): | class FuelingTask(Base): | ||||
Id = Column(Integer, primary_key=True, index=True) | Id = Column(Integer, primary_key=True, index=True) | ||||
VehicleId = Column(ForeignKey("vehicle.Id"), nullable=False) | VehicleId = Column(ForeignKey("vehicle.Id"), nullable=False) | ||||
@@ -14,4 +15,6 @@ class FuelingTask(Base): | |||||
GasStationName = Column(String, nullable=False) | GasStationName = Column(String, nullable=False) | ||||
ImageBefore = Column(String, nullable=False) | ImageBefore = Column(String, nullable=False) | ||||
ImageAfter = Column(String, nullable=False) | ImageAfter = Column(String, nullable=False) | ||||
CreatedBy = relationship("User", back_populates="fuelingTasks", foreign_keys="FuelingTask.CreatedById") | |||||
CreatedBy = relationship( | |||||
"User", back_populates="fuelingTasks", foreign_keys="FuelingTask.CreatedById" | |||||
) |
@@ -0,0 +1,61 @@ | |||||
from sqlalchemy.orm import Session | |||||
from schemas.auction import CreateAuction | |||||
from db.models.auction import Auction | |||||
from db.repository.user import get_user_by_id | |||||
from db.repository.vehicle import get_vehicle_by_id | |||||
def get_all_auctions(db: Session): | |||||
auctions = db.query(Auction).all() | |||||
for auction in auctions: | |||||
auction.car = auction.vehicle | |||||
return auctions | |||||
def get_auction_by_id(id: int, db: Session): | |||||
auction = db.query(Auction).filter(Auction.Id == id).first() | |||||
auction.car = auction.vehicle | |||||
return auction | |||||
def create_new_auction(auction: CreateAuction, createId: int, db: Session): | |||||
vehicle = get_vehicle_by_id(auction.carId, db) | |||||
if vehicle is None: | |||||
return "novehicle" | |||||
auction_object = Auction( | |||||
initialPrice=auction.initialPrice, | |||||
minimalBet=auction.minimalBet, | |||||
carID=auction.carId, | |||||
creatorId=createId, | |||||
dateStart=auction.dateStart, | |||||
dateEnd=auction.dateEnd, | |||||
) | |||||
db.add(auction_object) | |||||
db.commit() | |||||
db.refresh(auction_object) | |||||
return auction_object | |||||
def delete_auction_by_id(id: int, db: Session): | |||||
auction = db.query(Auction).filter(Auction.Id == id).first() | |||||
if not auction: | |||||
return "noauction" | |||||
db.delete(auction) | |||||
db.commit() | |||||
return auction | |||||
def edit_auction_by_id(id: int, auction: CreateAuction, db: Session): | |||||
auction_db = db.query(Auction).filter(Auction.Id == id) | |||||
auction_object = auction_db.first() | |||||
if not auction_object: | |||||
return "noauction" | |||||
auction_object.initialPrice = auction.initialPrice | |||||
auction_object.minimalBet = auction.minimalBet | |||||
auction_object.carID = auction.carId | |||||
auction_object.dateStart = auction.dateStart | |||||
auction_object.dateEnd = auction.dateEnd | |||||
db.add(auction_object) | |||||
db.commit() | |||||
db.refresh(auction_object) | |||||
return auction_object |
@@ -6,7 +6,7 @@ from db.repository.user import get_user_by_id | |||||
def create_new_task(task: CreateTask, db: Session): | def create_new_task(task: CreateTask, db: Session): | ||||
driver = get_user_by_id(task.DriverId, db) | |||||
driver = get_user_by_id(task.DriverId, "Driver", db) | |||||
if driver is None: | if driver is None: | ||||
return "notdriver" | return "notdriver" | ||||
elif driver.Role != "Driver": | elif driver.Role != "Driver": | ||||
@@ -28,6 +28,12 @@ def change_task_status(task_id: int, status: str, db: Session): | |||||
task = db.query(DriveTask).filter(DriveTask.Id == task_id).first() | task = db.query(DriveTask).filter(DriveTask.Id == task_id).first() | ||||
if not task: | if not task: | ||||
return "notaskfound" | return "notaskfound" | ||||
if status == "In Progress": | |||||
# see if there are any other tasks in progress by this driver, if yes, cancel | |||||
tasks = db.query(DriveTask).filter(DriveTask.DriverId == task.DriverId).all() | |||||
for task in tasks: | |||||
if task.Status == "In Progress": | |||||
return "driverhasothertask" | |||||
task.Status = status | task.Status = status | ||||
db.commit() | db.commit() | ||||
db.refresh(task) | db.refresh(task) | ||||
@@ -38,14 +44,14 @@ def get_task_driver(task_id: int, db: Session): | |||||
task = db.query(DriveTask).filter(DriveTask.Id == task_id).first() | task = db.query(DriveTask).filter(DriveTask.Id == task_id).first() | ||||
if not task: | if not task: | ||||
return "notaskfound" | return "notaskfound" | ||||
driver = get_user_by_id(task.DriverId, db) | |||||
driver = get_user_by_id(task.DriverId, "Any", db) | |||||
if not driver: | if not driver: | ||||
return "notdriver" | return "notdriver" | ||||
return driver | return driver | ||||
def get_tasks_by_driver(driver_id: int, db: Session): | def get_tasks_by_driver(driver_id: int, db: Session): | ||||
driver = get_user_by_id(driver_id, db) | |||||
driver = get_user_by_id(driver_id, "Any", db) | |||||
if not driver: | if not driver: | ||||
return "notdriver" | return "notdriver" | ||||
if driver.Role != "Driver": | if driver.Role != "Driver": | ||||
@@ -56,15 +62,24 @@ def get_tasks_by_driver(driver_id: int, db: Session): | |||||
def get_task_by_id(task_id: int, db: Session): | def get_task_by_id(task_id: int, db: Session): | ||||
task = db.query(DriveTask).filter(DriveTask.Id == task_id).first() | task = db.query(DriveTask).filter(DriveTask.Id == task_id).first() | ||||
task.Driver = task.CreatedBy.__dict__ | |||||
task.Driver["AssignedVehicle"] = task.CreatedBy.vehicle | |||||
if not task: | if not task: | ||||
return "notaskfound" | return "notaskfound" | ||||
return task | return task | ||||
def get_all_tasks(db: Session): | |||||
tasks = db.query(DriveTask).all() | |||||
def get_all_tasks(status: str, db: Session): | |||||
if status != "Any": | |||||
tasks = db.query(DriveTask).filter(DriveTask.Status == status).all() | |||||
else: | |||||
tasks = db.query(DriveTask).all() | |||||
for task in tasks: | |||||
task.Driver = task.CreatedBy.__dict__ | |||||
task.Driver["AssignedVehicle"] = task.CreatedBy.vehicle | |||||
return tasks | return tasks | ||||
def edit_task(task_id: int, task: CreateTask, db: Session): | def edit_task(task_id: int, task: CreateTask, db: Session): | ||||
tasks = db.query(DriveTask).filter(DriveTask.Id == task_id).first() | tasks = db.query(DriveTask).filter(DriveTask.Id == task_id).first() | ||||
if not tasks: | if not tasks: | ||||
@@ -75,3 +90,22 @@ def edit_task(task_id: int, task: CreateTask, db: Session): | |||||
db.commit() | db.commit() | ||||
db.refresh(tasks) | db.refresh(tasks) | ||||
return tasks | return tasks | ||||
def get_active_route_by_driver(driver_id: int, db: Session): | |||||
driver = get_user_by_id(driver_id, "Driver", db) | |||||
if not driver: | |||||
return "notdriver" | |||||
tasks = db.query(DriveTask).filter(DriveTask.DriverId == driver_id).all() | |||||
for task in tasks: | |||||
if task.Status == "In Progress": | |||||
return task | |||||
return "noroute" | |||||
def get_my_routes(driver_id: int, db: Session): | |||||
driver = get_user_by_id(driver_id, "Driver", db) | |||||
if not driver: | |||||
return "notdriver" | |||||
tasks = db.query(DriveTask).filter(DriveTask.DriverId == driver_id).all() | |||||
return tasks |
@@ -39,7 +39,7 @@ def create_car_part(car_part: CreateCarPart, db: Session): | |||||
def calculate_total_cost(car_parts: CarPart): | def calculate_total_cost(car_parts: CarPart): | ||||
total_cost = 0 | total_cost = 0 | ||||
for part in car_parts: | for part in car_parts: | ||||
total_cost += part["Cost"] | |||||
total_cost += part.Cost | |||||
return total_cost | return total_cost | ||||
@@ -48,8 +48,9 @@ def get_all_maintenance_jobs(db: Session): | |||||
result = [] | result = [] | ||||
for job in maintenancejobs: | for job in maintenancejobs: | ||||
job_dict = job.__dict__ | job_dict = job.__dict__ | ||||
job_dict["CarParts"] = [part.__dict__ for part in job.CarParts] | |||||
job_dict["CarPartsList"] = [part.__dict__ for part in job.CarParts] | |||||
job_dict["TotalCost"] = calculate_total_cost(job.CarParts) | job_dict["TotalCost"] = calculate_total_cost(job.CarParts) | ||||
job_dict["AssignedTo"] = job.CreatedBy.__dict__ | |||||
result.append(job_dict) | result.append(job_dict) | ||||
return maintenancejobs | return maintenancejobs | ||||
@@ -58,9 +59,15 @@ def get_maintenance_job(maintenancejob_id: int, db: Session): | |||||
maintenancejob = ( | maintenancejob = ( | ||||
db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first() | db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first() | ||||
) | ) | ||||
result = maintenancejob.__dict__ | |||||
result["CarParts"] = [part.__dict__ for part in maintenancejob.CarParts] | |||||
return result | |||||
maintenancejob.CarPartsList = [part.__dict__ for part in maintenancejob.CarParts] | |||||
# print(type(result.CarPartsList)) | |||||
maintenancejob.TotalCost = calculate_total_cost(maintenancejob.CarParts) | |||||
# print(result.TotalCost) | |||||
maintenancejob.AssignedTo = maintenancejob.CreatedBy.__dict__ | |||||
print(maintenancejob.AssignedTo) | |||||
print("DB Access complete") | |||||
return maintenancejob | |||||
def change_maintenance_status(maintenancejob_id: int, status: str, db: Session): | def change_maintenance_status(maintenancejob_id: int, status: str, db: Session): | ||||
maintenancejob = ( | maintenancejob = ( | ||||
@@ -71,4 +78,4 @@ def change_maintenance_status(maintenancejob_id: int, status: str, db: Session): | |||||
maintenancejob.Status = status | maintenancejob.Status = status | ||||
db.commit() | db.commit() | ||||
db.refresh(maintenancejob) | db.refresh(maintenancejob) | ||||
return maintenancejob | |||||
return maintenancejob |
@@ -4,6 +4,7 @@ from sqlalchemy.orm import Session | |||||
from schemas.user import UserCreate, DriverCreate | from schemas.user import UserCreate, DriverCreate | ||||
from db.models.user import User | from db.models.user import User | ||||
from core.hashing import Hasher | from core.hashing import Hasher | ||||
from db.models.drivetask import DriveTask | |||||
def create_new_user(user: UserCreate, db: Session): | def create_new_user(user: UserCreate, db: Session): | ||||
@@ -22,6 +23,8 @@ def create_new_user(user: UserCreate, db: Session): | |||||
db.commit() | db.commit() | ||||
db.refresh(user_object) | db.refresh(user_object) | ||||
return user_object | return user_object | ||||
def create_new_driver(driver: DriverCreate, db: Session): | def create_new_driver(driver: DriverCreate, db: Session): | ||||
driver_object = User( | driver_object = User( | ||||
Email=driver.Email, | Email=driver.Email, | ||||
@@ -33,16 +36,19 @@ def create_new_driver(driver: DriverCreate, db: Session): | |||||
ContactNumber=driver.ContactNumber, | ContactNumber=driver.ContactNumber, | ||||
Role="Driver", | Role="Driver", | ||||
HashedPassword=Hasher.get_password_hash(driver.Password), | HashedPassword=Hasher.get_password_hash(driver.Password), | ||||
DrivingLicenseNumber=driver.DrivingLicenseNumber | |||||
DrivingLicenseNumber=driver.DrivingLicenseNumber, | |||||
) | ) | ||||
db.add(driver_object) | db.add(driver_object) | ||||
db.commit() | db.commit() | ||||
db.refresh(driver_object) | db.refresh(driver_object) | ||||
return driver_object | return driver_object | ||||
def get_user_by_id(user_id: int, role: str, db: Session): | def get_user_by_id(user_id: int, role: str, db: Session): | ||||
user = db.query(User).filter(User.Id == user_id).first() | user = db.query(User).filter(User.Id == user_id).first() | ||||
if user.Role != role: | |||||
if not user: | |||||
return False | |||||
if user.Role != role and role != "Any": | |||||
return False | return False | ||||
return user | return user | ||||
@@ -77,6 +83,7 @@ def list_users(db: Session, role: str = "Any"): | |||||
users = db.query(User).filter((User.Role == role) | (role == "Any")).all() | users = db.query(User).filter((User.Role == role) | (role == "Any")).all() | ||||
return users | return users | ||||
def replace_user_data(user_id: int, user_data: UserCreate, db: Session): | def replace_user_data(user_id: int, user_data: UserCreate, db: Session): | ||||
user = db.query(User).filter(User.Id == user_id).first() | user = db.query(User).filter(User.Id == user_id).first() | ||||
if not user: | if not user: | ||||
@@ -92,4 +99,16 @@ def replace_user_data(user_id: int, user_data: UserCreate, db: Session): | |||||
user.HashedPassword = Hasher.get_password_hash(user_data.Password) | user.HashedPassword = Hasher.get_password_hash(user_data.Password) | ||||
db.commit() | db.commit() | ||||
db.refresh(user) | db.refresh(user) | ||||
return user | |||||
return user | |||||
def delete_user_data(id: int, db: Session): | |||||
user = db.query(User).filter(User.Id == id).first() | |||||
if not user: | |||||
return "userNotFound" | |||||
drivetasks = db.query(DriveTask).filter(DriveTask.DriverId == id).all() | |||||
for task in drivetasks: # delete all tasks assigned to this user | |||||
db.delete(task) | |||||
db.delete(user) | |||||
db.commit() | |||||
return "userDeleted" |
@@ -7,10 +7,7 @@ from db.repository.user import get_car_driver | |||||
def create_new_vehicle(vehicle: CreateVehicle, db: Session): | def create_new_vehicle(vehicle: CreateVehicle, db: Session): | ||||
vehicle_object = Vehicle( | vehicle_object = Vehicle( | ||||
**vehicle.model_dump(), | |||||
Fuel=0, | |||||
Status="Inactive", | |||||
CurrentLocation=[] | |||||
**vehicle.model_dump(), Fuel=0, Status="Inactive", CurrentLocation=[] | |||||
) | ) | ||||
db.add(vehicle_object) | db.add(vehicle_object) | ||||
db.commit() | db.commit() | ||||
@@ -32,16 +29,30 @@ def update_vehicle_geoloc(vehicle_id: int, location: VehicleLocation, db: Sessio | |||||
def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session): | def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session): | ||||
driver = ( | |||||
db.query(User).filter((User.Id == driver_id) & (User.Role == "Driver")).first() | |||||
) | |||||
print(driver) | |||||
vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first() | |||||
if not vehicle: | |||||
return "novehicle" | |||||
if not driver: | |||||
return "nodriver" | |||||
driver.AssignedVehicle = vehicle_id | |||||
if driver_id != None: | |||||
driver = ( | |||||
db.query(User) | |||||
.filter((User.Id == driver_id) & (User.Role == "Driver")) | |||||
.first() | |||||
) | |||||
print(driver) | |||||
vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first() | |||||
if not vehicle: | |||||
return "novehicle" | |||||
if not driver: | |||||
return "nodriver" | |||||
driver.AssignedVehicle = vehicle_id | |||||
else: | |||||
vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first() | |||||
if not vehicle: | |||||
return "novehicle" | |||||
if vehicle.Status == "Active": | |||||
return "alreadyassigned" | |||||
driver = get_car_driver(vehicle_id, db) | |||||
if driver: | |||||
driver.AssignedVehicle = None | |||||
else: | |||||
return "nodriver" | |||||
db.add(driver) | db.add(driver) | ||||
db.commit() | db.commit() | ||||
return vehicle | return vehicle | ||||
@@ -53,7 +64,7 @@ def list_vehicles(db: Session): | |||||
for vehicle in vehicles: | for vehicle in vehicles: | ||||
driver = get_car_driver(vehicle.Id, db) | driver = get_car_driver(vehicle.Id, db) | ||||
if driver: | if driver: | ||||
vehicle.AssignedDriver = driver.Id | |||||
vehicle.AssignedDriver = driver.__dict__ | |||||
else: | else: | ||||
vehicle.AssignedDriver = None | vehicle.AssignedDriver = None | ||||
return vehicles | return vehicles | ||||
@@ -65,7 +76,7 @@ def get_vehicle_by_id(vehicle_id: int, db: Session): | |||||
return None | return None | ||||
driver = get_car_driver(vehicle.Id, db) | driver = get_car_driver(vehicle.Id, db) | ||||
if driver: | if driver: | ||||
vehicle.AssignedDriver = driver.Id | |||||
vehicle.AssignedDriver = driver.__dict__ | |||||
else: | else: | ||||
vehicle.AssignedDriver = None | vehicle.AssignedDriver = None | ||||
return vehicle | return vehicle | ||||
@@ -0,0 +1,23 @@ | |||||
from pydantic import BaseModel, Field | |||||
from datetime import datetime | |||||
from schemas.vehicle import OutputVehicle | |||||
class CreateAuction(BaseModel): | |||||
initialPrice: int = Field(...) | |||||
minimalBet: int = Field(...) | |||||
carId: int = Field(...) | |||||
dateStart: datetime | |||||
dateEnd: datetime | |||||
class ShowAuction(BaseModel): | |||||
Id: int | |||||
initialPrice: int | |||||
minimalBet: int | |||||
car: OutputVehicle | |||||
dateStart: datetime | |||||
dateEnd: datetime | |||||
class Config: | |||||
orm_mode = True |
@@ -8,3 +8,12 @@ class CreateCarPart(BaseModel): | |||||
Condition: str = Field(...) | Condition: str = Field(...) | ||||
ImageURL: str = Field(...) | ImageURL: str = Field(...) | ||||
Cost: int = Field(...) | Cost: int = Field(...) | ||||
class ShowCarPart(BaseModel): | |||||
ParentId: int = Field(...) | |||||
Name: str = Field(...) | |||||
Number: str = Field(...) | |||||
Condition: str = Field(...) | |||||
ImageURL: str = Field(...) | |||||
Cost: int = Field(...) |
@@ -1,4 +1,7 @@ | |||||
from pydantic import BaseModel, Field | from pydantic import BaseModel, Field | ||||
from datetime import datetime | |||||
from schemas.user import ShowDriver | |||||
from typing import Optional | |||||
class CreateTask(BaseModel): | class CreateTask(BaseModel): | ||||
@@ -10,11 +13,12 @@ class CreateTask(BaseModel): | |||||
class ShowTask(BaseModel): | class ShowTask(BaseModel): | ||||
Id: int | Id: int | ||||
DriverId: int | |||||
Driver: Optional[ShowDriver] | |||||
Description: str | Description: str | ||||
Status: str | Status: str | ||||
StartLocation: tuple[str, str] | StartLocation: tuple[str, str] | ||||
EndLocation: tuple[str, str] | EndLocation: tuple[str, str] | ||||
StartDateTime: Optional[datetime] | |||||
class Config: | class Config: | ||||
orm_mode = True | orm_mode = True |
@@ -1,7 +1,19 @@ | |||||
from pydantic import BaseModel, Field | from pydantic import BaseModel, Field | ||||
from datetime import datetime | from datetime import datetime | ||||
from typing import List, Optional | |||||
from schemas.carpart import ShowCarPart | |||||
from schemas.user import ShowUser | |||||
class CreateMaintenanceJob(BaseModel): | class CreateMaintenanceJob(BaseModel): | ||||
Description: str = Field(...) | Description: str = Field(...) | ||||
Date: datetime = Field(...) | Date: datetime = Field(...) | ||||
class OutputMaintenanceJob(BaseModel): | |||||
Id: int | |||||
Description: str | |||||
Date: datetime | |||||
CarPartsList: Optional[List[ShowCarPart]] | |||||
TotalCost: int | |||||
AssignedTo: ShowUser |
@@ -1,7 +1,7 @@ | |||||
# Purpose: User schema for pydantic (validation, inside-api usage) | # Purpose: User schema for pydantic (validation, inside-api usage) | ||||
from pydantic import BaseModel, EmailStr, Field | from pydantic import BaseModel, EmailStr, Field | ||||
from schemas.vehicle import OutputVehicle | |||||
class UserCreate(BaseModel): | class UserCreate(BaseModel): | ||||
Email: EmailStr | Email: EmailStr | ||||
@@ -14,9 +14,10 @@ class UserCreate(BaseModel): | |||||
Address: str = Field(...) | Address: str = Field(...) | ||||
Email: EmailStr = Field(...) | Email: EmailStr = Field(...) | ||||
class DriverCreate(UserCreate): | class DriverCreate(UserCreate): | ||||
DrivingLicenseNumber: str = Field(...) | DrivingLicenseNumber: str = Field(...) | ||||
class ShowUser(BaseModel): | class ShowUser(BaseModel): | ||||
Id: int | Id: int | ||||
@@ -29,11 +30,18 @@ class ShowUser(BaseModel): | |||||
Email: EmailStr | Email: EmailStr | ||||
Role: str | Role: str | ||||
class Config: | class Config: | ||||
orm_mode = True | orm_mode = True | ||||
validate_assignment = True | validate_assignment = True | ||||
class ShowDriverNoVehicle(ShowUser): | |||||
DrivingLicenseNumber: str | None | |||||
from schemas.vehicle import OutputVehicle | |||||
class ShowDriver(ShowUser): | class ShowDriver(ShowUser): | ||||
DrivingLicenseNumber: str | None | DrivingLicenseNumber: str | None | ||||
AssignedVehicle: OutputVehicle | None | |||||
AssignedVehicle: OutputVehicle | None |
@@ -1,5 +1,6 @@ | |||||
from typing import Optional | from typing import Optional | ||||
from pydantic import BaseModel | from pydantic import BaseModel | ||||
from schemas.user import ShowDriverNoVehicle | |||||
class CreateVehicle(BaseModel): | class CreateVehicle(BaseModel): | ||||
@@ -21,7 +22,7 @@ class OutputVehicle(BaseModel): | |||||
Fuel: Optional[int] = 0 | Fuel: Optional[int] = 0 | ||||
Type: str | Type: str | ||||
DriverHistory: Optional[list[int]] = None | DriverHistory: Optional[list[int]] = None | ||||
AssignedDriver: Optional[int] = None | |||||
AssignedDriver: Optional[ShowDriverNoVehicle] = None | |||||
Capacity: int | Capacity: int | ||||
Status: str | Status: str | ||||