mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 15:15:36 +01:00
Add genres api
This commit is contained in:
@@ -7,7 +7,7 @@ RUN apt-get update \
|
|||||||
WORKDIR /root/poetry
|
WORKDIR /root/poetry
|
||||||
COPY pyproject.toml poetry.lock /root/poetry/
|
COPY pyproject.toml poetry.lock /root/poetry/
|
||||||
|
|
||||||
RUN pip install poetry --no-cache-dir \
|
RUN pip install poetry wheel --no-cache-dir \
|
||||||
&& poetry export --without-hashes > requirements.txt
|
&& poetry export --without-hashes > requirements.txt
|
||||||
|
|
||||||
ENV VENV_PATH=/opt/venv
|
ENV VENV_PATH=/opt/venv
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ class Source(ormar.Model):
|
|||||||
tablename = "sources"
|
tablename = "sources"
|
||||||
|
|
||||||
id: int = ormar.SmallInteger(primary_key=True, nullable=False) # type: ignore
|
id: int = ormar.SmallInteger(primary_key=True, nullable=False) # type: ignore
|
||||||
|
|
||||||
name: str = ormar.String(max_length=32, nullable=False, unique=True) # type: ignore
|
name: str = ormar.String(max_length=32, nullable=False, unique=True) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
42
fastapi_book_server/app/serializers/genre.py
Normal file
42
fastapi_book_server/app/serializers/genre.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
from pydantic import BaseModel, constr
|
||||||
|
|
||||||
|
from app.serializers.orjson_config import ORJSONConfig
|
||||||
|
|
||||||
|
|
||||||
|
class GenreSource(BaseModel):
|
||||||
|
id: int
|
||||||
|
name: str
|
||||||
|
|
||||||
|
|
||||||
|
class Genre(BaseModel):
|
||||||
|
id: int
|
||||||
|
source: GenreSource
|
||||||
|
remote_id: int
|
||||||
|
code: str
|
||||||
|
description: str
|
||||||
|
meta: str
|
||||||
|
|
||||||
|
class Config(ORJSONConfig):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class CreateGenre(BaseModel):
|
||||||
|
source: int
|
||||||
|
remote_id: int
|
||||||
|
code: constr(max_length=45) # type: ignore
|
||||||
|
description: constr(max_length=99) # type: ignore
|
||||||
|
meta: constr(max_length=45) # type: ignore
|
||||||
|
|
||||||
|
class Config(ORJSONConfig):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class UpdateGenre(BaseModel):
|
||||||
|
source: int
|
||||||
|
remote_id: int
|
||||||
|
code: constr(max_length=45) # type: ignore
|
||||||
|
description: constr(max_length=99) # type: ignore
|
||||||
|
meta: constr(max_length=45) # type: ignore
|
||||||
|
|
||||||
|
class Config(ORJSONConfig):
|
||||||
|
pass
|
||||||
@@ -2,6 +2,7 @@ from app.views.author import author_router, translator_router
|
|||||||
from app.views.author_annotation import author_annotation_router
|
from app.views.author_annotation import author_annotation_router
|
||||||
from app.views.book import book_router
|
from app.views.book import book_router
|
||||||
from app.views.book_annotation import book_annotation_router
|
from app.views.book_annotation import book_annotation_router
|
||||||
|
from app.views.genre import genre_router
|
||||||
from app.views.healthcheck import healtcheck_router
|
from app.views.healthcheck import healtcheck_router
|
||||||
from app.views.sequence import sequence_router
|
from app.views.sequence import sequence_router
|
||||||
from app.views.source import source_router
|
from app.views.source import source_router
|
||||||
@@ -17,5 +18,6 @@ routers = [
|
|||||||
book_annotation_router,
|
book_annotation_router,
|
||||||
translation_router,
|
translation_router,
|
||||||
sequence_router,
|
sequence_router,
|
||||||
|
genre_router,
|
||||||
healtcheck_router,
|
healtcheck_router,
|
||||||
]
|
]
|
||||||
|
|||||||
54
fastapi_book_server/app/views/genre.py
Normal file
54
fastapi_book_server/app/views/genre.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
|
|
||||||
|
from fastapi_pagination import Params, Page
|
||||||
|
from fastapi_pagination.ext.ormar import paginate
|
||||||
|
|
||||||
|
from app.depends import check_token
|
||||||
|
from app.models import Genre as GenreDB
|
||||||
|
from app.serializers.genre import Genre, CreateGenre, UpdateGenre
|
||||||
|
|
||||||
|
|
||||||
|
genre_router = APIRouter(
|
||||||
|
prefix="/api/v1/genres", tags=["genres"], dependencies=[Depends(check_token)]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
PREFETCH_RELATED_FIELDS = ["source"]
|
||||||
|
|
||||||
|
|
||||||
|
@genre_router.get("/", response_model=Page[Genre], dependencies=[Depends(Params)])
|
||||||
|
async def get_genres():
|
||||||
|
return await paginate(GenreDB.objects.prefetch_related(PREFETCH_RELATED_FIELDS))
|
||||||
|
|
||||||
|
|
||||||
|
@genre_router.get("/{id}", response_model=Genre)
|
||||||
|
async def get_genre(id: int):
|
||||||
|
genre = await GenreDB.objects.prefetch_related(PREFETCH_RELATED_FIELDS).get_or_none(
|
||||||
|
id=id
|
||||||
|
)
|
||||||
|
|
||||||
|
if genre is None:
|
||||||
|
raise HTTPException(status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
return genre
|
||||||
|
|
||||||
|
|
||||||
|
@genre_router.post("/", response_model=Genre)
|
||||||
|
async def create_genre(data: CreateGenre):
|
||||||
|
return await GenreDB.objects.prefetch_related(PREFETCH_RELATED_FIELDS).create(
|
||||||
|
**data.dict()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@genre_router.put("/{id}", response_model=Genre)
|
||||||
|
async def update_genre(id: int, data: UpdateGenre):
|
||||||
|
genre = await GenreDB.objects.prefetch_related(PREFETCH_RELATED_FIELDS).get_or_none(
|
||||||
|
id=id
|
||||||
|
)
|
||||||
|
|
||||||
|
if genre is None:
|
||||||
|
raise HTTPException(status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
genre.update_from_dict(data.dict())
|
||||||
|
|
||||||
|
return await genre.save()
|
||||||
Reference in New Issue
Block a user