Skip to content

Commit

Permalink
Add list tags API
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKs committed Mar 21, 2022
1 parent 55f8de0 commit a395642
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/api/api_v2/api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from fastapi import APIRouter

from app.api.api_v2.endpoints import delete, list_videos, patch_video, upload
from app.api.api_v2.endpoints import delete, list_videos, patch_video, tags, upload

api_router = APIRouter()
api_router.include_router(list_videos.router, tags=['files'])
api_router.include_router(upload.router, tags=['files'])
api_router.include_router(delete.router, tags=['files'])
api_router.include_router(patch_video.router, tags=['files'])
api_router.include_router(tags.router, tags=['tags'])
40 changes: 40 additions & 0 deletions app/api/api_v2/endpoints/tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import asyncio

from fastapi import APIRouter, Depends, Query
from sqlalchemy import desc, func
from sqlmodel import select
from sqlmodel.ext.asyncio.session import AsyncSession

from app.api.dependencies import yield_db_session
from app.api.security import cognito_scheme_or_anonymous
from app.models.klepp import ListResponse, Tag, TagRead

router = APIRouter()


@router.get('/tags', response_model=ListResponse[TagRead], dependencies=[Depends(cognito_scheme_or_anonymous)])
async def get_all_tags(
session: AsyncSession = Depends(yield_db_session),
offset: int = 0,
limit: int = Query(default=100, lte=100),
) -> dict[str, int | list]:
"""
Get a list of all non-hidden files, unless you're the owner of the file, then you can request
hidden files.
Works both as anonymous user and as a signed in user.
"""
# Video query
tag_statement = select(Tag).order_by(desc(Tag.name))
# Total count query based on query params, without pagination
count_statement = select(func.count('*')).select_from(tag_statement) # type: ignore

# Add pagination
tag_statement = tag_statement.offset(offset=offset).limit(limit=limit)
# Do DB requests async
tasks = [
asyncio.create_task(session.exec(tag_statement)), # type: ignore
asyncio.create_task(session.exec(count_statement)),
]
results, count = await asyncio.gather(*tasks)
count_number = count.first()
return {'total_count': count_number, 'response': results.all()}

0 comments on commit a395642

Please sign in to comment.