Преглед изворни кода

Added ability to change vehicle information

main
Madiwka3 пре 1 година
родитељ
комит
d8bfb523be
3 измењених фајлова са 49 додато и 2 уклоњено
  1. +16
    -1
      app/apis/v1/route_vehicle.py
  2. +21
    -1
      app/db/repository/vehicle.py
  3. +12
    -0
      app/schemas/vehicle.py

+ 16
- 1
app/apis/v1/route_vehicle.py Прегледај датотеку

@@ -3,12 +3,13 @@ from sqlalchemy.orm import Session
from fastapi import Depends
from typing import List
from db.session import get_db
from schemas.vehicle import OutputVehicle, CreateVehicle
from schemas.vehicle import OutputVehicle, CreateVehicle, UpdateVehicle
from db.repository.vehicle import (
create_new_vehicle,
assign_vehicle_driver,
list_vehicles,
get_vehicle_by_id,
replace_vehicle_data,
)

router = APIRouter()
@@ -78,3 +79,17 @@ async def get_vehicle(vehicle_id: int, db: Session = Depends(get_db)):
if not vehicle:
raise HTTPException(status_code=404, detail="Vehicle not found")
return vehicle


@router.put(
"/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
)
def update_vehicle(
vehicle_id: int, vehicle: UpdateVehicle, db: Session = Depends(get_db)
):
vehicleRes = replace_vehicle_data(id=vehicle_id, vehicle=vehicle, db=db)
if vehicleRes == "vehicleNotFound":
raise HTTPException(status_code=404, detail="Vehicle not found")
elif vehicleRes == "badreq":
raise HTTPException(status_code=502, detail="Bad request")
return vehicleRes

+ 21
- 1
app/db/repository/vehicle.py Прегледај датотеку

@@ -1,5 +1,5 @@
from sqlalchemy.orm import Session
from schemas.vehicle import CreateVehicle, OutputVehicle
from schemas.vehicle import CreateVehicle, OutputVehicle, UpdateVehicle
from db.models.vehicle import Vehicle
from db.repository.user import verify_driver_exists

@@ -45,3 +45,23 @@ def list_vehicles(db: Session):
def get_vehicle_by_id(vehicle_id: int, db: Session):
vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
return vehicle


def replace_vehicle_data(id: int, vehicle: UpdateVehicle, db: Session):
vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
vehicle_object = vehicle_db.first()
if not vehicle_object:
return "vehiclenotfound"
vehicle_object.AssignedDriverIds = vehicle.AssignedDriverIds
vehicle_object.CurrentLocation = vehicle.CurrentLocation
vehicle_object.Fuel = vehicle.Fuel
vehicle_object.LicensePlate = vehicle.LicensePlate
vehicle_object.MaintenanceNotes = vehicle.MaintenanceNotes
vehicle_object.Mileage = vehicle.Mileage
vehicle_object.Model = vehicle.Model
vehicle_object.Type = vehicle.Type
vehicle_object.Year = vehicle.Year
print(vehicle_object)
db.add(vehicle_object)
db.commit()
return vehicle_object

+ 12
- 0
app/schemas/vehicle.py Прегледај датотеку

@@ -23,3 +23,15 @@ class OutputVehicle(BaseModel):
Fuel: Optional[int] = 0
MaintenanceNotes: Optional[list[str]] = None
AssignedDriverIds: Optional[list[int]] = None


class UpdateVehicle(BaseModel):
Model: str
Year: int
LicensePlate: str
Type: str
Mileage: int
CurrentLocation: Optional[list[str]] = None
Fuel: Optional[int] = 0
MaintenanceNotes: Optional[list[str]] = None
AssignedDriverIds: Optional[list[int]] = None

Loading…
Откажи
Сачувај