Browse Source

Fixed vehicle location assignment

main
Madiwka3 1 year ago
parent
commit
267e16a114
4 changed files with 21 additions and 17 deletions
  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 View File

@@ -11,7 +11,7 @@ from db.repository.vehicle import (
get_vehicle_by_id, get_vehicle_by_id,
replace_vehicle_data, replace_vehicle_data,
delete_vehicle_data, delete_vehicle_data,
update_vehicle_location,
update_vehicle_geoloc,
) )
from db.models.user import User from db.models.user import User
from apis.v1.route_auth import get_current_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) @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, location: VehicleLocation,
current_user: User = Depends(get_current_user), current_user: User = Depends(get_current_user),
db: Session = Depends(get_db),
db: Session = Depends(get_db),
): ):
print(current_user) print(current_user)
print(current_user.Name) 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: 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") 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": if vehicle == "vehiclenotfound":
raise HTTPException(status_code=404, detail="Vehicle not found") raise HTTPException(status_code=404, detail="Vehicle not found")
return vehicle
return vehicle

+ 4
- 5
app/db/repository/vehicle.py View File

@@ -1,5 +1,5 @@
from sqlalchemy.orm import Session 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.models.vehicle import Vehicle
from db.base import User from db.base import User
from db.repository.user import get_car_driver from db.repository.user import get_car_driver
@@ -19,12 +19,13 @@ def create_new_vehicle(vehicle: CreateVehicle, db: Session):
return vehicle_object 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_db = db.query(Vehicle).filter(Vehicle.Id == vehicle_id)
vehicle_object = vehicle_db.first() vehicle_object = vehicle_db.first()
if not vehicle_object: if not vehicle_object:
return "vehiclenotfound" return "vehiclenotfound"
vehicle_object.CurrentLocation = location
print("Location: " + str(location.CurrentLocation))
vehicle_object.CurrentLocation = location.CurrentLocation
db.add(vehicle_object) db.add(vehicle_object)
db.commit() db.commit()
db.refresh(vehicle_object) db.refresh(vehicle_object)
@@ -101,5 +102,3 @@ def delete_vehicle_data(id: int, db: Session):
db.delete(vehicle_object) db.delete(vehicle_object)
db.commit() db.commit()
return vehicle_object return vehicle_object



+ 2
- 2
app/schemas/user.py View File

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


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




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


class Config: class Config:
orm_mode = True orm_mode = True


+ 2
- 1
app/schemas/vehicle.py View File

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



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

Loading…
Cancel
Save