浏览代码

Fixed vehicle location assignment

main
Madiwka3 1年前
父节点
当前提交
267e16a114
共有 4 个文件被更改,包括 21 次插入17 次删除
  1. +13
    -9
      app/apis/v1/route_vehicle.py
  2. +4
    -5
      app/db/repository/vehicle.py
  3. +2
    -2
      app/schemas/user.py
  4. +2
    -1
      app/schemas/vehicle.py

+ 13
- 9
app/apis/v1/route_vehicle.py 查看文件

@@ -11,7 +11,7 @@ from db.repository.vehicle import (
get_vehicle_by_id,
replace_vehicle_data,
delete_vehicle_data,
update_vehicle_location,
update_vehicle_geoloc,
)
from db.models.user import User
from apis.v1.route_auth import get_current_user
@@ -143,20 +143,24 @@ def delete_vehicle(


@router.post("/{vehicle_id}/location", status_code=status.HTTP_200_OK)
async def update_vehicle_location(
vehicle_id: int,
def update_vehicle_location(
vehicle_id: int,
location: VehicleLocation,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db),
db: Session = Depends(get_db),
):
print(current_user)
print(current_user.Name)
if current_user.Role != "Driver":
raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
if current_user.Role != "Driver":
raise HTTPException(
status_code=403, detail="You are not authorized to perform this action"
)
if current_user.AssignedVehicle != vehicle_id:
raise HTTPException(status_code=403, detail="You are not the correct car driver")
raise HTTPException(
status_code=403, detail="You are not the correct car driver"
)
print("FUNNY")
vehicle = update_vehicle_location(vehicle_id = vehicle_id, location=location, db=db)
vehicle = update_vehicle_geoloc(vehicle_id=vehicle_id, location=location, db=db)
if vehicle == "vehiclenotfound":
raise HTTPException(status_code=404, detail="Vehicle not found")
return vehicle
return vehicle

+ 4
- 5
app/db/repository/vehicle.py 查看文件

@@ -1,5 +1,5 @@
from sqlalchemy.orm import Session
from schemas.vehicle import CreateVehicle, UpdateVehicle
from schemas.vehicle import CreateVehicle, UpdateVehicle, VehicleLocation
from db.models.vehicle import Vehicle
from db.base import User
from db.repository.user import get_car_driver
@@ -19,12 +19,13 @@ def create_new_vehicle(vehicle: CreateVehicle, db: Session):
return vehicle_object


def update_vehicle_location(vehicle_id: int, location: list[str], db: Session):
def update_vehicle_geoloc(vehicle_id: int, location: VehicleLocation, db: Session):
vehicle_db = db.query(Vehicle).filter(Vehicle.Id == vehicle_id)
vehicle_object = vehicle_db.first()
if not vehicle_object:
return "vehiclenotfound"
vehicle_object.CurrentLocation = location
print("Location: " + str(location.CurrentLocation))
vehicle_object.CurrentLocation = location.CurrentLocation
db.add(vehicle_object)
db.commit()
db.refresh(vehicle_object)
@@ -101,5 +102,3 @@ def delete_vehicle_data(id: int, db: Session):
db.delete(vehicle_object)
db.commit()
return vehicle_object



+ 2
- 2
app/schemas/user.py 查看文件

@@ -2,14 +2,13 @@

from datetime import datetime
from pydantic import BaseModel, EmailStr, Field
from typing import Optional


class UserCreate(BaseModel):
Email: EmailStr
Password: str = Field(..., min_length=7, max_length=20)
Name: str = Field(..., min_length=3, max_length=50)
MiddleName: str = Field(None, min_length=3, max_length=50)
MiddleName: str = Field(None)
LastName: str = Field(..., min_length=3, max_length=50)
ContactNumber: str = Field(..., min_length=12, max_length=12)
BirthDate: datetime = Field(...)
@@ -26,6 +25,7 @@ class ShowUser(BaseModel):
BirthDate: datetime
Email: EmailStr
Role: str
AssignedVehicle: int | None

class Config:
orm_mode = True


+ 2
- 1
app/schemas/vehicle.py 查看文件

@@ -36,5 +36,6 @@ class UpdateVehicle(BaseModel):
MaintenanceNotes: Optional[list[str]] = None
AssignedDriver: Optional[int] = None


class VehicleLocation(BaseModel):
CurrentLocation: list[str]
CurrentLocation: list[str]

正在加载...
取消
保存