from fastapi import APIRouter, Body, Request, status, HTTPException from typing import List import sys from ..models.users import User, UpdateUser from ..operations import users as users router = APIRouter(prefix="/user", tags=["User"]) def get_collection_users(request: Request): db = request.app.database["user_records"] return db @router.post("/", response_description="Create a new user", status_code=status.HTTP_201_CREATED, response_model=User) async def create_user(request: Request, user: User = Body(...)): collection = get_collection_users(request) return users.create_user(collection, user) @router.get("/", response_description="List users", response_model=List[User]) async def list_users(request: Request): collection = get_collection_users(request) return users.list_users(collection, 100) @router.put("/{user_id}", response_description="Update a User", response_model=UpdateUser) async def update_user(request: Request, user_id: str, user: UpdateUser = Body(...)): collection = get_collection_users(request) return users.update_user(collection, user_id, user) @router.get("/{user_id}", response_description="Get a single user by id", response_model=User) async def find_user(request: Request, user_id: str): collection = get_collection_users(request) return users.find_user(collection, user_id) @router.get("/find-name-id/{user_id}", response_description="Get a username from user id") async def find_name_from_id(request: Request, user_id: str): collection = get_collection_users(request) return users.find_name_from_id(collection, user_id) @router.get("/name/{user_name}", response_description="Get a single user by name", response_model=User) async def find_user_name(request: Request, name: str): collection = get_collection_users(request) return users.find_user_name(collection, name) @router.get("/email/{email_addr}", response_description="Get a single user by email", response_model=User) async def find_user_email(request: Request, email: str): collection = get_collection_users(request) return users.find_user_email(collection, email) @router.delete("/{user_id}", response_description="Delete a user") async def delete_user(request: Request, user_id:str): collection = get_collection_users(request) return users.delete_user(collection, user_id)