fix: 🐛 fixed driver and vehicle apis, swapped dbms to postgres and refactored project structure
This commit is contained in:
46
modules/vehicleHandler.py
Normal file
46
modules/vehicleHandler.py
Normal file
@ -0,0 +1,46 @@
|
||||
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 = self.dbSession.query(Vehicle).filter_by(id=vehicleID).first()
|
||||
|
||||
return {
|
||||
"id": vehicle.id,
|
||||
"name": vehicle.name
|
||||
}
|
||||
|
||||
# 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
|
Reference in New Issue
Block a user