소스 검색

Search functionality

main
Saruar 1 년 전
부모
커밋
b1f0eeb1fe
4개의 변경된 파일59개의 추가작업 그리고 2개의 파일을 삭제
  1. BIN
      app/.DS_Store
  2. +31
    -2
      app/apis/v1/route_user.py
  3. +9
    -0
      app/db/repository/user.py
  4. +19
    -0
      app/schemas/user.py

BIN
app/.DS_Store 파일 보기


+ 31
- 2
app/apis/v1/route_user.py 파일 보기

@@ -1,12 +1,14 @@
# Routes for user. MAIN PART OF THE API
from fastapi import APIRouter, HTTPException, status
from math import ceil
from fastapi import APIRouter, HTTPException, status, Query
from sqlalchemy.orm import Session
from sqlalchemy import or_
from fastapi import Depends
from typing import List, Annotated
from apis.v1.route_auth import get_current_user
from core.config import settings
from db.models.user import User
from schemas.user import UserCreate, ShowUser, ShowDriver, DriverCreate
from schemas.user import UserCreate, ShowUser, ShowDriver, DriverCreate, OutputUser, UsersPage
from db.session import get_db
from db.repository.user import (
create_new_user,
@@ -15,9 +17,11 @@ from db.repository.user import (
replace_user_data,
create_new_driver,
delete_user_data,
get_users_by_name
)



router = APIRouter()


@@ -126,3 +130,28 @@ def delete_user(
if result == "userNotFound":
raise HTTPException(status_code=404, detail="User not found")
return result


@router.get("/search/", response_model=UsersPage)
def search_users(db: Session = Depends(get_db), name: str = None, role: str = None, page: int = 1, per_page: int = 20):
query = db.query(User).filter(User.Name.like(f"{name}%"))
if role is not None and role != "Admin":
query = query.filter(User.Role == role)
total_users = query.count()
total_pages = ceil(total_users / per_page)
users = query.offset((page - 1) * per_page).limit(per_page).all()

output_users = [OutputUser(
id= user.Id,
Name=user.Name,
MiddleName = user.MiddleName,
LastName=user.LastName,
ContactNumber=user.ContactNumber,
Address=user.Address,
Email=user.Email,
Role=user.Role,
AssignedVehicle= None
) for user in users]


return {"users": output_users, "total_pages": total_pages}

+ 9
- 0
app/db/repository/user.py 파일 보기

@@ -83,6 +83,15 @@ def list_users(db: Session, role: str = "Any"):
users = db.query(User).filter((User.Role == role) | (role == "Any")).all()
return users

def get_users_by_name(db: Session, name: str = "", role: str = None, page: int = 1, per_page: int = 20):
if role == "Admin":
return None
if role is None:
users = db.query(User).filter(User.Name.like(f"{name}%"), User.Role != "Admin")
else:
users = db.query(User).filter(User.Name.like(f"{name}%"), User.Role == role)
users = users.offset((page - 1) * per_page).limit(per_page).all()
return users

def replace_user_data(user_id: int, user_data: UserCreate, db: Session):
user = db.query(User).filter(User.Id == user_id).first()


+ 19
- 0
app/schemas/user.py 파일 보기

@@ -1,6 +1,7 @@
# Purpose: User schema for pydantic (validation, inside-api usage)

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


class UserCreate(BaseModel):
@@ -36,6 +37,24 @@ class ShowUser(BaseModel):
validate_assignment = True


class OutputUser(BaseModel):
id: int
Name: str
MiddleName: Optional[str]
LastName: str
ContactNumber: str
Address: str
Email: EmailStr
Role: str
AssignedVehicle: Optional[str] # replace str with the actual type of AssignedVehicle

class Config:
orm_mode = True

class UsersPage(BaseModel):
users: List[OutputUser]
total_pages: int

class ShowDriverNoVehicle(ShowUser):
DrivingLicenseNumber: str | None



불러오는 중...
취소
저장