mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
add filter on relation example
This commit is contained in:
parent
3e65b7f9a2
commit
04ccd80ad4
@ -16,7 +16,7 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
- .:/code
|
- .:/code
|
||||||
ports:
|
ports:
|
||||||
- 8080:8080
|
- "8080:8080"
|
||||||
depends_on:
|
depends_on:
|
||||||
- db
|
- db
|
||||||
|
|
||||||
|
15
the_app/api/shakespeare.py
Normal file
15
the_app/api/shakespeare.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
from fastapi import APIRouter, Depends, status
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from the_app.database import get_db
|
||||||
|
from the_app.models.shakespeare import Paragraph
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/v1/shakespeare")
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/",)
|
||||||
|
async def find_paragraph(
|
||||||
|
character: str,
|
||||||
|
db_session: AsyncSession = Depends(get_db),
|
||||||
|
):
|
||||||
|
return await Paragraph.find(db_session=db_session, character=character)
|
@ -2,8 +2,7 @@ from fastapi import FastAPI
|
|||||||
|
|
||||||
from the_app.api.nonsense import router as nonsense_router
|
from the_app.api.nonsense import router as nonsense_router
|
||||||
from the_app.api.stuff import router as stuff_router
|
from the_app.api.stuff import router as stuff_router
|
||||||
from the_app.database import engine
|
from the_app.api.shakespeare import router as shakespeare_router
|
||||||
from the_app.models.base import Base
|
|
||||||
from the_app.utils import get_logger
|
from the_app.utils import get_logger
|
||||||
|
|
||||||
logger = get_logger(__name__)
|
logger = get_logger(__name__)
|
||||||
@ -12,6 +11,7 @@ app = FastAPI(title="Stuff And Nonsense API", version="0.4")
|
|||||||
|
|
||||||
app.include_router(stuff_router)
|
app.include_router(stuff_router)
|
||||||
app.include_router(nonsense_router)
|
app.include_router(nonsense_router)
|
||||||
|
app.include_router(shakespeare_router)
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
|
@ -8,7 +8,9 @@ from sqlalchemy import (
|
|||||||
Table,
|
Table,
|
||||||
Text,
|
Text,
|
||||||
UniqueConstraint,
|
UniqueConstraint,
|
||||||
|
select,
|
||||||
)
|
)
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
|
|
||||||
from the_app.models.base import Base
|
from the_app.models.base import Base
|
||||||
@ -120,6 +122,13 @@ class Paragraph(Base):
|
|||||||
char_count = Column(Integer, nullable=False)
|
char_count = Column(Integer, nullable=False)
|
||||||
word_count = Column(Integer, nullable=False)
|
word_count = Column(Integer, nullable=False)
|
||||||
|
|
||||||
character = relationship("Character", back_populates="paragraph")
|
character = relationship("Character", back_populates="paragraph", lazy="selectin")
|
||||||
chapter = relationship("Chapter", back_populates="paragraph")
|
chapter = relationship("Chapter", back_populates="paragraph", lazy="selectin")
|
||||||
work = relationship("Work", back_populates="paragraph")
|
work = relationship("Work", back_populates="paragraph", lazy="selectin")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def find(cls, db_session: AsyncSession, character: str):
|
||||||
|
stmt = select(cls).join(Character).join(Chapter).join(Work).where(Character.name == character)
|
||||||
|
result = await db_session.execute(stmt)
|
||||||
|
instance = result.scalars().all()
|
||||||
|
return instance
|
||||||
|
51
the_app/schemas/shakespeare.py
Normal file
51
the_app/schemas/shakespeare.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any, Optional
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class Character(BaseModel):
|
||||||
|
id: str
|
||||||
|
abbrev: str
|
||||||
|
speech_count: int
|
||||||
|
name: str
|
||||||
|
description: Any
|
||||||
|
|
||||||
|
|
||||||
|
class Chapter(BaseModel):
|
||||||
|
work_id: str
|
||||||
|
section_number: int
|
||||||
|
description: str
|
||||||
|
id: int
|
||||||
|
chapter_number: int
|
||||||
|
|
||||||
|
|
||||||
|
class Work(BaseModel):
|
||||||
|
id: str
|
||||||
|
year: int
|
||||||
|
source: str
|
||||||
|
total_paragraphs: int
|
||||||
|
title: str
|
||||||
|
long_title: str
|
||||||
|
genre_type: str
|
||||||
|
total_words: int
|
||||||
|
notes: Any
|
||||||
|
|
||||||
|
|
||||||
|
class Paragraph(BaseModel):
|
||||||
|
id: int
|
||||||
|
character_id: str
|
||||||
|
phonetic_text: str
|
||||||
|
paragraph_type: str
|
||||||
|
section_number: int
|
||||||
|
char_count: int
|
||||||
|
work_id: str
|
||||||
|
paragraph_num: int
|
||||||
|
plain_text: str
|
||||||
|
stem_text: str
|
||||||
|
chapter_number: int
|
||||||
|
word_count: int
|
||||||
|
character: Character
|
||||||
|
chapter: Chapter
|
||||||
|
work: Work
|
Loading…
x
Reference in New Issue
Block a user