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.
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.
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.
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.
Create another interface called PersonListItem. It will look like this:
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.
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.
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 thetry
block, including thepermissionsRoute
. Erase everything within thetry
block.people.ts
file in thetypes
package. Call itPersonRow
. It should have camelCase properties for each column in theperson
table, except forid
.const { letter } = req.params;
PersonDao
. Call itgetPersonRowsByLetter
. It should take two parameters. First, astring
calledletter
. Second, thetrx
transaction seen in all other DAO functions. It will return aPersonRow[]
.At the beginning of this function, you will want to almost directly copy the beginning of the
getWords
function in theDictionaryWordDao
. Copy lines 127-155 into the newgetPersonRowsByLetter
function. You'll want to change what you copied from line 129, however, to use theperson
table andSELECT
the columns referenced in thePersonRow
interface usingAS
statements as needed.At the bottom, you'll need to add a
WHERE type = 'person'
and anyORDER BY
you might need.GET /people/:letter
route, call the newgetPersonRowsByLetter
function, passing in the letter you got from the request parameters. You should now have a variable of typePersonRow[]
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 callItemPropertiesDao.getPropertiesByReferenceUuid
to get the properties for each person.Create another interface called
PersonListItem
. It will look like this:Within the API route, use
.map
to loop over thePersonRow[]
that you have. From this mapping, you will create aPersonListItem[]
, which is what the API route will send as the response.Within the mapping, you will generate the
display
as we've discussed, using thelabel
as a backup. You'll connect the properties you found above by using the index in the mapping. Finally, for now, just setoccurrences
to0
. We'll generate that number in the next issue.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.