瀏覽代碼

Added ability to get by id

main
Madiwka3 1 年之前
父節點
當前提交
5ef2252930
共有 4 個文件被更改,包括 32 次插入3 次删除
  1. +10
    -2
      app/apis/v1/route_user.py
  2. +12
    -1
      app/apis/v1/route_vehicle.py
  3. +5
    -0
      app/db/repository/user.py
  4. +5
    -0
      app/db/repository/vehicle.py

+ 10
- 2
app/apis/v1/route_user.py 查看文件

@@ -1,12 +1,12 @@
# Routes for user. MAIN PART OF THE API
from fastapi import APIRouter, status
from fastapi import APIRouter, HTTPException, status
from sqlalchemy.orm import Session
from fastapi import Depends
from typing import List

from schemas.user import UserCreate, ShowUser
from db.session import get_db
from db.repository.user import create_new_user, list_users
from db.repository.user import create_new_user, list_users, get_user_by_id


router = APIRouter()
@@ -25,3 +25,11 @@ def get_all_users(db: Session = Depends(get_db), role: str = None):
return users
users = list_users(db=db, role=role)
return users


@router.get("/{user_id}", response_model=ShowUser, status_code=status.HTTP_200_OK)
def get_user(user_id: int, db: Session = Depends(get_db)):
user = get_user_by_id(user_id=user_id, db=db)
if not user:
raise HTTPException(status_code=404, detail="User not found")
return user

+ 12
- 1
app/apis/v1/route_vehicle.py 查看文件

@@ -8,12 +8,13 @@ from db.repository.vehicle import (
create_new_vehicle,
assign_vehicle_driver,
list_vehicles,
get_vehicle_by_id,
)

router = APIRouter()


@router.post("/new", response_model=OutputVehicle, status_code=status.HTTP_201_CREATED)
@router.post("/", response_model=OutputVehicle, status_code=status.HTTP_201_CREATED)
async def create_vehicle(vehicle: CreateVehicle, db: Session = Depends(get_db)):
vehicle = create_new_vehicle(vehicle=vehicle, db=db)
return vehicle
@@ -67,3 +68,13 @@ async def assign_driver(vehicle_id: int, driver_id: int, db: Session = Depends(g
async def get_all_vehicles(db: Session = Depends(get_db)):
vehicles = list_vehicles(db=db)
return vehicles


@router.get(
"/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
)
async def get_vehicle(vehicle_id: int, db: Session = Depends(get_db)):
vehicle = get_vehicle_by_id(vehicle_id=vehicle_id, db=db)
if not vehicle:
raise HTTPException(status_code=404, detail="Vehicle not found")
return vehicle

+ 5
- 0
app/db/repository/user.py 查看文件

@@ -23,6 +23,11 @@ def create_new_user(user: UserCreate, db: Session):
return user_object


def get_user_by_id(user_id: int, db: Session):
user = db.query(User).filter(User.Id == user_id).first()
return user


def verify_driver_exists(driver_id: int, db: Session):
driver = db.query(User).filter(User.Id == driver_id).first()
if not driver:


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

@@ -40,3 +40,8 @@ def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session):
def list_vehicles(db: Session):
vehicles = db.query(Vehicle).all()
return vehicles


def get_vehicle_by_id(vehicle_id: int, db: Session):
vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
return vehicle

Loading…
取消
儲存