add filter on relation example

This commit is contained in:
Jakub Miazek 2022-08-17 14:02:37 +02:00
parent 3e65b7f9a2
commit 04ccd80ad4
5 changed files with 81 additions and 6 deletions

View File

@ -16,7 +16,7 @@ services:
volumes:
- .:/code
ports:
- 8080:8080
- "8080:8080"
depends_on:
- db

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

View File

@ -2,8 +2,7 @@ from fastapi import FastAPI
from the_app.api.nonsense import router as nonsense_router
from the_app.api.stuff import router as stuff_router
from the_app.database import engine
from the_app.models.base import Base
from the_app.api.shakespeare import router as shakespeare_router
from the_app.utils import get_logger
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(nonsense_router)
app.include_router(shakespeare_router)
@app.on_event("startup")

View File

@ -8,7 +8,9 @@ from sqlalchemy import (
Table,
Text,
UniqueConstraint,
select,
)
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import relationship
from the_app.models.base import Base
@ -120,6 +122,13 @@ class Paragraph(Base):
char_count = Column(Integer, nullable=False)
word_count = Column(Integer, nullable=False)
character = relationship("Character", back_populates="paragraph")
chapter = relationship("Chapter", back_populates="paragraph")
work = relationship("Work", back_populates="paragraph")
character = relationship("Character", back_populates="paragraph", lazy="selectin")
chapter = relationship("Chapter", back_populates="paragraph", lazy="selectin")
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

View 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