@@ -9,6 +9,7 @@ from db.repository.drivetask import ( | |||||
get_all_tasks, | get_all_tasks, | ||||
get_task_by_id, | get_task_by_id, | ||||
get_tasks_by_driver, | get_tasks_by_driver, | ||||
edit_task | |||||
) | ) | ||||
from schemas.drivetask import CreateTask | from schemas.drivetask import CreateTask | ||||
from db.models.user import User | from db.models.user import User | ||||
@@ -118,3 +119,21 @@ def getTasksByDriver( | |||||
status_code=404, detail=f"Driver with id {driver_id} not found" | status_code=404, detail=f"Driver with id {driver_id} not found" | ||||
) | ) | ||||
return tasks | return tasks | ||||
@router.put("/{task_id}", status_code=status.HTTP_200_OK) | |||||
def updateTask( | |||||
task_id: int, | |||||
task: CreateTask, | |||||
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" | |||||
) | |||||
task = edit_task(task_id, task, db) | |||||
if task == "notaskfound": | |||||
raise HTTPException( | |||||
status_code=404, detail=f"Task with id {task_id} not found" | |||||
) | |||||
return task |
@@ -5,9 +5,9 @@ from fastapi import Depends | |||||
from typing import List, Annotated | from typing import List, Annotated | ||||
from apis.v1.route_auth import get_current_user | from apis.v1.route_auth import get_current_user | ||||
from db.models.user import User | from db.models.user import User | ||||
from schemas.user import UserCreate, ShowUser | |||||
from schemas.user import UserCreate, ShowUser, ShowDriver, DriverCreate | |||||
from db.session import get_db | from db.session import get_db | ||||
from db.repository.user import create_new_user, list_users, get_user_by_id, replace_user_data | |||||
from db.repository.user import create_new_user, list_users, get_user_by_id, replace_user_data, create_new_driver | |||||
router = APIRouter() | router = APIRouter() | ||||
@@ -26,6 +26,16 @@ def create_user( | |||||
user = create_new_user(user=user, db=db) | user = create_new_user(user=user, db=db) | ||||
return user | return user | ||||
@router.post("/driver", response_model=ShowDriver, status_code=status.HTTP_201_CREATED) | |||||
def create_driver( | |||||
driver: DriverCreate, | |||||
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") | |||||
driver = create_new_driver(driver=driver, db=db) | |||||
return driver | |||||
@router.get("/", response_model=List[ShowUser], status_code=status.HTTP_200_OK) | @router.get("/", response_model=List[ShowUser], status_code=status.HTTP_200_OK) | ||||
def get_all_users(db: Session = Depends(get_db), role: str = None): | def get_all_users(db: Session = Depends(get_db), role: str = None): | ||||
@@ -63,3 +73,13 @@ def get_user(user_id: int, db: Session = Depends(get_db)): | |||||
if not user: | if not user: | ||||
raise HTTPException(status_code=404, detail="User not found") | raise HTTPException(status_code=404, detail="User not found") | ||||
return user | return user | ||||
@router.get("/driver/{driver_id}", response_model=ShowDriver, status_code=status.HTTP_200_OK) | |||||
def get_driver(driver_id: int, db: Session = Depends(get_db)): | |||||
driver = get_user_by_id(user_id=driver_id, role = "Driver", db=db) | |||||
res = driver.__dict__ | |||||
res["AssignedVehicle"] = driver.vehicle | |||||
if not driver: | |||||
raise HTTPException(status_code=404, detail="Driver not found") | |||||
return driver |
@@ -64,3 +64,14 @@ def get_task_by_id(task_id: int, db: Session): | |||||
def get_all_tasks(db: Session): | def get_all_tasks(db: Session): | ||||
tasks = db.query(DriveTask).all() | tasks = db.query(DriveTask).all() | ||||
return tasks | return tasks | ||||
def edit_task(task_id: int, task: CreateTask, db: Session): | |||||
tasks = db.query(DriveTask).filter(DriveTask.Id == task_id).first() | |||||
if not tasks: | |||||
return "notaskfound" | |||||
tasks.Description = task.Description | |||||
tasks.StartLocation = task.StartLocation | |||||
tasks.EndLocation = task.EndLocation | |||||
db.commit() | |||||
db.refresh(tasks) | |||||
return tasks |
@@ -1,7 +1,7 @@ | |||||
# Creating a new user in the database | # Creating a new user in the database | ||||
from sqlalchemy.orm import Session | from sqlalchemy.orm import Session | ||||
from schemas.user import UserCreate | |||||
from schemas.user import UserCreate, DriverCreate | |||||
from db.models.user import User | from db.models.user import User | ||||
from core.hashing import Hasher | from core.hashing import Hasher | ||||
@@ -22,10 +22,28 @@ def create_new_user(user: UserCreate, db: Session): | |||||
db.commit() | db.commit() | ||||
db.refresh(user_object) | db.refresh(user_object) | ||||
return user_object | return user_object | ||||
def create_new_driver(driver: DriverCreate, db: Session): | |||||
driver_object = User( | |||||
Email=driver.Email, | |||||
Name=driver.Name, | |||||
MiddleName=driver.MiddleName, | |||||
LastName=driver.LastName, | |||||
GovernmentId=driver.GovernmentId, | |||||
Address=driver.Address, | |||||
ContactNumber=driver.ContactNumber, | |||||
Role="Driver", | |||||
HashedPassword=Hasher.get_password_hash(driver.Password), | |||||
DrivingLicenseNumber=driver.DrivingLicenseNumber | |||||
) | |||||
db.add(driver_object) | |||||
db.commit() | |||||
db.refresh(driver_object) | |||||
return driver_object | |||||
def get_user_by_id(user_id: int, db: Session): | |||||
def get_user_by_id(user_id: int, role: str, db: Session): | |||||
user = db.query(User).filter(User.Id == user_id).first() | user = db.query(User).filter(User.Id == user_id).first() | ||||
if user.Role != role: | |||||
return False | |||||
return user | return user | ||||
@@ -1,7 +1,7 @@ | |||||
# Purpose: User schema for pydantic (validation, inside-api usage) | # Purpose: User schema for pydantic (validation, inside-api usage) | ||||
from pydantic import BaseModel, EmailStr, Field | from pydantic import BaseModel, EmailStr, Field | ||||
from schemas.vehicle import OutputVehicle | |||||
class UserCreate(BaseModel): | class UserCreate(BaseModel): | ||||
Email: EmailStr | Email: EmailStr | ||||
@@ -13,8 +13,10 @@ class UserCreate(BaseModel): | |||||
GovernmentId: str = Field(...) | GovernmentId: str = Field(...) | ||||
Address: str = Field(...) | Address: str = Field(...) | ||||
Email: EmailStr = Field(...) | Email: EmailStr = Field(...) | ||||
Role: str = Field(..., min_length=3, max_length=50) | |||||
class DriverCreate(UserCreate): | |||||
DrivingLicenseNumber: str = Field(...) | |||||
class ShowUser(BaseModel): | class ShowUser(BaseModel): | ||||
Id: int | Id: int | ||||
@@ -26,9 +28,12 @@ class ShowUser(BaseModel): | |||||
Address: str | Address: str | ||||
Email: EmailStr | Email: EmailStr | ||||
Role: str | Role: str | ||||
AssignedVehicle: int | None | |||||
class Config: | class Config: | ||||
orm_mode = True | orm_mode = True | ||||
validate_assignment = True | validate_assignment = True | ||||
class ShowDriver(ShowUser): | |||||
DrivingLicenseNumber: str | None | |||||
AssignedVehicle: OutputVehicle | None |