oaregithub / oare_mono

1 stars 0 forks source link

Refactor `/people/:letter` backend route #1498

Closed hbludworth closed 2 years ago

hbludworth commented 2 years ago

The first step in getting the Persons/People (which should we call it) functionality refactored is to get the backend route built. This is the backend route that will get a list of People that start with a given letter.

This issue is not very detailed as to how to exactly do things so that you can practice figuring some of this out yourself. The general idea is there though.

We will be replacing the existing GET /people/:letter route. You can keep everything as is except for the code that falls within the try block, including the permissionsRoute. Erase everything within the try block.

  1. Create a new interface within the people.ts file in the types package. Call it PersonRow. It should have camelCase properties for each column in the person table, except for id.
export interface PersonRow {
   uuid: string;
   nameUuid: string | null;
   relation: string | null;
   relationNameUuid: string | null;
   label: string;
   type: string;
}
  1. Get the letter from the request parameters.

const { letter } = req.params;

  1. Write a DAO function for getting PersonRows in the existing PersonDao. Call it getPersonRowsByLetter. It should take two parameters. First, a string called letter. Second, the trx transaction seen in all other DAO functions. It will return a PersonRow[].

At the beginning of this function, you will want to almost directly copy the beginning of the getWords function in the DictionaryWordDao. Copy lines 127-155 into the new getPersonRowsByLetter function. You'll want to change what you copied from line 129, however, to use the person table and SELECT the columns referenced in the PersonRow interface using AS statements as needed.

At the bottom, you'll need to add a WHERE type = 'person' and any ORDER BY you might need.

  1. Back within the GET /people/:letter route, call the new getPersonRowsByLetter function, passing in the letter you got from the request parameters. You should now have a variable of type PersonRow[] that has all the persons whose names start with the letter.

Using an await Promise.all statement (ask me if you need some explaining there), map over the array and call ItemPropertiesDao.getPropertiesByReferenceUuid to get the properties for each person.

  1. Create another interface called PersonListItem. It will look like this:

    export interface PersonListItem {
    person: PersonRow;
    display: string;
    properties: ItemPropertyRow[];
    occurrences: number;
    }
  2. Within the API route, use .map to loop over the PersonRow[] that you have. From this mapping, you will create a PersonListItem[], which is what the API route will send as the response.

Within the mapping, you will generate the display as we've discussed, using the label as a backup. You'll connect the properties you found above by using the index in the mapping. Finally, for now, just set occurrences to 0. We'll generate that number in the next issue.

  1. Send this PersonListItem[] as the HTTP response.

At this point, you should be able to use Postman to send a request to this route and get a list of the persons.

In the next issue, we will implement the cache in this route and get the occurrences working.

hbludworth commented 2 years ago

Closed by PR #1504