瀏覽代碼

trying to add vehicle loc

main
Madiwka 1 年之前
父節點
當前提交
32249a028d
共有 5 個文件被更改,包括 47 次插入3 次删除
  1. +3
    -0
      app/apis/v1/route_user.py
  2. +24
    -2
      app/apis/v1/route_vehicle.py
  3. +14
    -0
      app/db/repository/vehicle.py
  4. +3
    -1
      app/schemas/user.py
  5. +3
    -0
      app/schemas/vehicle.py

+ 3
- 0
app/apis/v1/route_user.py 查看文件

@@ -17,7 +17,10 @@ router = APIRouter()
def create_user(
user: UserCreate,
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")
# if current_user.Role != "Admin":
# raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
user = create_new_user(user=user, db=db)


+ 24
- 2
app/apis/v1/route_vehicle.py 查看文件

@@ -1,9 +1,9 @@
from fastapi import APIRouter, status, HTTPException
from sqlalchemy.orm import Session
from fastapi import Depends
from typing import List
from typing import Annotated, List
from db.session import get_db
from schemas.vehicle import OutputVehicle, CreateVehicle, UpdateVehicle
from schemas.vehicle import OutputVehicle, CreateVehicle, UpdateVehicle, VehicleLocation
from db.repository.vehicle import (
create_new_vehicle,
assign_vehicle_driver,
@@ -11,6 +11,7 @@ from db.repository.vehicle import (
get_vehicle_by_id,
replace_vehicle_data,
delete_vehicle_data,
update_vehicle_location,
)
from db.models.user import User
from apis.v1.route_auth import get_current_user
@@ -24,6 +25,7 @@ async def create_vehicle(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
):
print(current_user.Role)
if current_user.Role != "Admin":
raise HTTPException(
status_code=403, detail="You are not authorized to perform this action"
@@ -138,3 +140,23 @@ def delete_vehicle(
if result == "vehicleNotFound":
raise HTTPException(status_code=404, detail="Vehicle not found")
return {"msg": "Vehicle deleted successfully"}


@router.post("/{vehicle_id}/location", status_code=status.HTTP_200_OK)
async def update_vehicle_location(
vehicle_id: int,
location: VehicleLocation,
current_user: User = Depends(get_current_user),
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.AssignedVehicle != vehicle_id:
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)
if vehicle == "vehiclenotfound":
raise HTTPException(status_code=404, detail="Vehicle not found")
return vehicle

+ 14
- 0
app/db/repository/vehicle.py 查看文件

@@ -19,6 +19,18 @@ def create_new_vehicle(vehicle: CreateVehicle, db: Session):
return vehicle_object


def update_vehicle_location(vehicle_id: int, location: list[str], 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
db.add(vehicle_object)
db.commit()
db.refresh(vehicle_object)
return vehicle_object


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()
@@ -89,3 +101,5 @@ def delete_vehicle_data(id: int, db: Session):
db.delete(vehicle_object)
db.commit()
return vehicle_object



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

@@ -2,6 +2,7 @@

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


class UserCreate(BaseModel):
@@ -19,7 +20,7 @@ class UserCreate(BaseModel):
class ShowUser(BaseModel):
Id: int
Name: str
MiddleName: str
MiddleName: str | None
LastName: str
ContactNumber: str
BirthDate: datetime
@@ -28,3 +29,4 @@ class ShowUser(BaseModel):

class Config:
orm_mode = True
validate_assignment = True

+ 3
- 0
app/schemas/vehicle.py 查看文件

@@ -35,3 +35,6 @@ class UpdateVehicle(BaseModel):
Fuel: Optional[int] = 0
MaintenanceNotes: Optional[list[str]] = None
AssignedDriver: Optional[int] = None

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

Loading…
取消
儲存