Browsing Genres

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

java
neoflix/services/GenreService.java
public List<Map<String, Object>> all() {
    // TODO: Open a new session
    // TODO: Get a list of Genres from the database
    // TODO: Close the session

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

  • Remember to close the session and use the toMap() function result.list to convert the values to native Java types.

Working Solution

Click here to view the completed all() method.
java
neoflix/services/GenreService.java
public List<Map<String, Object>> all() {
    // Open a new Session, close automatically at the end
    try (var session = driver.session()) {
        // Get a list of Genres from the database
        var query = """
                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 {
                  .name,
                  link: '/genres/'+ g.name,
                  poster: poster,
                  movies: count { (g)<-[:IN_GENRE]-() }
                } as genre
                ORDER BY g.name ASC
                """;
        var genres = session.executeRead(
                tx -> tx.run(query)
                        .list(row ->
                            row.get("genre").asMap()));

        // Return results
        return genres;
    }
}

Testing

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

sh
Running the test
mvn test -Dtest=neoflix._09_GenreListTest

The test file is located at src/test/java/neoflix/_09_GenreListTest.java.

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 GenreService to retrieve a list of genres from the database.

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