Skip to content

1#641

Open
Maksym-Dazidov wants to merge 3 commits into
mate-academy:mainfrom
Maksym-Dazidov:develop
Open

1#641
Maksym-Dazidov wants to merge 3 commits into
mate-academy:mainfrom
Maksym-Dazidov:develop

Conversation

@Maksym-Dazidov
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

The provided response is not valid JSON and contains markdown formatting, comments, and instructions instead of a proper JSON object. It must be fixed to match the expected schema with 'decision' as either 'APPROVE' or 'REQUEST_CHANGES' and a 'reason' string.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment thread crud.py Outdated
Comment on lines +1 to +4
from sqlalchemy.orm import Session

import schemas
from db import models
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The import statement from db import models indicates the models are located in a db/ subdirectory. The requirements specified creating models.py at the project root level. Ensure this structure matches your intended deployment.

Comment thread database.py
@@ -0,0 +1,11 @@
from sqlalchemy import create_engine
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The main.py file is missing the database table creation on startup. The task requires creating database tables using SQLAlchemy models, but there's no Base.metadata.create_all(bind=engine) call anywhere. Add this at the end of main.py to initialize the tables when the app starts.

Comment thread models.py
@@ -0,0 +1,24 @@
from datetime import date
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The task requirements explicitly state that main.py must 'Create the database tables using the SQLAlchemy models'. This critical initialization step is missing. Add from db.database import engine, Base and Base.metadata.create_all(bind=engine) after the app initialization to create the tables when the application starts.

Comment thread main.py
def get_books(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
return crud.get_books(db, skip=skip, limit=limit)

@app.get("/books/by-author/{author_id}", response_model=list[schemas.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.

The skip and limit parameters on line 43 don't have default values (unlike lines 17 and 39 which use skip: int = 0, limit: int = 10). This makes them required parameters, but users would expect optional pagination like other endpoints. Add default values: skip: int = 0, limit: int = 10.

Comment thread main.py
Comment on lines +43 to +44
@app.get("/books/by-author/{author_id}", response_model=list[schemas.Book])
def get_books_by_author(author_id: int, skip: int, limit: int, db: Session = Depends(get_db)):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The endpoint retrieves books by author but doesn't verify the author exists first. Unlike /authors/{author_id} (line 27-28) which returns 404 for non-existent authors, this endpoint silently returns an empty list. Consider adding an author existence check for consistency and clearer API behavior.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

This implementation has two critical blockers that prevent the API from functioning correctly. First, the database tables are never created — you must add Base.metadata.create_all(bind=engine) in main.py before the app starts, as this is explicitly required by the task checklist item #8. Without this, the SQLite database will have no tables and all queries will fail. Second, the get_books_by_author endpoint in main.py assigns the result to a variable but never returns it, causing the endpoint to return null instead of the book list. Add return books at the end of that function. Additionally, the BookBase schema incorrectly includes author_id as a request body field — since books are created through /authors/{author_id}/books, the author_id should come from the path parameter, not the request body. Remove author_id from BookBase and keep it only in the Book response schema for serialization. These issues must be fixed before the API can work properly.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment thread alembic/env.py
Comment on lines +14 to +19
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)

# add your model's MetaData object here
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The author_id field should not be in BookBase and BookCreate schemas. Since books are created through /authors/{author_id}/books, the author_id is determined by the path parameter, not the request body. Remove author_id from these schemas.

Comment thread alembic/env.py
Comment on lines +22 to +26
# target_metadata = mymodel.Base.metadata
target_metadata = [Base.metadata]

# other values from the config, defined by the needs of env.py,
# can be acquired:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The Book response schema doesn't need author_id since it already includes the full author relationship. Including both is redundant. Consider removing author_id from this response schema.

Comment thread alembic/env.py
Comment on lines +1 to +15
from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context

from models import Base

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
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 implementation: The task requires creating database tables using SQLAlchemy models in main.py (requirement #8). Add Base.metadata.create_all(bind=engine) to create tables on startup.

Comment thread alembic/env.py
Comment on lines +42 to +44
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

For consistency with the create endpoint (which checks if author exists), consider verifying the author exists before querying their books. If the author doesn't exist, return 404.


"""
from typing import Sequence, Union

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The database tables are never created. Need to call Base.metadata.create_all(bind=engine) to initialize the database schema on application startup.

Comment thread main.py
Comment on lines +43 to +44
@app.get("/books/by-author/{author_id}", response_model=list[schemas.Book])
def get_books_by_author(author_id: int, skip: int, limit: int, db: Session = Depends(get_db)):
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 author validation: Unlike create_book and get_author endpoints which verify the author exists first, this endpoint should also check if the author exists and return 404 if not found. Consider using crud.get_author(db, author_id=author_id) to verify before querying books.

Comment thread schemas.py
Comment on lines +1 to +15
from datetime import date

from pydantic import BaseModel


class AuthorBase(BaseModel):
name: str
bio: str


class AuthorCreate(AuthorBase):
pass


class Author(AuthorBase):
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 requirement: The task specifies creating database tables on startup. Add from database import engine, Base and call Base.metadata.create_all(bind=engine) before defining routes.

Comment thread schemas.py
class BookBase(BaseModel):
title: str
summary: str
publication_date: date
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Schema design issue: author_id should be removed from BookBase. Since books are created via /authors/{author_id}/books, the author_id comes from the path, not the request body. This makes BookCreate require an unnecessary author_id field.

Comment thread schemas.py
class BookCreate(BookBase):
pass


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Redundant field: The Book response schema inherits author_id from BookBase but also has author: Author. Including both is redundant. Consider creating separate schemas: BookCreate without author_id, BookResponse with author relationship only.

Comment thread models.py
Comment on lines +1 to +7
from datetime import date

from sqlalchemy import ForeignKey, Date, String
from sqlalchemy.orm import Mapped, mapped_column, relationship

from database import Base

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The task requires creating database tables using SQLAlchemy models (requirement #8). Import Base and engine from database.py, then add Base.metadata.create_all(bind=engine) before the app = FastAPI() line to create tables on startup.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

Your implementation is approved as an educational override. You've done excellent work fixing the two critical blockers from previous reviews - database tables are now properly created at startup with Base.metadata.create_all(bind=engine), and the get_books_by_author endpoint correctly returns results. All core requirements are implemented and functional. One schema design issue remains: BookBase (in schemas.py line 23) includes author_id: int as a request body field, but since books are created via /authors/{author_id}/books, the author_id comes from the path parameter. This means clients would need to provide author_id twice (in URL and body), which is redundant. Remove author_id from BookBase and keep it only in the Book response schema for proper serialization. This is a Medium-priority usability issue rather than a blocking bug. Your core functionality works correctly, and fixing this schema design will make your API cleaner and more intuitive. Keep up the great progress!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Comment thread alembic/env.py
Comment on lines +15 to +20
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)

# add your model's MetaData object here
# for 'autogenerate' support
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

BookBase includes author_id as a required request body field, but since books are created via /authors/{author_id}/books, the author_id should come from the path parameter, not the request body. Remove author_id from BookBase and keep it only in the Book response schema for serialization.

Comment thread crud.py
Comment on lines +11 to +14
)
db.add(author)
db.commit()
db.refresh(author)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The BookBase schema includes author_id: int as a request body field, but since books are created through /authors/{author_id}/books endpoint, the author_id should come from the path parameter, not the request body. Remove author_id from BookBase (keep it only in the response schema) so clients don't need to send author_id in the request body.

Comment thread models.py
Comment on lines +19 to +24
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(255))
summary: Mapped[str] = mapped_column(String(255))
publication_date: Mapped[date] = mapped_column(Date, nullable=False)
author_id: Mapped[int] = mapped_column(ForeignKey("author.id"), nullable=False)
author: Mapped[Author] = relationship(back_populates="books")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

BookBase includes author_id as a request body field, but since books are created through /authors/{author_id}/books, the author_id should come from the path parameter, not the request body. Remove author_id from BookBase and keep it only in the Book response schema for serialization. This will allow clients to create books without duplicating the author_id in both the URL and body.

Comment thread schemas.py
Comment on lines +19 to +23

class BookBase(BaseModel):
title: str
summary: str
publication_date: date
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

BookBase still includes author_id as a request body field (line 23). Since books are created via /authors/{author_id}/books endpoint, the author_id should come from the path parameter, not the request body. Clients would need to send author_id twice (in path and body), which is redundant. Remove author_id from BookBase and keep it only in the Book response schema for serialization.

Comment thread main.py
Comment on lines +19 to +24
db.close()


@app.get("/authors/", response_model=list[schemas.Author])
def get_authors(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
return crud.get_authors(db, skip=skip, limit=limit)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

BookBase includes author_id: int as a request body field, but since books are created via /authors/{author_id}/books, the author_id comes from the path parameter. This requires clients to send author_id twice (in path and body), which is redundant. Remove author_id from BookBase and only include it in the Book response schema for serialization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants