Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
__pycache__/
*.py[cod]
*$py.class
database.db
database.py
.idea

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing table creation: The task requires creating database tables using SQLAlchemy models (checklist item #17), but Base.metadata.create_all(bind=engine) is never called. Add this line to create the tables when the app starts.


# C extensions
*.so
Expand Down
41 changes: 41 additions & 0 deletions crud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from sqlalchemy import select
from models import Author, Book
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing file: models.py is not provided but is required. The task specification explicitly requires this file for CHECKLIST ITEMS #11-#26. Line 2 of crud.py imports from this file: from models import Author, Book

from schemas import AuthorCreate, BookCreate


def get_author(db, author_id: int):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing file: schemas.py is not provided but is required. The task specification explicitly requires this file (CHECKLIST ITEM #14). Line 6 of main.py imports from this file: from schemas import AuthorRead, AuthorCreate, BookCreate, BookRead

result = db.execute(select(Author).where(Author.id == author_id))
author = result.scalars().first()
return author

def get_authors(db, skip: int = 0, limit: int = 10):
result = db.execute(select(Author).offset(skip).limit(limit))
authors = result.scalars().all()
return authors

def create_author(db, author: AuthorCreate):
db_author = Author(name=author.name, bio=author.bio)
db.add(db_author)
db.commit()
db.refresh(db_author)
return db_author

def get_books(db, skip: int = 0, limit: int = 10, author_id: int = None):
query = select(Book)
if author_id:
query = query.where(Book.author_id == author_id)
result = db.execute(query.offset(skip).limit(limit))
books = result.scalars().all()
return books

def create_book(db, book: BookCreate):
db_book = Book(
title=book.title,
summary=book.summary,
publication_date=book.publication_date,
author_id=book.author_id
)
db.add(db_book)
db.commit()
Comment on lines +37 to +39
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing feature: This endpoint does not support filtering books by author ID as required by CHECKLIST ITEM #6. The function should accept an optional author_id: int = None query parameter and pass it to crud.get_books().

db.refresh(db_book)
return db_book
14 changes: 14 additions & 0 deletions database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import DeclarativeBase, sessionmaker

SQLALCHEMY_DATABASE_URL = "sqlite:///database.db"

engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing table creation. The task requirement #17 explicitly states to 'Create database tables using the SQLAlchemy models'. This line should be added to initialize the database on app startup.

)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


class Base(DeclarativeBase):
pass
46 changes: 46 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from typing import List
from fastapi import FastAPI, Depends
from sqlalchemy.orm import Session
import crud
from database import SessionLocal, Base, engine
from schemas import AuthorRead, AuthorCreate, BookCreate, BookRead

Base.metadata.create_all(bind=engine)

app = FastAPI()

def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()

@app.get("/")
def root() -> dict:
return {"message": "Hello World"}

@app.post("/authors/", response_model=AuthorRead)
def create_author(author: AuthorCreate, db: Session = Depends(get_db)):
return crud.create_author(db=db, author=author)

@app.get("/authors/", response_model=List[AuthorRead])
def read_authors(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
return crud.get_authors(db, skip=skip, limit=limit)

@app.get("/authors/{author_id}", response_model=AuthorRead)
def read_author(author_id: int, db: Session = Depends(get_db)):
return crud.get_author(db, author_id=author_id)

@app.post("/books/", response_model=BookRead)
def create_book(book: BookCreate, db: Session = Depends(get_db)):
return crud.create_book(db, book=book)

@app.get("/books/", response_model=List[BookRead])
def read_books(
skip: int = 0,
limit: int = 10,
db: Session = Depends(get_db),
author_id: int = None,
):
return crud.get_books(db, skip=skip, limit=limit, author_id=author_id)
25 changes: 25 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import datetime
from typing import List
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from database import Base


class Author(Base):
__tablename__ = "authors"

id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(unique=True)
bio: Mapped[str] = mapped_column()
books: Mapped[List["Book"]] = relationship(back_populates="author")


class Book(Base):
__tablename__ = "books"

id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column()
summary: Mapped[str] = mapped_column()
publication_date: Mapped[datetime.date] = mapped_column()
author_id: Mapped[int] = mapped_column(ForeignKey("authors.id"))
author: Mapped[Author] = relationship(back_populates="books")
16 changes: 16 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
annotated-doc==0.0.4
annotated-types==0.7.0
anyio==4.13.0
click==8.3.3
colorama==0.4.6
fastapi==0.136.1
greenlet==3.5.0
h11==0.16.0
idna==3.15
pydantic==2.13.4
pydantic_core==2.46.4
SQLAlchemy==2.0.49
starlette==1.0.0
typing-inspection==0.4.2
typing_extensions==4.15.0
uvicorn==0.47.0
36 changes: 36 additions & 0 deletions schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import datetime
from typing import List
from pydantic import BaseModel


class AuthorCreate(BaseModel):
name: str
bio: str
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing table creation: The task requires creating database tables using SQLAlchemy models (checklist item #17). Add Base.metadata.create_all(bind=engine) to create the tables when the application starts.



class BookRead(BaseModel):
id: int
title: str
summary: str
publication_date: datetime.date
author_id: int

class Config:
from_attributes = True


class AuthorRead(BaseModel):
id: int
name: str
bio: str
books: List[BookRead]

class Config:
from_attributes = True


class BookCreate(BaseModel):
title: str
summary: str
publication_date: datetime.date
author_id: int