Browse Source

Added ability to get by id

main
Madiwka3 1 year ago
parent
commit
5ef2252930
4 changed files with 32 additions and 3 deletions
  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 View File

@@ -1,12 +1,12 @@
# Routes for user. MAIN PART OF THE API # 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 sqlalchemy.orm import Session
from fastapi import Depends from fastapi import Depends
from typing import List from typing import List


from schemas.user import UserCreate, ShowUser from schemas.user import UserCreate, ShowUser
from db.session import get_db 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() router = APIRouter()
@@ -25,3 +25,11 @@ def get_all_users(db: Session = Depends(get_db), role: str = None):
return users return users
users = list_users(db=db, role=role) users = list_users(db=db, role=role)
return users 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 View File

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


router = APIRouter() 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)): async def create_vehicle(vehicle: CreateVehicle, db: Session = Depends(get_db)):
vehicle = create_new_vehicle(vehicle=vehicle, db=db) vehicle = create_new_vehicle(vehicle=vehicle, db=db)
return vehicle 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)): async def get_all_vehicles(db: Session = Depends(get_db)):
vehicles = list_vehicles(db=db) vehicles = list_vehicles(db=db)
return vehicles 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 View File

@@ -23,6 +23,11 @@ def create_new_user(user: UserCreate, db: Session):
return user_object 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): def verify_driver_exists(driver_id: int, db: Session):
driver = db.query(User).filter(User.Id == driver_id).first() driver = db.query(User).filter(User.Id == driver_id).first()
if not driver: if not driver:


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

@@ -40,3 +40,8 @@ def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session):
def list_vehicles(db: Session): def list_vehicles(db: Session):
vehicles = db.query(Vehicle).all() vehicles = db.query(Vehicle).all()
return vehicles 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…
Cancel
Save