1
00:00:00,510 --> 00:00:06,620
In this video, we are going to develop create product method for product number microservices.

2
00:00:06,630 --> 00:00:09,030
We are using SDK libraries.

3
00:00:09,120 --> 00:00:15,630
So as you remember that in the last video we have accommodate the HTTP gate method with using the gate

4
00:00:15,630 --> 00:00:17,390
item command and scan command.

5
00:00:17,400 --> 00:00:20,550
But in this video we will focus on the post method.

6
00:00:20,550 --> 00:00:25,290
So a client sent us a http post request and lambda function.

7
00:00:25,290 --> 00:00:30,660
Accommodate the http post request in order to create a new item on Dynamo DB table.

8
00:00:31,170 --> 00:00:33,420
So please open the pseudocode.

9
00:00:33,420 --> 00:00:41,760
And this time as you can see that our switch statement only designed by the case operation to accommodate

10
00:00:41,760 --> 00:00:43,440
http gate method.

11
00:00:43,440 --> 00:00:50,970
So this time we will add additional case block which will be accommodate the HTTP post method in order

12
00:00:50,970 --> 00:00:54,450
to create a new item for that purpose.

13
00:00:54,450 --> 00:00:55,830
I'm going to.

14
00:00:57,610 --> 00:01:00,890
Come here and write the case below.

15
00:01:00,970 --> 00:01:04,150
As you can see that we have write the case post.

16
00:01:04,150 --> 00:01:08,260
So that means the event method will be the post.

17
00:01:08,260 --> 00:01:15,160
And in that case we will come here and run the great product method with passing the event object.

18
00:01:15,160 --> 00:01:16,540
So what is the event object?

19
00:01:16,540 --> 00:01:21,700
Event object the exact same that under the event you can see the post station.

20
00:01:21,790 --> 00:01:29,440
The event object is exact same with this structure which is the method as a post and you can see the

21
00:01:29,440 --> 00:01:33,580
resource and pet information if the method is posed.

22
00:01:33,580 --> 00:01:39,580
In that case we have an additional attribute which is the body attribute and in the body attribute we

23
00:01:39,580 --> 00:01:47,860
have as a JSON item format in the product JSON object and we will insert this item into dynamic DB table.

24
00:01:47,950 --> 00:01:56,110
So if you come back to our index case now, we have accommodate the HTTP post operation and run the

25
00:01:56,110 --> 00:01:59,500
create product method with passing these events JSON object.

26
00:01:59,500 --> 00:02:05,890
And after that I have added a default statement, which means if there is a string method that we don't

27
00:02:05,890 --> 00:02:10,990
cover in our switch statement, we can throw an exception with unsupported trout.

28
00:02:11,410 --> 00:02:16,720
Okay, now we can focus on this HTTP post operation before we implement the method.

29
00:02:16,720 --> 00:02:22,120
It is good to understand the command that we will use during the create product operation.

30
00:02:22,120 --> 00:02:28,390
If you open the SDK library documentation, you can see in here we will use put item command to perform

31
00:02:28,390 --> 00:02:29,320
create item.

32
00:02:29,320 --> 00:02:36,520
If you scroll down, you can see the example usage and see the command object and go to the input parameters

33
00:02:36,520 --> 00:02:41,860
and see the parameters that we will provide when inserting any item on Dynamo DB table.

34
00:02:42,160 --> 00:02:48,340
So please go back to our indexes and now we are going to implement create product method with passing

35
00:02:48,340 --> 00:02:49,840
the event JSON object.

36
00:02:49,870 --> 00:02:56,620
That means we will pass the event JSON object to get the body information and pass this item information

37
00:02:56,620 --> 00:02:58,660
to our command parameters.

38
00:02:58,660 --> 00:03:06,280
If you scroll down and go at the end of the page, we basically create a method which name is create

39
00:03:06,280 --> 00:03:07,030
product.

40
00:03:07,030 --> 00:03:09,100
So I'm going to copy and paste.

41
00:03:09,100 --> 00:03:11,890
After that I will be explained one by one.

42
00:03:11,950 --> 00:03:18,610
Here you can see the our method, which is the great product and is the async method to wait for the

43
00:03:18,610 --> 00:03:20,710
dynamic DB client send operation.

44
00:03:20,710 --> 00:03:24,490
And this time we will get event object as a parameter.

45
00:03:24,490 --> 00:03:31,150
And this event object is in here which is the post JSON and we will see exactly the same structure in

46
00:03:31,150 --> 00:03:31,930
this file.

47
00:03:31,960 --> 00:03:38,680
And basically before explaining this method, let me add the required import statement.

48
00:03:38,680 --> 00:03:44,770
So let's see one by one, if there is a required package, we can add.

49
00:03:46,360 --> 00:03:52,130
For example, Marshall we have already added dynamic declined object and this time we will add the put

50
00:03:52,150 --> 00:03:52,690
item command.

51
00:03:52,690 --> 00:03:58,230
So please copy or import with clicking this button in our case.

52
00:03:58,240 --> 00:03:59,530
In my case it's not working.

53
00:03:59,530 --> 00:04:01,900
So that's why I have added manually.

54
00:04:03,640 --> 00:04:04,300
Okay.

55
00:04:04,300 --> 00:04:10,690
So make sure that you have edit put item command under the your client dynamo DB library import statement.

56
00:04:10,720 --> 00:04:17,800
If you go back to at the end of the method, you can see that the put item command is located in here.

57
00:04:17,830 --> 00:04:24,820
So if you go right click and go to definition, you can see the input parameters into the output item

58
00:04:24,850 --> 00:04:26,090
input object.

59
00:04:26,110 --> 00:04:31,390
We can see the table name, item, expected return modus and so on.

60
00:04:31,690 --> 00:04:34,480
Okay, so please go back to our method.

61
00:04:34,510 --> 00:04:36,790
Okay, let's explain the method.

62
00:04:37,150 --> 00:04:39,600
As you can see in here, we have a try case below.

63
00:04:39,640 --> 00:04:46,060
And in the first line of code we basically log the operation, which means we are in the create product

64
00:04:46,060 --> 00:04:49,030
method and also logging the incoming event JSON object.

65
00:04:49,120 --> 00:04:52,290
And after that, the important part is in here.

66
00:04:52,300 --> 00:04:57,310
So we basically pass even that body information and get the request body.

67
00:04:57,340 --> 00:05:04,120
So what is included in these event body information if you go to the our.

68
00:05:05,820 --> 00:05:06,680
Even to object.

69
00:05:06,690 --> 00:05:11,620
Let me close these files and go to the post JSON file under the events.

70
00:05:11,640 --> 00:05:15,180
You can see that in the body of this event.

71
00:05:15,360 --> 00:05:16,620
Some object here.

72
00:05:16,620 --> 00:05:23,700
We have a JSON object that is the item of the product table, including the ID as a passing key.

73
00:05:23,700 --> 00:05:28,230
A name and price is attributes of this product dynamo DB table.

74
00:05:28,230 --> 00:05:34,710
So that's why the first thing we should pass this JSON in our code and after that we get this request

75
00:05:34,710 --> 00:05:42,000
body and paste this request body to the item of the parameters of put item command object.

76
00:05:42,000 --> 00:05:48,750
So it is required for the put item command and these are the properties as a table name and item an

77
00:05:48,750 --> 00:05:52,830
item comes from the JSON parts of the event body object.

78
00:05:53,040 --> 00:05:56,970
So as you can see that we are using in here marginal operation.

79
00:05:56,970 --> 00:06:01,800
If you don't use Marshall operation, we have to specify the data types.

80
00:06:01,830 --> 00:06:03,090
Let me show in here.

81
00:06:03,120 --> 00:06:08,430
This should be the structure of the number and the string will be in here.

82
00:06:08,430 --> 00:06:15,570
But when we are using the Marshall, we can pass the whole JSON object and this will be handled all

83
00:06:15,570 --> 00:06:16,740
operations for us.

84
00:06:16,740 --> 00:06:26,550
So that's why I'm removing these old style item definition and come here, we basically create a request

85
00:06:26,550 --> 00:06:34,590
body information and paste these JSON object as a dynamic DB item into parameters with using Marshall

86
00:06:34,590 --> 00:06:36,870
Utility Libraries.

87
00:06:37,200 --> 00:06:37,830
Okay.

88
00:06:37,830 --> 00:06:43,260
And also, as you know, that table name comes from the environment variables that we set this information

89
00:06:43,260 --> 00:06:49,470
when creating lambda function, we can go to configurations and set the table name or any environment

90
00:06:49,470 --> 00:06:50,730
variable in that place.

91
00:06:50,730 --> 00:06:56,520
So that's why we get these information from that and set the parameters of table name in here.

92
00:06:56,520 --> 00:07:03,840
So after we have prepared the parameters of to put item command, we basically use put item command

93
00:07:03,870 --> 00:07:07,860
in order to insert any item into Dynamo DB table.

94
00:07:07,950 --> 00:07:14,840
So this is important statement and this is again a variable request to send Dynamo DB with using input

95
00:07:14,850 --> 00:07:15,660
item command.

96
00:07:15,690 --> 00:07:21,690
This will return to create result of this operation success or fail.

97
00:07:21,720 --> 00:07:26,760
And we basically log this result and return back to resort to the function response.

98
00:07:26,760 --> 00:07:33,690
And there is one thing we should consider very importantly is you remember that we are parsing the request

99
00:07:33,690 --> 00:07:41,550
body and these body information is becomes an item, but this item should be include some attributes

100
00:07:41,550 --> 00:07:42,930
from the post operation.

101
00:07:42,930 --> 00:07:49,050
You can see the body information, but these JSON objects, the item object of the product should be

102
00:07:49,050 --> 00:07:52,440
include primary K, which means the partition K.

103
00:07:52,440 --> 00:07:58,350
So for that purpose, let me open the documentation and see that we are using PWD Item Command and this

104
00:07:58,350 --> 00:08:03,840
command basically creates a new item or replace an old item with any item.

105
00:08:03,840 --> 00:08:10,190
So if we check the product dynamo, we definition, we have a product table and the ID is a partition

106
00:08:10,200 --> 00:08:13,800
K so you can come here and go to Dynamo DB.

107
00:08:14,250 --> 00:08:21,300
You can see that we have a product table and partition and primary key is ID information.

108
00:08:21,300 --> 00:08:27,780
So that means according to this point, put item command, if you send a same ID in several times,

109
00:08:27,780 --> 00:08:33,600
this will be updated and existing item and not create any item and not return any exception.

110
00:08:33,600 --> 00:08:36,540
So this is the important thing that you should know.

111
00:08:36,540 --> 00:08:42,840
Understand which command you are using and understand what is doing in this command.

112
00:08:42,870 --> 00:08:44,340
This is very important.

113
00:08:44,340 --> 00:08:51,540
So since we send the ID information, for example, in, let me open the OR post JSON.

114
00:08:51,540 --> 00:08:54,720
So basically we are sending this kind of JSON object, right?

115
00:08:54,720 --> 00:09:01,260
So since we send the ID information and use the put item command, this will replace our existing record.

116
00:09:01,410 --> 00:09:04,770
And we don't want to do this in order to solve this issue.

117
00:09:04,800 --> 00:09:11,250
We should not send the ID partition key information and partition key should be generated automatically

118
00:09:11,250 --> 00:09:13,560
with using UUID libraries.

119
00:09:13,560 --> 00:09:21,270
So that means if you come here and if you send the same ID information as a JSON body, this will be

120
00:09:21,270 --> 00:09:24,810
replaced our code and we don't want to do this kind of operation.

121
00:09:24,810 --> 00:09:32,310
We only would like to use a name information or create a new item on our table and we don't want to

122
00:09:32,340 --> 00:09:38,970
update existing ID record in order to avoid these kind of issues, we should generate these IDs, right?

123
00:09:39,950 --> 00:09:43,340
Generate with your ID library.

124
00:09:43,640 --> 00:09:50,120
So that means in the next video we will generate these ID information when creating any product on our

125
00:09:50,120 --> 00:09:51,050
dynamic DB.

126
00:09:51,080 --> 00:09:58,490
To avoid the overwrite operation, we use the generating the ID with you ID library.

127
00:09:59,270 --> 00:10:05,210
So in order to solve this issue, we should not send the ID information in the body operation.

128
00:10:05,210 --> 00:10:11,090
We will solve this issue with generating the ID with using you ID libraries.

129
00:10:11,090 --> 00:10:17,780
So in the next video we will see how we can use your ID library in order to generate product table partition

130
00:10:17,780 --> 00:10:18,560
k column.
