-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solution #44
base: main
Are you sure you want to change the base?
Solution #44
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your hard work on this project! 😊 While your implementation meets many of the task requirements, there are a few critical issues that need to be addressed before we can approve the pull request:
-
Response Handling in
patch_movie
: The function currently raises anHTTPException
with a status code of 200, which is incorrect. Instead, it should return a JSON response with a success message and a status code of 200. This is crucial for ensuring the API behaves as expected. -
Data Integrity in Relationships: There are several areas where the handling of related data (genres, actors, languages) could lead to data integrity issues. Specifically, the current checks might not account for partial matches, which could result in duplicate entries or missing data. It's important to ensure that only missing related entities are created.
-
Error Handling and Logging: Consider improving error handling by logging exception details. This will aid in debugging and provide better error tracking.
Please address these issues to ensure the functionality and integrity of the code. Once these are resolved, we can proceed with the approval. Keep up the great work, and don't hesitate to reach out if you need any assistance! 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
🔄 Fix the issues, commit the changes, and then re-request my review using the Re-request review
button in the Reviewers tab.
src/crud/movies.py
Outdated
genres_data = db.query(GenreModel).filter(GenreModel.name.in_(movie.genres)).all() | ||
if not genres_data: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check for genres_data
being empty might not be sufficient if some genres exist and some don't. Consider handling partial matches by creating only the missing genres.
src/crud/movies.py
Outdated
actors_data = db.query(ActorModel).filter(ActorModel.name.in_(movie.actors)).all() | ||
if not actors_data: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to genres, the check for actors_data
might not account for partial matches. Ensure that only missing actors are created.
src/crud/movies.py
Outdated
languages_data = db.query(LanguageModel).filter(LanguageModel.name.in_(movie.languages)).all() | ||
if not languages_data: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same issue applies here for languages_data
. Handle cases where some languages exist and others need to be created.
except SQLAlchemyError: | ||
db.rollback() | ||
raise HTTPException(status_code=400, detail="Invalid input data.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider logging the exception details for better debugging and error tracking, rather than just raising a generic HTTPException.
src/main.py
Outdated
@router.patch("/movies/{movie_id}/") | ||
def patch_movie(movie_id: int, movie: MovieUpdateSchema, db: Session = Depends(get_db)): | ||
update_movie(db, movie_id, movie) | ||
return HTTPException(status_code=200, detail="Movie updated successfully.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The patch_movie
function should return a success response, not raise an HTTPException. Consider returning a JSON response with a success message instead.
src/routes/movies.py
Outdated
@router.patch("/movies/{movie_id}/") | ||
def patch_movie(movie_id: int, movie: MovieUpdateSchema, db: Session = Depends(get_db)): | ||
update_movie(db, movie_id, movie) | ||
return HTTPException(status_code=200, detail="Movie updated successfully.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The patch_movie
function should return a success response, not raise an HTTPException. Consider returning a JSON response with a success message instead.
src/schemas/movies.py
Outdated
class CountrySchema(BaseModel): | ||
id: int | ||
code: str | ||
name: str | None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using Optional[str]
instead of str | None
for consistency with other optional fields in the schemas.
src/schemas/movies.py
Outdated
|
||
class MovieCreateSchema(BaseModel): | ||
name: str = Field(..., max_length=255) | ||
date: date_module = Field(..., le=(date_module.today() + timedelta(days=365))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The date
field in MovieCreateSchema
allows dates up to one year in the future. Ensure this aligns with your application's requirements for movie release dates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review Summary
Great job on implementing the code! 🎉 While the overall structure and functionality are well thought out, there are several critical issues that need attention to ensure the application runs smoothly and maintains data integrity. Here are the key points to address:
-
Response Handling in
patch_movie
: The function should return a JSON response with a success message and a status code of 200 instead of raising anHTTPException
. This change is crucial for ensuring the API behaves as expected. -
Data Integrity in Relationships: The current logic for handling genres, actors, and languages may lead to duplicates if some entities already exist. Modify the logic to create only the missing entities to prevent data integrity issues.
-
Error Handling and Logging: Improve error handling by wrapping database operations in try-except blocks and logging exceptions. This will aid in debugging and provide better error tracking.
-
Session Binding in SQLite: Bind the session directly to
sqlite_engine
instead ofsqlite_connection
to avoid issues with transaction management. -
Path Construction in
settings.py
: Correct the syntax error in thePATH_TO_MOVIES_CSV
path construction to ensure it points accurately to theimdb_movies.csv
file. -
Migration Configuration: Ensure the
run_migrations_offline
function is correctly configured to use a URL instead of an engine for offline migrations.
These improvements will enhance the robustness and maintainability of your code. Keep up the good work, and use this feedback to refine your skills further. Remember, addressing these issues will not only improve this project but also help you avoid similar problems in the future. Happy coding! 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
PATH_TO_MOVIES_CSV: str = str( | ||
BASE_DIR / "database" / "seed_data" / "imdb_movies.csv" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a syntax error in the path construction for PATH_TO_MOVIES_CSV
. The path should not end with a slash, as it is not a directory. Ensure the path is correctly constructed to point to the imdb_movies.csv
file.
if not genres_data: | ||
genres_data = create_objects(movie.genres, GenreModel, db) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current logic for handling genres, actors, and languages may lead to data integrity issues. If any of these entities partially exist, the create_objects
function will create duplicates for the non-existing ones. Consider modifying the logic to only create missing entities.
if not actors_data: | ||
actors_data = create_objects(movie.actors, ActorModel, db) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the genres handling, the logic for actors may lead to duplicates if some actors already exist. Ensure that only missing actors are created.
if not languages_data: | ||
languages_data = create_objects(movie.languages, LanguageModel, db) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for handling languages can also lead to duplicates if some languages already exist. Modify the logic to create only the missing languages.
|
||
except SQLAlchemyError: | ||
db.rollback() | ||
raise HTTPException(status_code=400, detail="Invalid input data.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding logging for the SQLAlchemyError to improve error tracking and debugging.
db.rollback() | ||
raise HTTPException(status_code=400, detail="Invalid input data.") | ||
else: | ||
return {"detail": "Movie updated successfully."} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The update_movie
function correctly returns a success message upon successful update. Ensure consistency in response format across different endpoints.
@field_validator("country", mode="before") | ||
@classmethod | ||
def normalize_country(cls, value: str) -> str: | ||
return value.upper() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The normalize_country
validator correctly normalizes the country code to uppercase. Ensure that the input data is consistent with this format.
@field_validator("genres", "actors", "languages", mode="before") | ||
@classmethod | ||
def normalize_list_fields(cls, value: List[str]) -> List[str]: | ||
return [item.title() for item in value] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The normalize_list_fields
validator capitalizes the first letter of each word in the list fields. This is a good practice to ensure consistency in data formatting.
f"/theater/movies/?page={page + 1}&per_page={per_page}" | ||
if page < total_pages | ||
else None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure that the next_page
link is correctly formatted and matches the expected URL structure. This is crucial for verifying pagination functionality.
assert ( | ||
response_data["detail"] == expected_detail | ||
), f"Expected detail message: {expected_detail}, but got: {response_data['detail']}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test for duplicate movie creation correctly checks for a 409 conflict error. Ensure that the error message is consistent with the expected format.
No description provided.