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)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from typing import Union
|
||||
from random import choice as random_choice
|
||||
|
||||
from fastapi import APIRouter, Depends, Request, HTTPException, status
|
||||
|
||||
@@ -38,6 +39,15 @@ async def create_book(data: Union[CreateBook, CreateRemoteBook]):
|
||||
return await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get(id=book.id)
|
||||
|
||||
|
||||
@book_router.get("/random", response_model=BookDetail)
|
||||
async def get_random_book():
|
||||
book_ids: list[int] = await BookDB.objects.filter(is_deleted=False).values_list("id", flatten=True)
|
||||
|
||||
book_id = random_choice(book_ids)
|
||||
|
||||
return await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get(id=book_id)
|
||||
|
||||
|
||||
@book_router.get("/{id}", response_model=BookDetail)
|
||||
async def get_book(id: int):
|
||||
book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none(id=id)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from random import choice as random_choice
|
||||
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
|
||||
from fastapi_pagination import Params
|
||||
@@ -24,6 +26,15 @@ async def get_sequences():
|
||||
)
|
||||
|
||||
|
||||
@sequence_router.get("/random", response_model=Sequence)
|
||||
async def get_random_sequence():
|
||||
sequence_ids: list[int] = await SequenceDB.objects.values_list("id", flatten=True)
|
||||
|
||||
sequence_id = random_choice(sequence_ids)
|
||||
|
||||
return await SequenceDB.objects.get(id=sequence_id)
|
||||
|
||||
|
||||
@sequence_router.get("/{id}", response_model=Sequence)
|
||||
async def get_sequence(id: int):
|
||||
return await SequenceDB.objects.get(id=id)
|
||||
|
||||
Reference in New Issue
Block a user