Optional Lesson
This lesson is optional and will not count towards your achievement.
To view the completed code, check out the 10-genre-details
branch.
When the user clicks a genre in the list, they are taken to a list of movies for that genre. This list is populated by an API request to /api/genres/[name]
, for example http://localhost:3000/api/genres/Comedy.
The find()
method the the GenreDAO
accepts one argument, the name of the genre, and should return the information about the genre, along with a count of movies and a poster image.
def find(self, name):
# TODO: Open a new session
# TODO: Define a unit of work to find the genre by it's name
# TODO: Execute within a Read Transaction
return [g for g in genres if g["name"] == name][0]
In this challenge, you will modify the method to run the following Cypher statement in a read transaction:
MATCH (g:Genre {name: $name})<-[:IN_GENRE]-(m:Movie)
WHERE m.imdbRating IS NOT NULL AND m.poster IS NOT NULL AND g.name <> '(no genres listed)'
WITH g, m
ORDER BY m.imdbRating DESC
WITH g, head(collect(m)) AS movie
RETURN g {
.name,
movies: count { (g)<-[:IN_GENRE]-() },
poster: movie.poster
} AS genre
What does this query do?
This query uses the MATCH
clause for a Genre
node with the name passed through with the function call as a parameter.
The query then finds the highest rated movie with a poster
property and uses that image as the background to the card in the UI.
The size()
function uses a precalculated value stored against the Genre
node to return the number of incoming IN_GENRE
relationships the Genre
node.
Your Task
-
Modify the
find()
method on theGenreDAO
to call the Neo4j database and return details for a genre. -
The
name
variable should be passed to therun()
call as a parameter. -
If no records are found, a
NotFoundException
should be raised.
Working Solution
Click here to view the completed find()
method.
def find(self, name):
# Define a unit of work to find the genre by it's name
def find_genre(tx, name):
first = tx.run("""
MATCH (g:Genre {name: $name})<-[:IN_GENRE]-(m:Movie)
WHERE m.imdbRating IS NOT NULL AND m.poster IS NOT NULL AND g.name <> '(no genres listed)'
WITH g, m
ORDER BY m.imdbRating DESC
WITH g, head(collect(m)) AS movie
RETURN g {
.name,
movies: size((g)<-[:IN_GENRE]-()),
poster: movie.poster
} AS genre
""", name=name).single()
# If no records are found raise a NotFoundException
if first == None:
raise NotFoundException()
return first.get("genre")
# Open a new session
with self.driver.session() as session:
# Execute within a Read Transaction
return session.execute_read(find_genre, name)
Testing
To test that this functionality has been correctly implemented, run the following code in a new terminal session:
pytest -s tests/10_genre_details__test.py
The test file is located at tests/10_genre_details__test.py
.
Are you stuck? Click here for help
If you get stuck, you can see a working solution by checking out the 10-genre-details
branch by running:
git checkout 10-genre-details
You may have to commit or stash your changes before checking out this branch. You can also click here to expand the Support pane.
Verifying the Test
How many movies are in the Action genre?
After the test has succeeded, the test suite will log the count of movies in the Action genre to the console.
Enter the number of movies below and click Check Answer.
-
✓ 1545
Hint
You can also find the answer by running the following Cypher statement:
MATCH (g:Genre {name: "Action"})
RETURN count { (g)<-[:IN_GENRE]-() }
Lesson Summary
In this Challenge, you modified the find()
method in the GenreDAO
to retrieve genre information from Neo4j.
In the next Challenge, you will update multiple methods in the MovieDAO
that return a paginated list of movies.