Skip to content

Commit

Permalink
Merge pull request #91 from arayabrain/feature/user-search-api
Browse files Browse the repository at this point in the history
Feature/user search api
  • Loading branch information
itutu-tienday authored Aug 1, 2023
2 parents 2adcc94 + 8d527d4 commit bab6ee0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions studio/__main_unit__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
run,
users_admin,
users_me,
users_search,
workspace,
)
from studio.app.dir_path import DIRPATH
Expand All @@ -36,6 +37,7 @@
app.include_router(run.router)
app.include_router(users_admin.router)
app.include_router(users_me.router)
app.include_router(users_search.router)
app.include_router(workspace.router)

# optinist routers
Expand Down
44 changes: 44 additions & 0 deletions studio/app/common/routers/users_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import List

from fastapi import APIRouter, Depends, Query
from sqlmodel import Session, or_

from studio.app.common.core.auth.auth_dependencies import get_current_user
from studio.app.common.db.database import get_db
from studio.app.common.models import User as UserModel
from studio.app.common.schemas.users import User, UserInfo

router = APIRouter(prefix="/users/search", tags=["users/search"])


@router.get(
"/share_users",
response_model=List[UserInfo],
description="""
- Get a list of users with whom content is shared.
- Note: Maximum of 10 responses. (security considerations)
""",
)
def search_share_users(
keyword: str = Query(description="partial match (user.name or user.email)"),
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
):
MAX_RESPONSE_COUNT = 10
users = (
db.query(UserModel)
.filter(
UserModel.active.is_(True),
UserModel.organization_id == current_user.organization_id,
)
.filter(
or_(
UserModel.name.like("%{0}%".format(keyword)),
UserModel.email.like("%{0}%".format(keyword)),
)
)
.order_by(UserModel.id)
.limit(MAX_RESPONSE_COUNT)
.all()
)
return users

0 comments on commit bab6ee0

Please sign in to comment.