Add random endpoints

This commit is contained in:
2021-12-27 22:01:00 +03:00
parent 1117e36046
commit a7805fe468
5 changed files with 43 additions and 1 deletions

View File

@@ -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)