Let's continue building our arrest API in this demo now when they use a lot of thing to our application
we might want to list all the notes created by him or her.
So we are going to create Getaround that runs a crazy operation on dynamo Levy and returns up to say
five nodes created by the user and sorted by the time stamp in descending order.
So router are get slash API slash notes we call it nodes in plural form since we are expecting to receive
one or more items in response.
And then the processing function
lets declare the pad on some variable table name is table name and so we specify the key condition expression
are the where clause of our query operation as user ID equals colon U R and D and then define colon
you ID using the expression attribute values.
So we are attempting to create all items with hash key equal to the user id of the logged in user limit
as a limit.
And I want to create or limit variable or declared a limit variable to take in an optional limit value
from the string.
So if that calling application pro-white a limit value we return that main number of items as we default
to about 5 items.
We also want the items to be in reverse chronological order meaning the recently created Nel's should
appear first.
So so are the items by times Dan or the sarky in descending order to do that which specifies scanning
less forward as far as and this would return items sorted by the sarky in descending order.
Then since this is passionated query we want to restart key we have the user id We just need to know
the time stamp from the last evaluated key return in the previous operation.
So our API Quest can send in an optional star barometer as crazy string.
And we use it as the star key to get to the next page of items.
So let's define a variable star times tab and get its value from request.
Dart Querrey Darkstar we have to pass the query string paramita call start to get that next page for
the first page.
We don't need to parse this Parminter.
And if it is not Basto we assume it to be zero.
Then if the start time span is greater than zero we populate the exclusive Starky to query the next
page of results.
Finally we call the document blind method.
Just like that
and in the success call we turn the data along with our 200 success response.
Let's test this out.
First let's make the API call without any start parameter
and we do get five items with the latest item at the top and then we also have the last few valuated
key here.
Let's copy the time stamp from here and we'll pass it to the same request as a star parameter in the
query string.
So question mark Starr equal to the time stamp that be copied.
Hit send and receive them.
Next page of results.
Awesome.
So now our application can show the names created by the user and user can navigate through them page
by page.
Next if the user clicks on a node from this list he or she should be able to read the complete node.
So for that let's create another API out to return the selected note.
So we'll use the get method again.
This time we want the user to parse the node ID of the node to be returned.
And this should be mandatory parameter so we specify the mandatory pattern meters inside the API endpoint
Lysa router door get slash API slash node slash colon node ID here.
Note ID is the mandatory parameter that user must send in in order to make this API car.
And then the processing function as usual we get the node Id passed by the user from request dog pass
of it just like that.
Then lets move the parents argument for our Querrey operation.
This time we only have the note ID and we dont have the time stamp.
So we'll create the global secondary index that we have created on the node ID to give.
So Table name is table name index name as node ID index and we can find the actual indexed name from
the indexes tab in the Dynamo the console right here.
All right then we specify the red Glaus using the key condition expression lights.
Note ID equal to call an ID and then we define the colon ID is an expression attribute values like so.
We can also specify a limit as 1 since we don't expect to have two items with the same node ID then
we call the document client operation.
As usual
and if that ease success response there is a possibility of an empty response as well.
We just check if the state handout items is empty or not.
If it has data out we return the first item from the Arey Lysa else we turn 4 0 for not found response.
All right let's test the
let's speak up a note Id from our dining table.
And made the API call
just like that.
And we do get the requested item.
Awesome.
Now I'm going to duplicate an item inside dynamo D.B and change the user id times that A.I
just like that.
Let's assume that this item was inserted by another user called Test User 2.
And now if we make a get request with this no tidy Ideally they should see an error because the item
belongs to another user.
Let's try that out.
Oops.
But the API returned the item from another user as well.
And we'll fix this issue using fine grained access control when we implement Federated access using
in the US go to meet her.
Next up we want the users to be able to delete the display node.
For that we're going to use the delete method router dot delete slash API slash no slash Kurland time
stamp to delete an item.
We must pass they complete primary key.
We already have the user id with us.
So we simply asked the user to provide us with the time stamp.
And it should be a mandatory parameter and then the processing function we capture the time stamp from
request Dar Patten's object
pass it inside the parents variable as a key attribute along with the user id just like that.
And then we called the document blind darg delete method
just like that.
Let's test this out.
Simply big time stamp from Dynamo the table and make a delete request with that time stamp.
Just like that.
And we get 200 OK response.
And if we refresh the data in the table in the code and so we do see that the item has been deleted
ASA our API is now ready and we are now ready to integrate things that are front end to build and end
to end web application.
This has been a long lecture and thank you so much for bearing with me.
If you need to take a break do take one.
And when you come back we'll integrate this rest API with the front end application.