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

@@ -20,6 +20,11 @@ def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
op.create_index(op.f('ix_books_title'), 'books', ['title'], unique=False) op.create_index(op.f('ix_books_title'), 'books', ['title'], unique=False)
op.create_index(op.f('ix_sequences_name'), 'sequences', ['name'], unique=False) op.create_index(op.f('ix_sequences_name'), 'sequences', ['name'], unique=False)
op.create_index(op.f('tgrm_books_title'), 'books', ['title'], postgresql_using='gin', postgresql_ops={'description': 'gin_trgm_ops'})
op.create_index(op.f('tgrm_sequences_name'), 'sequences', ['name'], postgresql_using='gin', postgresql_ops={'description': 'gin_trgm_ops'})
op.create_index(op.f('tgrm_authors_lfm'), 'authors', [sa.text("(last_name || ' ' || first_name || ' ' || middle_name)")] ,postgresql_using='gin', postgresql_ops={'description': 'gin_trgm_ops'})
op.create_index(op.f('tgrm_authors_lf'), 'authors', [sa.text("(last_name || ' ' || first_name)")] ,postgresql_using='gin', postgresql_ops={'description': 'gin_trgm_ops'})
op.create_index(op.f('tgrm_authors_l'), 'authors', ['last_name'] ,postgresql_using='gin', postgresql_ops={'description': 'gin_trgm_ops'})
# ### end Alembic commands ### # ### end Alembic commands ###
@@ -27,4 +32,9 @@ def downgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_sequences_name'), table_name='sequences') op.drop_index(op.f('ix_sequences_name'), table_name='sequences')
op.drop_index(op.f('ix_books_title'), table_name='books') op.drop_index(op.f('ix_books_title'), table_name='books')
op.drop_index(op.f('tgrm_books_title'), table_name='books')
op.drop_index(op.f('tgrm_sequences_name'), table_name='books')
op.drop_index(op.f('tgrm_authors_lfm'), table_name='books')
op.drop_index(op.f('tgrm_authors_lf'), table_name='books')
op.drop_index(op.f('tgrm_authors_l'), table_name='books')
# ### end Alembic commands ### # ### end Alembic commands ###

View File

@@ -19,7 +19,7 @@ class TRGMSearchService(Generic[T]):
SELECT_RELATED: Optional[Union[list[str], str]] = None SELECT_RELATED: Optional[Union[list[str], str]] = None
PREFETCH_RELATED: Optional[Union[list[str], str]] = None PREFETCH_RELATED: Optional[Union[list[str], str]] = None
GET_OBJECT_IDS_QUERY: Optional[str] = None GET_OBJECT_IDS_QUERY: Optional[str] = None
CACHE_TTL = 5 * 60 CACHE_TTL = 60 * 60
@classmethod @classmethod
def get_params(cls) -> AbstractParams: def get_params(cls) -> AbstractParams:

View File

@@ -1,3 +1,5 @@
from random import choice as random_choice
from fastapi import APIRouter, Depends, Request, HTTPException, status from fastapi import APIRouter, Depends, Request, HTTPException, status
from fastapi_pagination import Params 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) 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) @author_router.get("/{id}", response_model=Author)
async def get_author(id: int): async def get_author(id: int):
author = await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get_or_none(id=id) author = await AuthorDB.objects.prefetch_related(PREFETCH_RELATED).get_or_none(id=id)

View File

@@ -1,4 +1,5 @@
from typing import Union from typing import Union
from random import choice as random_choice
from fastapi import APIRouter, Depends, Request, HTTPException, status 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) 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) @book_router.get("/{id}", response_model=BookDetail)
async def get_book(id: int): async def get_book(id: int):
book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none(id=id) book = await BookDB.objects.select_related(SELECT_RELATED_FIELDS).get_or_none(id=id)

View File

@@ -1,3 +1,5 @@
from random import choice as random_choice
from fastapi import APIRouter, Depends, Request from fastapi import APIRouter, Depends, Request
from fastapi_pagination import Params 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) @sequence_router.get("/{id}", response_model=Sequence)
async def get_sequence(id: int): async def get_sequence(id: int):
return await SequenceDB.objects.get(id=id) return await SequenceDB.objects.get(id=id)