Instructor: 0:00 It's time to add some extra features to our Lambda function. Currently, we are able to get all the todos and next stop, we are going to implement the add-to-the-item functionality.
0:08 The source code for this lesson contains a slightly updated Lambda function. The changes are as follows. Whenever this Lambda function is going to receive a get request, we are going to call it the get-all-todos inaudible , but whenever we send a post request to this Lambda function, we are going to call it the add-to-the-item with the data that we got from the request body.
0:28 Let's jump to the add-to-the-item effort. In order to then how can we implement that in order to store data in DynamoDB, we can take a look at this DynamoDB DocumentClient cheat sheet once more. Here we can see that in order to create a new item, we can use the put method.
0:41 Here we can see that put is going to either create a new item or if the item already exists, it is going to be replaced. We can see that in order to put a new item in DynamoDB, we need to create the params object which is going to have the table name and also the item that we would like to put in. Afterwards, we have to call the put method with this object and then we can get the promise.
1:01 Let's go ahead and implement that. We are going to do await dynamo.put. This is going to take an object with two props. The first one is exactly the same as in get-all-todos so you can just copy and paste that. Just a quick reminder, this table name comes from the process.env table name. This is the environment variable that is being passed into this function.
1:19 Next, we need to add the item. An item is going to be an object with two feet. The first one is an id. For now, I'm going to hard code it to a totally random id and we are going to fix that in the next lessons.
1:31 We are also going to send a todo string so I can just add it over here, todo = "todo" or I can just remove that since both key and value have exactly the same name, which is todo. Since we are using await over here, we are going to add that promise at the end of the function. Let me save that. Open up the terminal and run cdk deploy in order to deploy those changes to AWS.
1:53 After a successful deployment, we can test our updated API. In order to test our API, we are going to use Insomnia. Insomnia is a REST client that I just happen to use, but honestly you can test your API in anything that allows you to send HTTP requests. You can even create your own client if you want to.
2:10 In any case, if I send a get request to our endpoint, I am going to get all the to-the-items that we have in our database. Let's add a to-the-item. For that, I'm going to switch to post. Next up, I'm going to select a body of the request to be a JSON. Next, I'm going to paste into JSON, so this is going to be a todo with a content of "Test if adding items to the table actually works."
2:30 Let me send the request. We are going to get a confirmation that our todo has been successfully added to our database. After we send the get request again, we are going to be able to see our todo with a totally random id.