From e77fbe95c19878c2d5dbe1cdf0ca0bb262df5762 Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Sat, 30 Apr 2022 15:34:54 +0200 Subject: [PATCH] add multi stuff POST endpoint --- the_app/api/stuff.py | 26 +++++---- the_app/models/shakespeare.py | 105 ++++++++++++++++++---------------- 2 files changed, 72 insertions(+), 59 deletions(-) diff --git a/the_app/api/stuff.py b/the_app/api/stuff.py index a79de45..e1f57a7 100644 --- a/the_app/api/stuff.py +++ b/the_app/api/stuff.py @@ -1,25 +1,31 @@ from typing import List -from fastapi import APIRouter, Depends, status + +from fastapi import APIRouter, Depends, HTTPException, status +from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.ext.asyncio import AsyncSession from the_app.database import get_db from the_app.models.stuff import Stuff from the_app.schemas.stuff import StuffResponse, StuffSchema +from the_app.utils import get_logger router = APIRouter(prefix="/v1/stuff") +logger = get_logger(__name__) + @router.post("/add_many", status_code=status.HTTP_201_CREATED) async def create_multi_stuff(payload: List[StuffSchema], db_session: AsyncSession = Depends(get_db)): - stuff_instances = [ - Stuff( - name=stuf.name, description=stuf.description - ) - for stuf in payload - ] - db_session.add_all(stuff_instances) - await db_session.commit() - return True + try: + stuff_instances = [Stuff(name=stuf.name, description=stuf.description) for stuf in payload] + db_session.add_all(stuff_instances) + await db_session.commit() + except SQLAlchemyError as ex: + # logger.exception(ex) + raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) + else: + logger.info(f"{len(stuff_instances)} instances of Stuff inserted into database.") + return True @router.post("", status_code=status.HTTP_201_CREATED, response_model=StuffResponse) diff --git a/the_app/models/shakespeare.py b/the_app/models/shakespeare.py index 804c4bb..e554f36 100644 --- a/the_app/models/shakespeare.py +++ b/the_app/models/shakespeare.py @@ -1,18 +1,24 @@ -from sqlalchemy import (Column, ForeignKeyConstraint, Integer, - PrimaryKeyConstraint, String, Table, Text, - UniqueConstraint, ForeignKey) +from sqlalchemy import ( + Column, + ForeignKey, + ForeignKeyConstraint, + Integer, + PrimaryKeyConstraint, + String, + Table, + Text, + UniqueConstraint, +) from sqlalchemy.orm import relationship from the_app.models.base import Base + metadata = Base.metadata class Character(Base): - __tablename__ = 'character' - __table_args__ = ( - PrimaryKeyConstraint('id', name='character_pkey'), - {'schema': 'shakespeare'} - ) + __tablename__ = "character" + __table_args__ = (PrimaryKeyConstraint("id", name="character_pkey"), {"schema": "shakespeare"}) id = Column(String(32)) name = Column(String(64), nullable=False) @@ -20,16 +26,13 @@ class Character(Base): abbrev = Column(String(32)) description = Column(String(2056)) - work = relationship('Work', secondary='shakespeare.character_work', back_populates='character') - paragraph = relationship('Paragraph', back_populates='character') + work = relationship("Work", secondary="shakespeare.character_work", back_populates="character") + paragraph = relationship("Paragraph", back_populates="character") class Wordform(Base): - __tablename__ = 'wordform' - __table_args__ = ( - PrimaryKeyConstraint('id', name='wordform_pkey'), - {'schema': 'shakespeare'} - ) + __tablename__ = "wordform" + __table_args__ = (PrimaryKeyConstraint("id", name="wordform_pkey"), {"schema": "shakespeare"}) id = Column(Integer) plain_text = Column(String(64), nullable=False) @@ -39,11 +42,8 @@ class Wordform(Base): class Work(Base): - __tablename__ = 'work' - __table_args__ = ( - PrimaryKeyConstraint('id', name='work_pkey'), - {'schema': 'shakespeare'} - ) + __tablename__ = "work" + __table_args__ = (PrimaryKeyConstraint("id", name="work_pkey"), {"schema": "shakespeare"}) id = Column(String(32)) title = Column(String(32), nullable=False) @@ -55,55 +55,62 @@ class Work(Base): total_paragraphs = Column(Integer, nullable=False) notes = Column(Text) - character = relationship('Character', secondary='shakespeare.character_work', back_populates='work') - chapter = relationship('Chapter', back_populates='work') - paragraph = relationship('Paragraph', back_populates='work') + character = relationship("Character", secondary="shakespeare.character_work", back_populates="work") + chapter = relationship("Chapter", back_populates="work") + paragraph = relationship("Paragraph", back_populates="work") class Chapter(Base): - __tablename__ = 'chapter' + __tablename__ = "chapter" __table_args__ = ( - ForeignKeyConstraint(['work_id'], ['shakespeare.work.id'], name='chapter_work_id_fkey'), - PrimaryKeyConstraint('id', name='chapter_pkey'), - UniqueConstraint('work_id', 'section_number', 'chapter_number', name='chapter_work_id_section_number_chapter_number_key'), - {'schema': 'shakespeare'} + ForeignKeyConstraint(["work_id"], ["shakespeare.work.id"], name="chapter_work_id_fkey"), + PrimaryKeyConstraint("id", name="chapter_pkey"), + UniqueConstraint( + "work_id", "section_number", "chapter_number", name="chapter_work_id_section_number_chapter_number_key" + ), + {"schema": "shakespeare"}, ) id = Column(Integer) - work_id = Column(ForeignKey('shakespeare.work.id'), nullable=False) + work_id = Column(ForeignKey("shakespeare.work.id"), nullable=False) section_number = Column(Integer, nullable=False) chapter_number = Column(Integer, nullable=False) description = Column(String(256), nullable=False) - work = relationship('Work', back_populates='chapter') - paragraph = relationship('Paragraph', back_populates='chapter') + work = relationship("Work", back_populates="chapter") + paragraph = relationship("Paragraph", back_populates="chapter") t_character_work = Table( - 'character_work', metadata, - Column('character_id', ForeignKey('shakespeare.character.id'), nullable=False), - Column('work_id', ForeignKey('shakespeare.work.id'), nullable=False), - ForeignKeyConstraint(['character_id'], ['shakespeare.character.id'], name='character_work_character_id_fkey'), - ForeignKeyConstraint(['work_id'], ['shakespeare.work.id'], name='character_work_work_id_fkey'), - PrimaryKeyConstraint('character_id', 'work_id', name='character_work_pkey'), - schema='shakespeare' + "character_work", + metadata, + Column("character_id", ForeignKey("shakespeare.character.id"), nullable=False), + Column("work_id", ForeignKey("shakespeare.work.id"), nullable=False), + ForeignKeyConstraint(["character_id"], ["shakespeare.character.id"], name="character_work_character_id_fkey"), + ForeignKeyConstraint(["work_id"], ["shakespeare.work.id"], name="character_work_work_id_fkey"), + PrimaryKeyConstraint("character_id", "work_id", name="character_work_pkey"), + schema="shakespeare", ) class Paragraph(Base): - __tablename__ = 'paragraph' + __tablename__ = "paragraph" __table_args__ = ( - ForeignKeyConstraint(['character_id'], ['shakespeare.character.id'], name='paragraph_character_id_fkey'), - ForeignKeyConstraint(['work_id', 'section_number', 'chapter_number'], ['shakespeare.chapter.work_id', 'shakespeare.chapter.section_number', 'shakespeare.chapter.chapter_number'], name='paragraph_chapter_fkey'), - ForeignKeyConstraint(['work_id'], ['shakespeare.work.id'], name='paragraph_work_id_fkey'), - PrimaryKeyConstraint('id', name='paragraph_pkey'), - {'schema': 'shakespeare'} + ForeignKeyConstraint(["character_id"], ["shakespeare.character.id"], name="paragraph_character_id_fkey"), + ForeignKeyConstraint( + ["work_id", "section_number", "chapter_number"], + ["shakespeare.chapter.work_id", "shakespeare.chapter.section_number", "shakespeare.chapter.chapter_number"], + name="paragraph_chapter_fkey", + ), + ForeignKeyConstraint(["work_id"], ["shakespeare.work.id"], name="paragraph_work_id_fkey"), + PrimaryKeyConstraint("id", name="paragraph_pkey"), + {"schema": "shakespeare"}, ) id = Column(Integer) - work_id = Column(ForeignKey('shakespeare.work.id'), nullable=False) + work_id = Column(ForeignKey("shakespeare.work.id"), nullable=False) paragraph_num = Column(Integer, nullable=False) - character_id = Column(ForeignKey('shakespeare.character.id'), nullable=False) + character_id = Column(ForeignKey("shakespeare.character.id"), nullable=False) plain_text = Column(Text, nullable=False) phonetic_text = Column(Text, nullable=False) stem_text = Column(Text, nullable=False) @@ -113,6 +120,6 @@ 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") + chapter = relationship("Chapter", back_populates="paragraph") + work = relationship("Work", back_populates="paragraph")