1
00:00:00,150 --> 00:00:05,010
In this video, we are going to refactor out 100 method of our lambda function.

2
00:00:05,010 --> 00:00:07,920
So we basically remove unnecessary.

3
00:00:08,910 --> 00:00:10,440
These command sanctions.

4
00:00:10,440 --> 00:00:16,110
And after that, if you scroll down, you can see that we are missing something very important.

5
00:00:16,140 --> 00:00:20,910
You can see that we are still returning response with such scope to 100.

6
00:00:20,940 --> 00:00:27,670
So even we handle all HTTP methods, we don't respond back to correct statement.

7
00:00:27,690 --> 00:00:32,610
Instead of that, it is still remaining to hardcoded hello message.

8
00:00:32,610 --> 00:00:35,450
So let's modify this return statement.

9
00:00:35,460 --> 00:00:40,980
We need to return correct codes according to current operations of our product line microservices.

10
00:00:41,010 --> 00:00:45,810
First of all, it is good to cover all switch statement into case block.

11
00:00:46,020 --> 00:00:49,590
As you can see that we don't have any case block in the our main method.

12
00:00:49,590 --> 00:00:52,710
So I'm going to create a new try case block.

13
00:00:52,830 --> 00:01:00,600
And in here in the trial, look, I'm going to copy and paste to the switch statement.

14
00:01:00,630 --> 00:01:04,680
You can copy this switch statement and paste into the trial.

15
00:01:04,680 --> 00:01:05,220
Look.

16
00:01:05,220 --> 00:01:06,600
Okay, very good.

17
00:01:06,600 --> 00:01:14,700
So if everything goes well, so that means in happy bit, we basically lock the body information.

18
00:01:14,700 --> 00:01:18,360
As you can see that we have created a body information.

19
00:01:18,360 --> 00:01:21,870
Of course, we need to create a body information at the beginning of the method.

20
00:01:21,870 --> 00:01:22,830
It is really important.

21
00:01:22,830 --> 00:01:26,040
Please don't forget this line of seven.

22
00:01:26,040 --> 00:01:33,390
So create a body body variable and after that we will set body variable with incoming response from

23
00:01:33,390 --> 00:01:34,860
the these methods.

24
00:01:34,860 --> 00:01:41,100
And after that, if everything goes well in happy pat in the tribal log, I'm going to log the body

25
00:01:41,100 --> 00:01:45,420
and I'm going to return a success response.

26
00:01:46,420 --> 00:01:50,410
So let me come here and write the actual code.

27
00:01:50,490 --> 00:01:50,970
Okay.

28
00:01:50,980 --> 00:01:56,620
So as you can see that after the switch statement, we are looking to buy the information which comes

29
00:01:56,620 --> 00:01:59,690
from the these methods that we develop in the last videos.

30
00:01:59,710 --> 00:02:03,880
And after that, we are returning the score to 100 success response.

31
00:02:03,880 --> 00:02:11,230
And in the body of these return objects, we have a message which is a successful message, and we have

32
00:02:11,230 --> 00:02:15,310
a body information which comes from these methods in the message we provide.

33
00:02:15,310 --> 00:02:21,730
The event estimated that we mean that we are performing the HTTP, get post, delete and put operation

34
00:02:21,730 --> 00:02:22,930
successfully finished.

35
00:02:23,050 --> 00:02:24,780
So this is the happy part.

36
00:02:24,790 --> 00:02:26,710
What about the worst part?

37
00:02:26,710 --> 00:02:34,090
Or if we get an exception in the cache block, we got the expression and console log the exception and

38
00:02:34,090 --> 00:02:37,870
also return statement into the API gateway.

39
00:02:37,900 --> 00:02:46,990
This is important because we basically need to respond back to API gateway synchronously when our lambda

40
00:02:46,990 --> 00:02:53,530
function is triggered synchronously from any of these resources, in our case, API gateway.

41
00:02:53,560 --> 00:02:59,910
If we get an exception in this line of quotes in the try block, we should response back to the service

42
00:02:59,920 --> 00:03:05,110
code 500 and but the information which includes the message errors stack and so on.

43
00:03:05,110 --> 00:03:09,670
So let me change this catch error as a E and log.

44
00:03:09,670 --> 00:03:16,390
The E is a error and the other message and the error that comes from the E that message E that stick.

45
00:03:17,820 --> 00:03:18,360
Okay.

46
00:03:18,360 --> 00:03:18,720
Very good.

47
00:03:18,720 --> 00:03:26,220
So now we can remove the old return statement and we have successfully finished our main 100 method.

48
00:03:26,940 --> 00:03:30,240
Now, as you can see that we have two return statement.

49
00:03:30,240 --> 00:03:31,750
One is intertribal.

50
00:03:31,750 --> 00:03:37,410
Look, this means successfully finished HTTP operation and the second is the cage block.

51
00:03:37,440 --> 00:03:44,520
If something goes wrong, we send failure to perform Operation Message B to turn it 500 such code and

52
00:03:44,520 --> 00:03:46,860
detail information in the error message stack.

53
00:03:47,310 --> 00:03:48,900
The important thing is here.

54
00:03:48,900 --> 00:03:54,770
When you take link from API Gateway with synchronously, then you must return such code and the body

55
00:03:54,780 --> 00:03:56,310
object into the response.

56
00:03:56,460 --> 00:04:00,270
As you can see that we return such code and body both to site.

57
00:04:00,300 --> 00:04:06,330
Otherwise a the Willis Amazon API gateway don't show response properly, so please be careful about

58
00:04:06,330 --> 00:04:11,160
the response object when you are working with the synchronous invocations on lambda functions.

59
00:04:11,300 --> 00:04:11,630
Okay.

60
00:04:11,640 --> 00:04:13,820
Now we are ready to test our application.

61
00:04:13,830 --> 00:04:16,230
Before that let's all weave our codes.

62
00:04:16,710 --> 00:04:20,630
So if you go on top of the page, we have import statement.

63
00:04:20,640 --> 00:04:26,370
The first import statement includes almost all commands about the CRUD operations from the Dynamo DB

64
00:04:26,400 --> 00:04:27,480
SDK package.

65
00:04:27,510 --> 00:04:34,590
And after that we have Marshall and Marshall operations from the Dynamic DB Library to simplify these

66
00:04:34,590 --> 00:04:37,050
key definitions of the Dynamo DB table.

67
00:04:37,050 --> 00:04:41,190
And we have of course, Dynamo DB client object is a separate Node.js module.

68
00:04:41,340 --> 00:04:46,530
And here you can see the our main method of our product, Microsoft Lambda function.

69
00:04:46,530 --> 00:04:52,800
In the handle method, we basically have a tri cage block and after that we have a switch statement

70
00:04:52,800 --> 00:04:55,650
and accommodate all HTTP requests.

71
00:04:56,070 --> 00:05:03,600
We get post delete and put request and in the HTTP get request we are checking to create parameters,

72
00:05:03,810 --> 00:05:06,900
parameters and the all get all products.

73
00:05:06,900 --> 00:05:09,150
So in here we perform query command.

74
00:05:09,150 --> 00:05:16,320
In here we perform scan command and in here we are getting product by ID, get one single product,

75
00:05:16,320 --> 00:05:18,870
that means we will use get item command in here.

76
00:05:18,960 --> 00:05:21,690
So if you scroll down, you can see the post operation.

77
00:05:21,690 --> 00:05:28,380
We have a great product we use put item command in here in the delete method, we use delete item command

78
00:05:28,380 --> 00:05:35,370
with passing the parameters ID and we have an update product we will use again, we will use in here

79
00:05:35,370 --> 00:05:40,650
update item command with passing to parameters, we create an update product, we are passing the whole

80
00:05:40,680 --> 00:05:41,340
event object.

81
00:05:41,340 --> 00:05:48,660
And inside of this method we are passing incoming body information that we expect the body of the product

82
00:05:48,660 --> 00:05:50,790
data into Dynamo DB table.

83
00:05:50,790 --> 00:05:57,030
So you can see that after the success response, we are returning the success certificate to 100 and

84
00:05:57,030 --> 00:05:59,130
success message and the body information.

85
00:05:59,250 --> 00:06:05,430
If something goes wrong, we are returning the error message to the service code and the body information

86
00:06:05,430 --> 00:06:11,300
and if you scroll down you can see the all method implementation in here that we will cover all CRUD

87
00:06:11,340 --> 00:06:17,460
operation and we perform all method into the product microservices lambda function.

88
00:06:17,490 --> 00:06:22,890
In the next video we are going to deploy our product lambda function and after that test all architecture.
