mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2026-03-03 15:10:51 +01:00
Add random endpoints
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from random import choice as random_choice
|
||||
|
||||
from fastapi import APIRouter, Depends, Request, HTTPException, status
|
||||
|
||||
from fastapi_pagination import Params
|
||||
@@ -38,6 +40,15 @@ async def create_author(data: CreateAuthor):
|
||||
return await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get(id=author.id)
|
||||
|
||||
|
||||
@author_router.get("/random", response_model=Author)
|
||||
async def get_random_author():
|
||||
author_ids: list[int] = await AuthorDB.objects.values_list("id", flatten=True)
|
||||
|
||||
author_id = random_choice(author_ids)
|
||||
|
||||
return await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get(id=author_id)
|
||||
|
||||
|
||||
@author_router.get("/{id}", response_model=Author)
|
||||
async def get_author(id: int):
|
||||
author = await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get_or_none(id=id)
|
||||
|
||||
Reference in New Issue
Block a user