44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
from sqlalchemy.orm import Session
|
|
|
|
from modules.geoObjects import Vehicle
|
|
|
|
class VehicleHandler:
|
|
dbSession: Session
|
|
|
|
def __init__(self, session:Session):
|
|
self.dbSession = session
|
|
pass
|
|
|
|
# handles creating a vehicle and storing it in the database
|
|
def createVehicle(self, name:str) -> Vehicle:
|
|
if not name:
|
|
raise ValueError("name is empty")
|
|
|
|
vehicle = Vehicle(name=name)
|
|
|
|
self.dbSession.add(vehicle)
|
|
self.dbSession.commit()
|
|
|
|
return vehicle
|
|
|
|
# handles getting a vehicle identified with its id from the database
|
|
def getVehicle(self, vehicleID:int) -> Vehicle:
|
|
vehicle = self.dbSession.query(Vehicle).filter_by(id=vehicleID).first()
|
|
|
|
return vehicle
|
|
|
|
# handles getting all vehicles from database
|
|
def getVehicles(self):
|
|
vehicles = self.dbSession.query(Vehicle).all()
|
|
|
|
driverList = [
|
|
{
|
|
"id": vehicle.id,
|
|
"name": vehicle.name
|
|
}
|
|
# iterates all drivers and appends them to the list
|
|
for vehicle in vehicles
|
|
]
|
|
|
|
return driverList
|