mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
format
This commit is contained in:
parent
aa1b9ec867
commit
ee1d241d23
@ -9,9 +9,7 @@ router = APIRouter(prefix="/v1/nonsense")
|
|||||||
|
|
||||||
|
|
||||||
@router.post("/", status_code=status.HTTP_201_CREATED, response_model=NonsenseResponse)
|
@router.post("/", status_code=status.HTTP_201_CREATED, response_model=NonsenseResponse)
|
||||||
async def create_nonsense(
|
async def create_nonsense(payload: NonsenseSchema, db_session: AsyncSession = Depends(get_db)):
|
||||||
payload: NonsenseSchema, db_session: AsyncSession = Depends(get_db)
|
|
||||||
):
|
|
||||||
nonsense = Nonsense(**payload.dict())
|
nonsense = Nonsense(**payload.dict())
|
||||||
await nonsense.save(db_session)
|
await nonsense.save(db_session)
|
||||||
return nonsense
|
return nonsense
|
||||||
@ -40,3 +38,13 @@ async def update_nonsense(
|
|||||||
nonsense = await Nonsense.find(db_session, name)
|
nonsense = await Nonsense.find(db_session, name)
|
||||||
await nonsense.update(db_session, **payload.dict())
|
await nonsense.update(db_session, **payload.dict())
|
||||||
return nonsense
|
return nonsense
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/", response_model=NonsenseResponse)
|
||||||
|
async def merge_nonsense(
|
||||||
|
payload: NonsenseSchema,
|
||||||
|
db_session: AsyncSession = Depends(get_db),
|
||||||
|
):
|
||||||
|
nonsense = Nonsense(**payload.dict())
|
||||||
|
await nonsense.save_or_update(db_session)
|
||||||
|
return nonsense
|
||||||
|
@ -19,4 +19,5 @@ class Settings(BaseSettings):
|
|||||||
def get_settings():
|
def get_settings():
|
||||||
return Settings()
|
return Settings()
|
||||||
|
|
||||||
|
|
||||||
settings = get_settings()
|
settings = get_settings()
|
||||||
|
@ -38,9 +38,7 @@ class Base:
|
|||||||
db_session.add(self)
|
db_session.add(self)
|
||||||
return await db_session.commit()
|
return await db_session.commit()
|
||||||
except SQLAlchemyError as ex:
|
except SQLAlchemyError as ex:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) from ex
|
||||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)
|
|
||||||
) from ex
|
|
||||||
|
|
||||||
async def delete(self, db_session: AsyncSession):
|
async def delete(self, db_session: AsyncSession):
|
||||||
"""
|
"""
|
||||||
@ -53,9 +51,7 @@ class Base:
|
|||||||
await db_session.commit()
|
await db_session.commit()
|
||||||
return True
|
return True
|
||||||
except SQLAlchemyError as ex:
|
except SQLAlchemyError as ex:
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) from ex
|
||||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)
|
|
||||||
) from ex
|
|
||||||
|
|
||||||
async def update(self, db_session: AsyncSession, **kwargs):
|
async def update(self, db_session: AsyncSession, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -29,9 +29,7 @@ class Nonsense(Base):
|
|||||||
if instance is None:
|
if instance is None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
detail={
|
detail={"Record not found": f"There is no record for requested name value : {name}"},
|
||||||
"Record not found": f"There is no record for requested name value : {name}"
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return instance
|
return instance
|
||||||
|
@ -31,9 +31,7 @@ class Character(Base):
|
|||||||
abbrev = Column(String(32))
|
abbrev = Column(String(32))
|
||||||
description = Column(String(2056))
|
description = Column(String(2056))
|
||||||
|
|
||||||
work = relationship(
|
work = relationship("Work", secondary="shakespeare.character_work", back_populates="character")
|
||||||
"Work", secondary="shakespeare.character_work", back_populates="character"
|
|
||||||
)
|
|
||||||
paragraph = relationship("Paragraph", back_populates="character")
|
paragraph = relationship("Paragraph", back_populates="character")
|
||||||
|
|
||||||
|
|
||||||
@ -68,9 +66,7 @@ class Work(Base):
|
|||||||
total_paragraphs = Column(Integer, nullable=False)
|
total_paragraphs = Column(Integer, nullable=False)
|
||||||
notes = Column(Text)
|
notes = Column(Text)
|
||||||
|
|
||||||
character = relationship(
|
character = relationship("Character", secondary="shakespeare.character_work", back_populates="work")
|
||||||
"Character", secondary="shakespeare.character_work", back_populates="work"
|
|
||||||
)
|
|
||||||
chapter = relationship("Chapter", back_populates="work")
|
chapter = relationship("Chapter", back_populates="work")
|
||||||
paragraph = relationship("Paragraph", back_populates="work")
|
paragraph = relationship("Paragraph", back_populates="work")
|
||||||
|
|
||||||
@ -78,9 +74,7 @@ class Work(Base):
|
|||||||
class Chapter(Base):
|
class Chapter(Base):
|
||||||
__tablename__ = "chapter"
|
__tablename__ = "chapter"
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
ForeignKeyConstraint(
|
ForeignKeyConstraint(["work_id"], ["shakespeare.work.id"], name="chapter_work_id_fkey"),
|
||||||
["work_id"], ["shakespeare.work.id"], name="chapter_work_id_fkey"
|
|
||||||
),
|
|
||||||
PrimaryKeyConstraint("id", name="chapter_pkey"),
|
PrimaryKeyConstraint("id", name="chapter_pkey"),
|
||||||
UniqueConstraint(
|
UniqueConstraint(
|
||||||
"work_id",
|
"work_id",
|
||||||
@ -111,9 +105,7 @@ t_character_work = Table(
|
|||||||
["shakespeare.character.id"],
|
["shakespeare.character.id"],
|
||||||
name="character_work_character_id_fkey",
|
name="character_work_character_id_fkey",
|
||||||
),
|
),
|
||||||
ForeignKeyConstraint(
|
ForeignKeyConstraint(["work_id"], ["shakespeare.work.id"], name="character_work_work_id_fkey"),
|
||||||
["work_id"], ["shakespeare.work.id"], name="character_work_work_id_fkey"
|
|
||||||
),
|
|
||||||
PrimaryKeyConstraint("character_id", "work_id", name="character_work_pkey"),
|
PrimaryKeyConstraint("character_id", "work_id", name="character_work_pkey"),
|
||||||
schema="shakespeare",
|
schema="shakespeare",
|
||||||
)
|
)
|
||||||
@ -136,9 +128,7 @@ class Paragraph(Base):
|
|||||||
],
|
],
|
||||||
name="paragraph_chapter_fkey",
|
name="paragraph_chapter_fkey",
|
||||||
),
|
),
|
||||||
ForeignKeyConstraint(
|
ForeignKeyConstraint(["work_id"], ["shakespeare.work.id"], name="paragraph_work_id_fkey"),
|
||||||
["work_id"], ["shakespeare.work.id"], name="paragraph_work_id_fkey"
|
|
||||||
),
|
|
||||||
PrimaryKeyConstraint("id", name="paragraph_pkey"),
|
PrimaryKeyConstraint("id", name="paragraph_pkey"),
|
||||||
{"schema": "shakespeare"},
|
{"schema": "shakespeare"},
|
||||||
)
|
)
|
||||||
@ -162,13 +152,7 @@ class Paragraph(Base):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def find(cls, db_session: AsyncSession, character: str):
|
async def find(cls, db_session: AsyncSession, character: str):
|
||||||
stmt = (
|
stmt = select(cls).join(Character).join(Chapter).join(Work).where(Character.name == character)
|
||||||
select(cls)
|
|
||||||
.join(Character)
|
|
||||||
.join(Chapter)
|
|
||||||
.join(Work)
|
|
||||||
.where(Character.name == character)
|
|
||||||
)
|
|
||||||
result = await db_session.execute(stmt)
|
result = await db_session.execute(stmt)
|
||||||
instance = result.scalars().all()
|
instance = result.scalars().all()
|
||||||
return instance
|
return instance
|
||||||
|
Loading…
x
Reference in New Issue
Block a user