1
00:00:00,270 --> 00:00:07,470
In this video we are going to develop lambda function for the incoming rest API, even json object to

2
00:00:07,470 --> 00:00:08,820
perform business logic.

3
00:00:09,060 --> 00:00:10,440
Let's get started.

4
00:00:10,800 --> 00:00:13,230
For that purpose, open the Visual Studio code.

5
00:00:13,230 --> 00:00:20,550
I have created a new folder specific folder for the lecture and I have added the our incoming event

6
00:00:20,880 --> 00:00:23,370
object for the rest API to the lambda function.

7
00:00:23,370 --> 00:00:31,140
We can see these images object and in the index file index file we have created the lambda function

8
00:00:31,230 --> 00:00:33,570
and now we will develop our lambda function code.

9
00:00:33,570 --> 00:00:37,740
So this is the basic implementation 100 method of lambda function.

10
00:00:37,740 --> 00:00:42,960
This function designed for the API driven invocations to lambda function.

11
00:00:42,960 --> 00:00:46,410
So this is starting with the logging incoming event.

12
00:00:46,410 --> 00:00:53,020
After that we have tried cache block and in the tribe log we are returning to 100 success certificate

13
00:00:53,040 --> 00:00:59,460
with the body information and in the case below we are logging the error and return vector 400 code

14
00:00:59,460 --> 00:01:00,660
and the error message.

15
00:01:00,660 --> 00:01:08,220
So this is designed for the API driven services equal to the base lambda for example, API gateway or

16
00:01:08,670 --> 00:01:12,030
function from the lambda function.

17
00:01:12,030 --> 00:01:12,420
Okay.

18
00:01:12,420 --> 00:01:15,750
Now we are going to implement the actual business logic in here.

19
00:01:15,750 --> 00:01:22,530
So in the actual logic, we will basically accommodate the incoming request from coming from the rest

20
00:01:22,530 --> 00:01:30,160
API and we will accommodate these HTTP methods and implement the product related business logic into

21
00:01:30,160 --> 00:01:31,710
the lambda function.

22
00:01:31,920 --> 00:01:36,660
So as you remember that or even JSON objects are stored in here.

23
00:01:36,660 --> 00:01:44,790
So basically if we examine these folders, the important attributes are resource, pet and static methods.

24
00:01:44,790 --> 00:01:51,870
For example, this is the gate product and is the gate product by ID and this is the post product.

25
00:01:51,870 --> 00:01:57,900
So the idea there is that we should write the switch block according to the HTTP method and after that

26
00:01:57,900 --> 00:02:01,080
we will check the query string and pet parameters.

27
00:02:01,080 --> 00:02:03,210
So let's get start to development.

28
00:02:03,240 --> 00:02:09,840
Basically, I'm going to open the indexes and in here I have writing the switch statement in the K,

29
00:02:10,020 --> 00:02:17,820
we should look at the event that HTTP method to verify this attribute.

30
00:02:17,820 --> 00:02:24,000
You should get in here and copy from attribute name and come to code window and paste in here.

31
00:02:24,600 --> 00:02:28,920
And if you look at the case, we should check the HTTP methods.

32
00:02:28,920 --> 00:02:36,950
If you look at the exit method values in here in the method for the get request, it is get get the

33
00:02:36,990 --> 00:02:37,380
ID.

34
00:02:37,380 --> 00:02:40,290
It is again the same get post method.

35
00:02:40,320 --> 00:02:41,580
The method is post.

36
00:02:41,580 --> 00:02:47,910
So that means we should write the HTTP method case, get post and delete.

37
00:02:48,000 --> 00:02:55,680
After that we will develop additional conditions for gate by ID looking for the setting parameters and

38
00:02:55,680 --> 00:02:56,760
the parameters.

39
00:02:56,940 --> 00:02:58,440
Okay, let's get started.

40
00:02:58,440 --> 00:03:03,570
I'm going to copy these cases and after that I will explain one by one.

41
00:03:08,110 --> 00:03:08,530
Here.

42
00:03:08,530 --> 00:03:12,190
You can find the old case statement for our application.

43
00:03:12,190 --> 00:03:16,520
So let me copy all and make it clear.

44
00:03:16,540 --> 00:03:17,140
Yes.

45
00:03:17,140 --> 00:03:19,480
Here you can point our switch statement.

46
00:03:19,510 --> 00:03:25,860
Basically, we handle all incoming requests from the rest API by looking to our ATP method.

47
00:03:25,870 --> 00:03:34,150
And after that, if the method is get, we have also additional condition, which is the looking query

48
00:03:34,150 --> 00:03:35,260
setting parameters.

49
00:03:35,290 --> 00:03:39,810
It took our setting parameters, including this event JSON object.

50
00:03:39,820 --> 00:03:42,550
We are going to perform event parameters in here.

51
00:03:42,610 --> 00:03:48,460
But if there is no query string, we are looking for the checking for the parameters and after that

52
00:03:48,460 --> 00:03:49,830
apply the parameters.

53
00:03:49,840 --> 00:03:52,210
For example, this is for get by ID.

54
00:03:52,240 --> 00:03:57,910
If there is no parameters and setting parameters, that means we will return back to get all products

55
00:03:57,910 --> 00:04:01,870
so you can check these HTTP methods.

56
00:04:01,870 --> 00:04:04,840
Which condition is handled, which method?

57
00:04:04,840 --> 00:04:10,990
You can see this, you can check the these command sections, for example, this one is handled get

58
00:04:10,990 --> 00:04:14,950
product by ID and also create a setting parameter.

59
00:04:14,950 --> 00:04:21,280
And after that we only check, get product ID and last condition is get all products.

60
00:04:21,280 --> 00:04:27,700
So this is the first case for the gate condition, another case for the OR application looking for the

61
00:04:27,700 --> 00:04:28,300
post.

62
00:04:28,300 --> 00:04:30,400
You can see in here this is the post.

63
00:04:30,400 --> 00:04:33,190
So in the post block I'm going to.

64
00:04:34,980 --> 00:04:36,800
Check the event, buddy.

65
00:04:36,810 --> 00:04:40,440
So we are basically passing the event the in order to get the payload.

66
00:04:40,470 --> 00:04:45,360
After that, we are processing the payload for inserting a new item and so on.

67
00:04:45,360 --> 00:04:48,510
So another case we look is the delete.

68
00:04:48,510 --> 00:04:50,920
So in the delete method, http delete.

69
00:04:51,540 --> 00:04:56,190
We basically check the parameters to delete which ID of the product.

70
00:04:56,190 --> 00:05:00,570
So this is handled delete product by ID, this is handled post product.

71
00:05:00,570 --> 00:05:06,930
So as you can see that we have implemented the switch block in accommodating the incoming events from

72
00:05:06,930 --> 00:05:08,220
the rest API.

73
00:05:08,250 --> 00:05:15,570
So now we should copy this line of codes and paste to our lambda function in order to deploy and see

74
00:05:15,570 --> 00:05:17,910
how it is working in our application.

75
00:05:17,910 --> 00:05:26,220
So let me make it more clean and copy all codes, remove blank characters and I am going to copy all

76
00:05:26,220 --> 00:05:26,790
code.

77
00:05:27,390 --> 00:05:35,490
You can also copy from this index file of the lecture folder and go to the our lambda function product

78
00:05:35,490 --> 00:05:42,090
function go to functions, product function, open the code window and remove this code window copy

79
00:05:42,090 --> 00:05:44,160
from here and paste in here.

80
00:05:44,160 --> 00:05:49,530
So you can also check the codes and verify all with the codes in this place.

81
00:05:49,530 --> 00:05:52,290
So after that you can click the deploy button.

82
00:05:52,440 --> 00:05:56,100
Now we are ready for the test of our application.

83
00:05:56,100 --> 00:06:02,250
So as you can see that we have developed our lambda function that can handle all request incoming from

84
00:06:02,250 --> 00:06:03,330
the rest API.
