1
00:00:00,240 --> 00:00:07,510
In this video, we are going to update product method for product lambda microservices with using SDK

2
00:00:07,530 --> 00:00:08,430
libraries.

3
00:00:08,640 --> 00:00:17,310
For that purpose we expect as a HTTP put request from client and Amazon API gateway to invoke our product

4
00:00:17,310 --> 00:00:18,870
Microsoft Lambda function.

5
00:00:18,870 --> 00:00:26,070
When we hit the HTTP port operation, we perform update operation from the product table of Dynamo DB

6
00:00:26,070 --> 00:00:28,590
table into our lambda function.

7
00:00:28,980 --> 00:00:38,130
So to do so, please open the studio code and go on top of the index JS file and we will add an additional

8
00:00:38,130 --> 00:00:42,810
switch statement, case statement for the HTTP port operation.

9
00:00:42,810 --> 00:00:45,450
But before that, let's check the incoming event.

10
00:00:45,450 --> 00:00:52,050
If you go to events and open the put by ID, the JSON, this is the structure of how lambda function

11
00:00:52,050 --> 00:00:58,110
triggered when performing the HTTP input operation aces method is a put.

12
00:00:58,140 --> 00:01:00,540
This is the resource and PID parameters.

13
00:01:00,540 --> 00:01:08,010
If you scroll on, you can see pet parameters in here and also you can see that this could be get the

14
00:01:08,010 --> 00:01:12,120
body information that you can update an existing attribute.

15
00:01:12,120 --> 00:01:15,750
So let's implement this function before we start.

16
00:01:15,750 --> 00:01:19,140
We should accommodate the incoming HTTP post operation.

17
00:01:19,140 --> 00:01:27,300
So that's why after the case delete operation, I would like to add a naive case which will be the.

18
00:01:28,310 --> 00:01:29,030
Days.

19
00:01:30,030 --> 00:01:30,870
Operation.

20
00:01:31,630 --> 00:01:31,880
Here.

21
00:01:31,920 --> 00:01:40,000
You can see that we have added the HTTP put operation with the body of the update product method and

22
00:01:40,000 --> 00:01:46,510
we are placing the whole event object into this method in order to perform update operation.

23
00:01:46,510 --> 00:01:53,650
So please add this case statement into your switch below of the handle method of lambda function.

24
00:01:53,650 --> 00:01:57,040
And this is the indexes we are in the main file.

25
00:01:57,070 --> 00:02:03,220
Save this file and now it is time to implement these update product function.

26
00:02:03,220 --> 00:02:08,830
Before we implement, we should understand which command that we will use during the update operation

27
00:02:08,830 --> 00:02:09,820
for that purpose.

28
00:02:09,850 --> 00:02:16,810
Open the dynamic DB Client SDK documentation and open the update item command update item command that

29
00:02:16,810 --> 00:02:21,610
we will use when editing and updating an item on Dynamo DB table.

30
00:02:21,610 --> 00:02:29,170
And here you can see the example usage and here is the input parameters that we define these properties

31
00:02:29,170 --> 00:02:37,330
when updating an item OC Let's implement this code to do so, please open the index JS file and go go

32
00:02:37,330 --> 00:02:38,800
at the end of this page.

33
00:02:38,800 --> 00:02:42,760
So now I'm going to copy and paste and explain after that.

34
00:02:42,760 --> 00:02:49,540
So basically update product is a bit complicated as you can see that it is a bit complicated in function

35
00:02:49,540 --> 00:02:51,550
that we implement this function.

36
00:02:51,550 --> 00:02:58,840
So basically if you come to documentation, you can see that as you remember that when we are performing

37
00:02:58,840 --> 00:03:06,190
update item with using the click commands, we are placing the table name, key update, expression

38
00:03:06,190 --> 00:03:10,210
and explicit attribute values and the return values.

39
00:03:10,210 --> 00:03:16,900
So these are the properties that we should define when we are using the update item command.

40
00:03:16,900 --> 00:03:24,610
So that takes some complicated code that we are going to provide into the our update product function.

41
00:03:24,610 --> 00:03:26,320
But the idea is the same.

42
00:03:26,320 --> 00:03:30,610
Before explaining this code below, let me add the required import statement.

43
00:03:30,610 --> 00:03:35,200
If you scroll down, we basically using to Marshall and we use update item command.

44
00:03:35,200 --> 00:03:41,800
Please import this update item command on top of the page after the delete command of the dynamic DB

45
00:03:41,800 --> 00:03:43,630
client import statement.

46
00:03:43,630 --> 00:03:50,500
And if you go back to our code, make sure that you have update item command imported successfully.

47
00:03:50,500 --> 00:03:53,050
So let me start explaining this method.

48
00:03:53,080 --> 00:03:59,800
Basically, we will start with the method body and this method is get a parameter which is the event

49
00:03:59,800 --> 00:04:03,010
JSON object and we are covering the case below.

50
00:04:03,040 --> 00:04:09,160
And in the first line we basically get event information, pass this information as a request request

51
00:04:09,160 --> 00:04:11,830
body to get the updated attributes.

52
00:04:11,860 --> 00:04:19,000
After that, we get the keys into these request body to get which which columns, which attribute values

53
00:04:19,000 --> 00:04:22,480
should be update and after that perform log operation.

54
00:04:22,480 --> 00:04:29,140
And after that, basically we provide the parameters for the update item command and the paste the parameters

55
00:04:29,140 --> 00:04:31,600
to the Dynamo DB client send operation.

56
00:04:31,600 --> 00:04:35,350
So a parameter so we can define these parameters.

57
00:04:35,350 --> 00:04:41,320
Basically, we will follow the same discipline into the documentation and the commands we provide the

58
00:04:41,320 --> 00:04:45,730
table name, key update, expression and expression, attribute values.

59
00:04:45,730 --> 00:04:51,580
So if you go back to our code, we can see the table name comes from the Dynamo DB table environment

60
00:04:51,580 --> 00:05:00,010
variables and see the key is an ID and we get the ID from event parameters ID and marshall this operation

61
00:05:00,010 --> 00:05:03,250
to simplify to dynamo DB key definition.

62
00:05:03,250 --> 00:05:06,820
And after that see that we have an update expression.

63
00:05:06,820 --> 00:05:14,140
We generate the automatic set statement with looking the incoming object keys of the JSON object.

64
00:05:14,140 --> 00:05:22,420
So that means in that stage I would like to show you expected HTTP put request but the payload so think

65
00:05:22,420 --> 00:05:27,100
about that we are using the HTTP operation and sent this URL.

66
00:05:27,130 --> 00:05:30,160
These are the parameters that we define in the key.

67
00:05:30,160 --> 00:05:32,680
And after that we send the payload, right.

68
00:05:32,680 --> 00:05:39,370
So these object keys is collected name and price operation and create a set keyword.

69
00:05:39,400 --> 00:05:46,630
And these values are setting in the explicit attribute values to get value information from this JSON

70
00:05:46,630 --> 00:05:48,310
payload object.

71
00:05:48,310 --> 00:05:51,490
So if you scroll down, the whole operation is the same.

72
00:05:51,490 --> 00:05:57,970
So the idea is that we will use the update item command and follow the same discipline and send this

73
00:05:57,970 --> 00:06:04,900
payload and to give these parameter variables to following the same element of the Dynamo DB API.

74
00:06:05,260 --> 00:06:10,810
So as you can see that we have successfully finished the update product operation and make sure that

75
00:06:10,810 --> 00:06:16,900
your code is identical with this code and we will see other methods in the upcoming video.
