Listing People

Optional Lesson

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

If you open click on the People link in the main navigation, you will see a paginated list of actors and directors.

A list of people can be retrieved by calling the http://localhost:3000/people/ endpoint. The list is populated by the all() method in the PeopleDAO.

python
api/dao/people.py
def all(self, q, sort = 'name', order = 'ASC', limit = 6, skip = 0):
    # TODO: Get a list of people from the database
    # TODO: Remember to use double braces to replace the braces in the Cypher query {{ }}

    return people[skip:limit]

This person lists vary slightly from the movie lists because this features a Search by Name input. When this value is set, an additional q parameter is passed to tx.run(), and if defined, the query adds an additional CONTAINS predicate.

cypher
Find People with CONTAINS predicate
MATCH (p:Person)
WHERE $q IS NULL OR p.name CONTAINS $q
RETURN p { .* } AS person
ORDER BY p.name ASC
SKIP $skip
LIMIT $limit

Using Text Indexes

The STARTS WITH, ENDS WITH and CONTAINS predicates are case-sensitive. If you are looking for a case-insensitive search, you should consider setting up a full-text schema index.

Your Task

  • Modify the all() method to query Neo4j and return a list of people.

  • If the q parameter is defined, add a WHERE clause to check that the name property contains the value provided.

Click to reveal the completed all() method
python
api/dao/people.py
def all(self, q, sort = 'name', order = 'ASC', limit = 6, skip = 0):
    # Get a list of people from the database
    def get_all_people(tx, q, sort, order, limit, skip):
        cypher = "MATCH (p:Person) "

        # If q is set, use it to filter on the name property
        if q is not None:
            cypher += "WHERE p.name CONTAINS $q"

        cypher += """
        RETURN p {{ .* }} AS person
        ORDER BY p.`{0}` {1}
        SKIP $skip
        LIMIT $limit
        """.format(sort, order)

        result = tx.run(cypher, q=q, sort=sort, order=order, limit=limit, skip=skip)

        return [ row.get("person") for row in result ]

    with self.driver.session() as session:
        return session.execute_read(get_all_people, q, sort, order, limit, skip)

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/14_person_list__test.py

The test file is located at tests/14_person_list__test.py.

Are you stuck? Click here for help

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

sh
Check out the 14-person-list branch
git checkout 14-person-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

What is the name of the first person in the database in alphabetical order?

As part of the test suite, the final test will log the name of the first person to appear in the database in name order.

Paste the name of the person the box below without quotes or whitespace and click Check Answer.

  • ✓ 'Snub' Pollard

Hint

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

cypher
MATCH (p:Person)
RETURN p.name
ORDER BY p.name ASC LIMIT 1

Copy the answer without any double quotes or whitespace.

Lesson Summary

In this Challenge, you implemented the all() method in the PeopleDAO to return a paginated list of people from the Neo4j database and implemented a conditional WHERE clause on the Person name property.

In the next Challenge, you will retrieve an individual’s details from the database.