Skip to content

Solution#637

Open
PVS1905 wants to merge 2 commits into
mate-academy:mainfrom
PVS1905:develop
Open

Solution#637
PVS1905 wants to merge 2 commits into
mate-academy:mainfrom
PVS1905:develop

Conversation

@PVS1905
Copy link
Copy Markdown

@PVS1905 PVS1905 commented Apr 6, 2026

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

This is a great start, and the overall structure of your project is solid. However, I'm requesting a few changes because there are some critical issues that prevent the application from running and meeting the core requirements.

Here are the main blockers to address:

  1. Database Tables Not Created: In main.py, the application is missing the logic to create the database tables. You need to import engine and Base and call Base.metadata.create_all(bind=engine) before the app starts to prevent runtime errors.
  2. Incorrect Query Execution: In crud.py, the get_all_books and get_authors_list functions mix legacy and modern SQLAlchemy syntax. Calling db.scalars() on a db.query() object will cause an error. Please change this to query.all() to correctly fetch the results.
  3. Broken Filtering Logic: The get_all_books function in crud.py has inverted logic for filtering by author_id. The filter should be applied when author_id is not None.

Once these issues are resolved, the application should function as expected. Keep up the good work!


✨ 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 main.py
from db.database import SessionLocal
from db.models import Book

app = FastAPI()
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 application is missing the logic to create the database tables, which is a key requirement. You'll need to import engine from your database setup and Base from your models, and then call Base.metadata.create_all(bind=engine) to ensure the tables exist before the API starts.

Comment thread main.py


@app.get("/books/", response_model=list[schemas.BookListSchema])
def read_cheese_types(
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 function name read_cheese_types does not reflect what the function does (reading books). A more descriptive name, such as read_books, would significantly improve the code's readability and maintainability.

Comment thread main.py
)


@app.post("/books/", response_model=schemas.BookSchemaBase)
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 response_model is set to schemas.BookSchemaBase, which does not include the book's id. When a resource is created, the API response should typically include the full resource, including its unique identifier. Consider using a schema that includes the id field.

Comment thread main.py Outdated
return crud.get_authors_list(db=db, skip=skip, limit=limit)


@app.post("/authors/", response_model=schemas.AuthorSchemaBase)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Similar to the book creation endpoint, the response_model here (schemas.AuthorSchemaBase) does not include the new author's id. It's a best practice to return the id of a newly created resource. Using a schema that includes the id would be more conventional for a REST API.

Comment thread crud.py Outdated
Comment on lines +17 to +18
if author_id is None:
query = query.where(models.Book.author_id == author_id)
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 logic for filtering by author_id is inverted. This if condition only applies the filter when author_id is None, but the requirement is to filter when an author_id is provided. You should check if author_id is not None.

Comment thread crud.py Outdated
query = query.where(models.Book.author_id == author_id)
query = query.limit(limit).offset(skip)

return list(db.scalars(query))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There's a mix of SQLAlchemy query styles here. db.query() returns a Query object (legacy 1.x style), but db.scalars() expects a select() construct (modern 2.0 style). To execute the Query object and get a list of results, you should use .all() instead, like so: query.all().

Comment thread crud.py Outdated
def create_author(
db: Session,
author: schemas.AuthorCreateSchema
) -> models.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 function's return type hint is models.Book, but the implementation creates and returns an models.Author object on line 72. The type hint should be updated to models.Author to match the actual return value.

Comment thread crud.py Outdated
) -> list[models.Author]:
query = db.query(models.Author).offset(skip).limit(limit)

return list(db.scalars(query))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Similar to the get_all_books function, this line mixes SQLAlchemy API styles. The query object was created with db.query(), so you should call query.all() to retrieve the list of authors instead of using db.scalars().

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

Excellent work on this iteration! I am approving your submission as you've successfully addressed all the critical issues and met all the functional requirements. The database setup, models, and CRUD logic are all implemented correctly.

I have a few minor suggestions to enhance code clarity and consistency for you to consider in future projects:

  • In main.py, the function read_cheese_types could be renamed to read_books to better reflect its purpose.
  • Also in main.py, consider changing the response_model for the book creation endpoint to schemas.BookListSchema to include the new book's id in the response, which is a common REST API practice.
  • In crud.py, adding the : Session type hint to the db parameter in get_authors_list would make it consistent with other functions.

These are just suggestions for improvement, not blockers. Great job getting the application fully functional!


✨ 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 crud.py
return db_author


def get_authors_list(db, skip: int, limit: int) -> list[models.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.

It's a good practice to maintain consistency with type hints. The db parameter is missing its type hint here, unlike the other functions in this file. Please add : Session to it.

Comment thread main.py


@app.get("/books/", response_model=list[schemas.BookListSchema])
def read_cheese_types(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This function name, read_cheese_types, seems to be a leftover from a template or example. It's a bit confusing for an endpoint that fetches books. Consider renaming it to something more descriptive, like read_books, to match the endpoint's functionality.

Comment thread main.py
)


@app.post("/books/", response_model=schemas.BookSchemaBase)
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 response_model is set to schemas.BookSchemaBase, which doesn't include the id of the book. It's a common practice in REST APIs to return the full resource, including its unique identifier, after it's been created.

Consider changing this to schemas.BookListSchema to ensure the id of the newly created book is included in the response.

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