Browsing Genres

Optional Lesson

This lesson is optional and will not count towards your achievement. To view the completed code, check out the 09-genre-list branch.

If you click on the Genres link in the main navigation, you will be taken to a list of Genres. This list is populated by the API route at http://localhost:3000/api/genres, with the list being produced by the all() method within the GenreDAO.

python
api/dao/genres.py
def all(self):
    # TODO: Open a new session
    # TODO: Define a unit of work to Get a list of Genres
    # TODO: Execute within a Read Transaction

    return genres

In this challenge, you will modify the method to run the following Cypher statement in a read transaction:

cypher
Listing Genres
MATCH (g:Genre)
WHERE g.name <> '(no genres listed)'

CALL {
  WITH g
  MATCH (g)<-[:IN_GENRE]-(m:Movie)
  WHERE m.imdbRating IS NOT NULL AND m.poster IS NOT NULL
  RETURN m.poster AS poster
  ORDER BY m.imdbRating DESC LIMIT 1
}

RETURN g {
  .*,
  movies: count { (g)<-[:IN_GENRE]-(:Movie) },
  poster: poster
}
ORDER BY g.name ASC

Your Task

  • Modify the all() method on the GenreDAO to query Neo4j and return a list of properties ordered by genre name.

  • Each genre should have a name, the number of movies listed in that genre (movies), and a poster image (from the most popular movie in that genre).

Working Solution

Click here to view the completed all() method.
python
api/dao/genres.py
def all(self):
    # Define a unit of work to Get a list of Genres
    def get_movies(tx):
        result = tx.run("""
            MATCH (g:Genre)
            WHERE g.name <> '(no genres listed)'
            CALL {
                WITH g
                MATCH (g)<-[:IN_GENRE]-(m:Movie)
                WHERE m.imdbRating IS NOT NULL AND m.poster IS NOT NULL
                RETURN m.poster AS poster
                ORDER BY m.imdbRating DESC LIMIT 1
            }
            RETURN g {
                .*,
                movies: count { (g)<-[:IN_GENRE]-(:Movie) },
                poster: poster
            } AS genre
            ORDER BY g.name ASC
        """)

        return [ g.value(0) for g in result ]

    # Open a new session
    with self.driver.session() as session:
        # Execute within a Read Transaction
        return session.execute_read(get_movies)

Testing

To test that this functionality has been correctly implemented, run the following code in a new terminal session:

sh
Running the test
pytest -s tests/09_genre_list__test.py

The test file is located at tests/09_genre_list__test.py.

Are you stuck? Click here for help

If you get stuck, you can see a working solution by checking out the 09-genre-list branch by running:

sh
Check out the 09-genre-list branch
git checkout 09-genre-list

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

Which genre has the highest movie count?

After the test has succeeded, the test suite will log the name of the genre with the greatest number of movies.

Enter the name of the genre below and click Check Answer.

  • ✓ Drama

Hint

You can also find the answer by running the following Cypher statement:

cypher
MATCH (g:Genre)
RETURN g.name ORDER BY count{ (g)<-[:IN_GENRE]-() } DESC LIMIT 1

Copy the answer without any quotes or whitespace.

Lesson Summary

In this Challenge, you updated the GenreDAO to retrieve a list of genres from the database.

In the next Challenge, you will update the GenreDAO to query Neo4j to find the details of an individual genre.