Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Solution #44

wants to merge 2 commits into from

Conversation

BitRxp
Copy link

@BitRxp BitRxp commented Feb 11, 2025

No description provided.

Copy link

@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.


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:

  1. Response Handling in patch_movie: The function currently raises an HTTPException 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.

  2. 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.

  3. 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.

Comment on lines 41 to 42
genres_data = db.query(GenreModel).filter(GenreModel.name.in_(movie.genres)).all()
if not genres_data:

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.

Comment on lines 45 to 46
actors_data = db.query(ActorModel).filter(ActorModel.name.in_(movie.actors)).all()
if not actors_data:

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.

Comment on lines 49 to 50
languages_data = db.query(LanguageModel).filter(LanguageModel.name.in_(movie.languages)).all()
if not languages_data:

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.

Comment on lines +72 to +74
except SQLAlchemyError:
db.rollback()
raise HTTPException(status_code=400, detail="Invalid input data.")

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.")

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.

@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.")

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.

class CountrySchema(BaseModel):
id: int
code: str
name: str | None

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.


class MovieCreateSchema(BaseModel):
name: str = Field(..., max_length=255)
date: date_module = Field(..., le=(date_module.today() + timedelta(days=365)))

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.

Copy link

@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

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:

  1. 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 an HTTPException. This change is crucial for ensuring the API behaves as expected.

  2. 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.

  3. 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.

  4. Session Binding in SQLite: Bind the session directly to sqlite_engine instead of sqlite_connection to avoid issues with transaction management.

  5. Path Construction in settings.py: Correct the syntax error in the PATH_TO_MOVIES_CSV path construction to ensure it points accurately to the imdb_movies.csv file.

  6. 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! ✨

Comment on lines +11 to +13
PATH_TO_MOVIES_CSV: str = str(
BASE_DIR / "database" / "seed_data" / "imdb_movies.csv"
)

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.

Comment on lines +58 to +59
if not genres_data:
genres_data = create_objects(movie.genres, GenreModel, db)

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.

Comment on lines +64 to +65
if not actors_data:
actors_data = create_objects(movie.actors, ActorModel, db)

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.

Comment on lines +72 to +73
if not languages_data:
languages_data = create_objects(movie.languages, LanguageModel, db)

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.")

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."}

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.

Comment on lines +105 to +108
@field_validator("country", mode="before")
@classmethod
def normalize_country(cls, value: str) -> str:
return value.upper()

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.

Comment on lines +110 to +113
@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]

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.

Comment on lines +244 to +246
f"/theater/movies/?page={page + 1}&per_page={per_page}"
if page < total_pages
else None

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.

Comment on lines +471 to +473
assert (
response_data["detail"] == expected_detail
), f"Expected detail message: {expected_detail}, but got: {response_data['detail']}"

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.

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