|
@@ -1,12 +1,14 @@ |
|
|
# Routes for user. MAIN PART OF THE API |
|
|
# 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.orm import Session |
|
|
|
|
|
from sqlalchemy import or_ |
|
|
from fastapi import Depends |
|
|
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 core.config import settings |
|
|
from core.config import settings |
|
|
from db.models.user import User |
|
|
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.session import get_db |
|
|
from db.repository.user import ( |
|
|
from db.repository.user import ( |
|
|
create_new_user, |
|
|
create_new_user, |
|
@@ -15,9 +17,11 @@ from db.repository.user import ( |
|
|
replace_user_data, |
|
|
replace_user_data, |
|
|
create_new_driver, |
|
|
create_new_driver, |
|
|
delete_user_data, |
|
|
delete_user_data, |
|
|
|
|
|
get_users_by_name |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter() |
|
|
router = APIRouter() |
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -126,3 +130,28 @@ def delete_user( |
|
|
if result == "userNotFound": |
|
|
if result == "userNotFound": |
|
|
raise HTTPException(status_code=404, detail="User not found") |
|
|
raise HTTPException(status_code=404, detail="User not found") |
|
|
return result |
|
|
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} |