Skip to content

Commit

Permalink
api: implement get users endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
anarute committed Feb 12, 2024
1 parent 536aa94 commit 38d395d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 7 additions & 0 deletions api/routers/v1/users.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import List
from fastapi import APIRouter, Depends, status
from sqlalchemy.orm import Session

from db.db_connection import get_db
from auth.auth_bearer import BearerToken
from dependencies import get_current_user
from services.user import UserService
from schemas.user import AppUser

router = APIRouter(
Expand All @@ -20,3 +22,8 @@
@router.get("/me", response_model=AppUser)
async def get_current_user_profile(current_user=Depends(get_current_user), db: Session = Depends(get_db)):
return current_user


@router.get("/", response_model=List[AppUser])
async def get_users(current_user=Depends(get_current_user), db: Session = Depends(get_db), active: bool = True):
return UserService(db).get_users(active=active)
9 changes: 6 additions & 3 deletions api/services/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ def __init__(self, user_raw_data):


class UserService(AppService):
def get_users(self) -> List[User]:
users = self.db.query(User).all() or []
return users
def get_users(self, active) -> List[User]:
query = self.db.query(User)
if active:
query = query.filter(User.is_active)
users = query.all() or []
return [UserProfile(user) for user in users]

def get_user(self, username: str) -> AppUser:
user_in_db = self.db.query(User).filter(User.login == username).first() or None
Expand Down

0 comments on commit 38d395d

Please sign in to comment.