Explorar el Código

Search functionality

main
Saruar hace 1 año
padre
commit
b1f0eeb1fe
Se han modificado 4 ficheros con 59 adiciones y 2 borrados
  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 Ver fichero


+ 31
- 2
app/apis/v1/route_user.py Ver fichero

@@ -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 Ver fichero

@@ -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 Ver fichero

@@ -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



Cargando…
Cancelar
Guardar