@@ -17,7 +17,10 @@ router = APIRouter() | |||||
def create_user( | def create_user( | ||||
user: UserCreate, | user: UserCreate, | ||||
db: Session = Depends(get_db), | 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": | # if current_user.Role != "Admin": | ||||
# raise HTTPException(status_code=403, detail="You are not authorized to perform this action") | # raise HTTPException(status_code=403, detail="You are not authorized to perform this action") | ||||
user = create_new_user(user=user, db=db) | user = create_new_user(user=user, db=db) | ||||
@@ -1,9 +1,9 @@ | |||||
from fastapi import APIRouter, status, HTTPException | from fastapi import APIRouter, status, HTTPException | ||||
from sqlalchemy.orm import Session | from sqlalchemy.orm import Session | ||||
from fastapi import Depends | from fastapi import Depends | ||||
from typing import List | |||||
from typing import Annotated, List | |||||
from db.session import get_db | 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 ( | from db.repository.vehicle import ( | ||||
create_new_vehicle, | create_new_vehicle, | ||||
assign_vehicle_driver, | assign_vehicle_driver, | ||||
@@ -11,6 +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, | |||||
) | ) | ||||
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 | ||||
@@ -24,6 +25,7 @@ async def create_vehicle( | |||||
db: Session = Depends(get_db), | db: Session = Depends(get_db), | ||||
current_user: User = Depends(get_current_user), | current_user: User = Depends(get_current_user), | ||||
): | ): | ||||
print(current_user.Role) | |||||
if current_user.Role != "Admin": | if current_user.Role != "Admin": | ||||
raise HTTPException( | raise HTTPException( | ||||
status_code=403, detail="You are not authorized to perform this action" | status_code=403, detail="You are not authorized to perform this action" | ||||
@@ -138,3 +140,23 @@ def delete_vehicle( | |||||
if result == "vehicleNotFound": | if result == "vehicleNotFound": | ||||
raise HTTPException(status_code=404, detail="Vehicle not found") | raise HTTPException(status_code=404, detail="Vehicle not found") | ||||
return {"msg": "Vehicle deleted successfully"} | 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 |
@@ -19,6 +19,18 @@ 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): | |||||
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): | def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session): | ||||
driver = ( | driver = ( | ||||
db.query(User).filter((User.Id == driver_id) & (User.Role == "Driver")).first() | 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.delete(vehicle_object) | ||||
db.commit() | db.commit() | ||||
return vehicle_object | return vehicle_object | ||||
@@ -2,6 +2,7 @@ | |||||
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): | ||||
@@ -19,7 +20,7 @@ class UserCreate(BaseModel): | |||||
class ShowUser(BaseModel): | class ShowUser(BaseModel): | ||||
Id: int | Id: int | ||||
Name: str | Name: str | ||||
MiddleName: str | |||||
MiddleName: str | None | |||||
LastName: str | LastName: str | ||||
ContactNumber: str | ContactNumber: str | ||||
BirthDate: datetime | BirthDate: datetime | ||||
@@ -28,3 +29,4 @@ class ShowUser(BaseModel): | |||||
class Config: | class Config: | ||||
orm_mode = True | orm_mode = True | ||||
validate_assignment = True |
@@ -35,3 +35,6 @@ class UpdateVehicle(BaseModel): | |||||
Fuel: Optional[int] = 0 | Fuel: Optional[int] = 0 | ||||
MaintenanceNotes: Optional[list[str]] = None | MaintenanceNotes: Optional[list[str]] = None | ||||
AssignedDriver: Optional[int] = None | AssignedDriver: Optional[int] = None | ||||
class VehicleLocation(BaseModel): | |||||
CurrentLocation: list[str] |