Now we're going to write the code for our rest API endpoints also call these routes we are going to
do this inside the app store.
Jay S-5 said that's in start AWOS STK with save flag will also install some utility modules like moment
underscore and you ID will need this more use for some operations.
And then we reference the ADA vs SDK and instantiate an object of dynamically read document client class
as usual
the Express router has various methods available like get post patch delete that we can use to process
incoming UDP requests with the same name like SCDP.
Get to the post patch delayed and so on.
So for a moment let's think about what API requests are no taking and are is likely to make on behalf
of its users
we might need a way for users to create a new node.
So we would need a Strout to insert a node item and animo the table when users log into the app.
They should see all the notes that they previously created.
So we also need to get route to get all the notes created by the user who has logged in a user might
want to view or edit an existing node.
So we need to get around to return a single note and then patch route to a date that node or to save
changes and finally user might also want to remove it if they dont need it anymore.
So we also need a delete route to delete the selected No.
So let's go ahead and create these API endpoints.
All right let's begin with post-draft to add a new note.
So we're out door post and then we specify the end point.
You are a let's say slash API slash node and then a function that takes a request response and next
object's request is an incoming request object response is the object that we send out as SATB response
and next is a function that can pass control to the next request response processing block.
If there is any sense this function of ours will return the final response back to the calling application.
We don't have the makings of the next function here as there is no processing block after this.
So inside this function we are simply going to populate the parents object required by dynamically put
item operation and call the put.
I am using the document client object.
Let's declare a new variable item to hold item data received from the calling application and this will
be a rebel in the request dot body object.
Since this is a post request so request dot and body dot item for example.
Now we are going to have the Colling application send us only the node title content and category and
will populate the rest of the attributes ourselves.
We don't want to give the end user or the application the ability to alter user id attribute.
This way we allow the users to add notes against their own User ID and prevent them from adding theirs
with the user IDs of other users.
This is not a foolproof access control though and will fix it with fine grained access control later
when we implement Federated access server inside the item variable.
We now have data for our title content and get Taggerty let's populate the remaining variables as well.
I'm going to hard code the user id as test underscore user and user name as Test User
later on when we implement user authentication in our app will pause the actual data here.
Then to populate node ID I'm going to use the UI you are the module Lysa.
This will generate random you you are.
They are universally unique identifier.
In other words this will generate a fairly unique random string every time so it will always have a
new note for every new note that we create.
I'm also going to prefix this random string with the user id like user id concatenated with the union
ID and separated by a colon character.
I'm doing this so when we query the items from the secondary index and the secondary indexes on their
ID attribute in this case.
So when we query the items from the secondary index we should have a way to identify the user from the
next key and this is going to allow us to set up fine grained access control on this secondary index
and will do this later when we set up the Federated access using cognito time Stan Williams moment dog
you're next as usual
and for the expires attribute I'm going to use that time stamp 90 days in the future just as an example.
Then we simply call the put method to insert the item into the dining table table the table name is
TV.
Note that we are recreated and I'm going to declare it as a constant.
We can reuse it across all our route definitions.
If there is an error we console on the error object and return back the responses when the corresponding
status code and message
else really.
UDP 200 to denote a successful operation
and that's it.
Let's test this out using postman.
I'm going to use as GTP post request and our API endpoint as local host column 3000 followed by a slash
API slash note.
And in the request by the way RS Jason's ring with item attribute containing title content and category
just like that
I'm going to save this
and then hit send to maybe post request card.
We see 200 OK response.
This means the operation was successful and we should see a new item inserted in the alimony.
And we do.
Let's create a few more such items by fighting this API call a few more times.
All right the next round is SATB patch operation to allow users to date existing notes for this we could
use the same root operation but this time we want the users to tell us the node ID and the time stamp
of the original node that they wish to update timestep will allow us to locate their existing node using
the combination of user ID and time span as the primary key.
And then we can replace it with the new data.
We do not want to change the node ID this time so we asked the end users to parse the original node
ID as well in the rest.
So I'm simply going to copy all the code from the previous route and it just it slightly
we get the item from request body then populate the user id username and the expires attribute just
as we did the previous route.
I'm going to remove the code that populates node ID and times that as now we are going to receive it
from the end user.
And then we simply call the document client port operation.
At this time we need to perform a conditional write operation.
We should first check if the item with the given sarky exists and updated only if it does exist.
We do not want to create a new item if the users sensing a wrong time stamp.
So it's a good idea to check if the if the time stamp exists or not.
So I'm going to use the conditional expression hash t equal to Colon T.
And then define these variables using expression attribute names an expression attribute ralliers
the time stamp value will be available in the item variable.
And rest of the page should remain the same just like the previous book operation.
Let's test this out.
I'm going to create a patsy request like so.
And then let's copy the times tab and note Id from an existing item from our Dynamo the table
changed the title and current and slightly saved this
and send the request
and it returned 200.
OK.
And if we look at the dynamo D-B console we do see that the change is reflecting here as well.
Now let's see what happens if we pass and non-existent time stamp.
So let's change the time stamp here and find another regressed and we see 400 bad request with an error
message saying conditional request failed.
So we can only update existing items.
Awesome.
Let's continue building the API in the next media.