1
00:00:00,690 --> 00:00:11,190
Now we're going to write the code for our rest API endpoints also call these routes we are going to

2
00:00:11,190 --> 00:00:14,000
do this inside the app store.

3
00:00:14,040 --> 00:00:25,920
Jay S-5 said that's in start AWOS STK with save flag will also install some utility modules like moment

4
00:00:26,040 --> 00:00:33,060
underscore and you ID will need this more use for some operations.

5
00:00:59,150 --> 00:01:07,460
And then we reference the ADA vs SDK and instantiate an object of dynamically read document client class

6
00:01:07,610 --> 00:01:08,500
as usual

7
00:01:24,970 --> 00:01:34,650
the Express router has various methods available like get post patch delete that we can use to process

8
00:01:34,740 --> 00:01:40,040
incoming UDP requests with the same name like SCDP.

9
00:01:40,040 --> 00:01:44,470
Get to the post patch delayed and so on.

10
00:01:44,530 --> 00:01:55,110
So for a moment let's think about what API requests are no taking and are is likely to make on behalf

11
00:01:55,110 --> 00:01:56,550
of its users

12
00:01:59,160 --> 00:02:04,650
we might need a way for users to create a new node.

13
00:02:04,980 --> 00:02:15,060
So we would need a Strout to insert a node item and animo the table when users log into the app.

14
00:02:15,090 --> 00:02:19,970
They should see all the notes that they previously created.

15
00:02:20,010 --> 00:02:29,780
So we also need to get route to get all the notes created by the user who has logged in a user might

16
00:02:29,780 --> 00:02:33,640
want to view or edit an existing node.

17
00:02:33,740 --> 00:02:44,720
So we need to get around to return a single note and then patch route to a date that node or to save

18
00:02:44,720 --> 00:02:52,930
changes and finally user might also want to remove it if they dont need it anymore.

19
00:02:52,940 --> 00:03:00,700
So we also need a delete route to delete the selected No.

20
00:03:00,850 --> 00:03:05,120
So let's go ahead and create these API endpoints.

21
00:03:05,200 --> 00:03:12,980
All right let's begin with post-draft to add a new note.

22
00:03:13,090 --> 00:03:18,880
So we're out door post and then we specify the end point.

23
00:03:18,880 --> 00:03:32,090
You are a let's say slash API slash node and then a function that takes a request response and next

24
00:03:32,420 --> 00:03:43,580
object's request is an incoming request object response is the object that we send out as SATB response

25
00:03:43,670 --> 00:03:52,590
and next is a function that can pass control to the next request response processing block.

26
00:03:52,820 --> 00:04:03,320
If there is any sense this function of ours will return the final response back to the calling application.

27
00:04:03,650 --> 00:04:12,070
We don't have the makings of the next function here as there is no processing block after this.

28
00:04:12,080 --> 00:04:22,070
So inside this function we are simply going to populate the parents object required by dynamically put

29
00:04:22,160 --> 00:04:25,230
item operation and call the put.

30
00:04:25,250 --> 00:04:30,040
I am using the document client object.

31
00:04:30,650 --> 00:04:41,330
Let's declare a new variable item to hold item data received from the calling application and this will

32
00:04:41,330 --> 00:04:46,090
be a rebel in the request dot body object.

33
00:04:46,160 --> 00:04:54,390
Since this is a post request so request dot and body dot item for example.

34
00:04:54,650 --> 00:05:05,720
Now we are going to have the Colling application send us only the node title content and category and

35
00:05:05,720 --> 00:05:09,500
will populate the rest of the attributes ourselves.

36
00:05:09,620 --> 00:05:18,160
We don't want to give the end user or the application the ability to alter user id attribute.

37
00:05:18,380 --> 00:05:28,280
This way we allow the users to add notes against their own User ID and prevent them from adding theirs

38
00:05:28,280 --> 00:05:31,910
with the user IDs of other users.

39
00:05:32,540 --> 00:05:41,840
This is not a foolproof access control though and will fix it with fine grained access control later

40
00:05:41,840 --> 00:05:49,490
when we implement Federated access server inside the item variable.

41
00:05:49,490 --> 00:05:59,560
We now have data for our title content and get Taggerty let's populate the remaining variables as well.

42
00:06:00,410 --> 00:06:09,940
I'm going to hard code the user id as test underscore user and user name as Test User

43
00:06:13,090 --> 00:06:21,660
later on when we implement user authentication in our app will pause the actual data here.

44
00:06:25,590 --> 00:06:33,220
Then to populate node ID I'm going to use the UI you are the module Lysa.

45
00:06:33,720 --> 00:06:36,530
This will generate random you you are.

46
00:06:36,570 --> 00:06:41,670
They are universally unique identifier.

47
00:06:41,820 --> 00:06:50,580
In other words this will generate a fairly unique random string every time so it will always have a

48
00:06:50,580 --> 00:06:56,400
new note for every new note that we create.

49
00:06:56,400 --> 00:07:07,080
I'm also going to prefix this random string with the user id like user id concatenated with the union

50
00:07:07,080 --> 00:07:11,990
ID and separated by a colon character.

51
00:07:13,850 --> 00:07:22,220
I'm doing this so when we query the items from the secondary index and the secondary indexes on their

52
00:07:22,280 --> 00:07:25,270
ID attribute in this case.

53
00:07:25,670 --> 00:07:36,480
So when we query the items from the secondary index we should have a way to identify the user from the

54
00:07:36,480 --> 00:07:44,870
next key and this is going to allow us to set up fine grained access control on this secondary index

55
00:07:46,490 --> 00:07:56,940
and will do this later when we set up the Federated access using cognito time Stan Williams moment dog

56
00:07:56,940 --> 00:07:58,340
you're next as usual

57
00:08:00,920 --> 00:08:10,180
and for the expires attribute I'm going to use that time stamp 90 days in the future just as an example.

58
00:08:14,510 --> 00:08:23,640
Then we simply call the put method to insert the item into the dining table table the table name is

59
00:08:23,640 --> 00:08:24,120
TV.

60
00:08:24,120 --> 00:08:31,330
Note that we are recreated and I'm going to declare it as a constant.

61
00:08:31,740 --> 00:08:36,100
We can reuse it across all our route definitions.

62
00:09:00,160 --> 00:09:09,080
If there is an error we console on the error object and return back the responses when the corresponding

63
00:09:09,080 --> 00:09:11,540
status code and message

64
00:09:31,870 --> 00:09:33,020
else really.

65
00:09:33,060 --> 00:09:36,650
UDP 200 to denote a successful operation

66
00:09:42,620 --> 00:09:43,970
and that's it.

67
00:09:44,150 --> 00:09:54,410
Let's test this out using postman.

68
00:09:54,500 --> 00:10:05,480
I'm going to use as GTP post request and our API endpoint as local host column 3000 followed by a slash

69
00:10:05,570 --> 00:10:08,660
API slash note.

70
00:10:09,650 --> 00:10:19,250
And in the request by the way RS Jason's ring with item attribute containing title content and category

71
00:10:46,590 --> 00:10:48,190
just like that

72
00:10:51,670 --> 00:10:52,840
I'm going to save this

73
00:11:02,550 --> 00:11:07,110
and then hit send to maybe post request card.

74
00:11:07,650 --> 00:11:10,230
We see 200 OK response.

75
00:11:10,230 --> 00:11:23,290
This means the operation was successful and we should see a new item inserted in the alimony.

76
00:11:23,540 --> 00:11:25,340
And we do.

77
00:11:25,850 --> 00:11:32,810
Let's create a few more such items by fighting this API call a few more times.

78
00:11:39,830 --> 00:11:50,810
All right the next round is SATB patch operation to allow users to date existing notes for this we could

79
00:11:50,810 --> 00:11:59,660
use the same root operation but this time we want the users to tell us the node ID and the time stamp

80
00:11:59,750 --> 00:12:09,080
of the original node that they wish to update timestep will allow us to locate their existing node using

81
00:12:09,080 --> 00:12:15,390
the combination of user ID and time span as the primary key.

82
00:12:16,010 --> 00:12:20,020
And then we can replace it with the new data.

83
00:12:20,510 --> 00:12:27,480
We do not want to change the node ID this time so we asked the end users to parse the original node

84
00:12:27,560 --> 00:12:31,590
ID as well in the rest.

85
00:12:31,610 --> 00:12:39,230
So I'm simply going to copy all the code from the previous route and it just it slightly

86
00:12:41,880 --> 00:12:53,720
we get the item from request body then populate the user id username and the expires attribute just

87
00:12:53,720 --> 00:12:56,170
as we did the previous route.

88
00:12:57,630 --> 00:13:04,770
I'm going to remove the code that populates node ID and times that as now we are going to receive it

89
00:13:04,770 --> 00:13:06,820
from the end user.

90
00:13:07,500 --> 00:13:12,660
And then we simply call the document client port operation.

91
00:13:12,700 --> 00:13:18,390
At this time we need to perform a conditional write operation.

92
00:13:18,390 --> 00:13:28,140
We should first check if the item with the given sarky exists and updated only if it does exist.

93
00:13:29,350 --> 00:13:35,650
We do not want to create a new item if the users sensing a wrong time stamp.

94
00:13:35,860 --> 00:13:43,020
So it's a good idea to check if the if the time stamp exists or not.

95
00:13:43,060 --> 00:13:51,190
So I'm going to use the conditional expression hash t equal to Colon T.

96
00:13:51,220 --> 00:13:59,500
And then define these variables using expression attribute names an expression attribute ralliers

97
00:14:12,930 --> 00:14:17,540
the time stamp value will be available in the item variable.

98
00:14:19,580 --> 00:14:30,120
And rest of the page should remain the same just like the previous book operation.

99
00:14:30,120 --> 00:14:31,950
Let's test this out.

100
00:14:37,650 --> 00:15:01,080
I'm going to create a patsy request like so.

101
00:15:01,330 --> 00:15:10,330
And then let's copy the times tab and note Id from an existing item from our Dynamo the table

102
00:15:14,800 --> 00:15:20,090
changed the title and current and slightly saved this

103
00:15:23,270 --> 00:15:25,760
and send the request

104
00:15:30,360 --> 00:15:32,730
and it returned 200.

105
00:15:32,790 --> 00:15:33,770
OK.

106
00:15:35,150 --> 00:15:46,340
And if we look at the dynamo D-B console we do see that the change is reflecting here as well.

107
00:15:48,200 --> 00:15:54,410
Now let's see what happens if we pass and non-existent time stamp.

108
00:15:54,410 --> 00:16:04,160
So let's change the time stamp here and find another regressed and we see 400 bad request with an error

109
00:16:04,160 --> 00:16:08,630
message saying conditional request failed.

110
00:16:08,990 --> 00:16:12,790
So we can only update existing items.

111
00:16:12,920 --> 00:16:13,790
Awesome.

112
00:16:14,510 --> 00:16:17,970
Let's continue building the API in the next media.
