1
00:00:00,810 --> 00:00:05,930
We are all done with the first goal of our API, so we are correctly serving up Riak project files.

2
00:00:06,720 --> 00:00:11,520
Now going to move on to steps two and three, which are essentially to retrieve a list of cells out

3
00:00:11,520 --> 00:00:15,540
of a stored file and add a list of cells into an existing file as well.

4
00:00:16,350 --> 00:00:18,910
These are going to be pretty easy and straightforward routes to put together.

5
00:00:19,110 --> 00:00:24,210
Really just going to receive some requests, pull some information out of the request, and then save

6
00:00:24,210 --> 00:00:29,250
some data into a file using the first module, which is a node standard library module.

7
00:00:29,830 --> 00:00:32,119
So let's get to it back inside my editor.

8
00:00:32,520 --> 00:00:35,160
I'm going to find my local API project inside.

9
00:00:35,160 --> 00:00:39,570
There, I'll find the Sarsae directory and I'll make a new folder inside there called Route's.

10
00:00:40,670 --> 00:00:44,540
Then inside of roots, I'll make a new file called Cels Dots.

11
00:00:46,340 --> 00:00:48,650
Inside of here, I'm going to first express.

12
00:00:49,640 --> 00:00:50,660
From Xpress.

13
00:00:51,590 --> 00:00:53,390
I'll then create an express router.

14
00:00:54,670 --> 00:00:59,320
If you're not familiar with the router, it is extremely similar to the app object that we're currently

15
00:00:59,320 --> 00:01:01,330
working with inside of our index not file.

16
00:01:01,630 --> 00:01:03,140
So that express app right there.

17
00:01:03,820 --> 00:01:10,030
So a router has methods assigned to it, like get post, delete does used to wire middleware and so

18
00:01:10,030 --> 00:01:10,210
on.

19
00:01:11,050 --> 00:01:15,400
So we're going to add a couple of routes onto this router object and then merge this router back into

20
00:01:15,400 --> 00:01:17,980
our app over inside of our indexed dots file.

21
00:01:19,650 --> 00:01:21,760
So we will first set up one or two different routes.

22
00:01:21,810 --> 00:01:27,600
Let's do a router dot get cells, we'll have that B async await.

23
00:01:28,860 --> 00:01:36,510
And then a function to run and we'll do a router post two cells that will also be an async function.

24
00:01:39,610 --> 00:01:40,370
OK, looks good.

25
00:01:40,930 --> 00:01:43,840
Let's receive our request in response objects on both them.

26
00:01:45,570 --> 00:01:49,680
And then inside of both these, let's just write a couple of comments out just to guide ourselves and

27
00:01:49,680 --> 00:01:54,360
make sure that we can kind of consider a couple of different scenarios to first off, in the case of

28
00:01:54,360 --> 00:01:55,770
getting a list of cells.

29
00:01:56,040 --> 00:02:00,630
The first thing we probably need to do inside of here is make sure that the file that is going to store

30
00:02:00,630 --> 00:02:02,430
a list of cells actually exists.

31
00:02:02,990 --> 00:02:08,430
So we're going to make sure the cell storage file exists.

32
00:02:08,820 --> 00:02:13,080
Remember, whenever a user starts up our supply, they're going to optionally provide us the name of

33
00:02:13,080 --> 00:02:17,590
the file that they want to use for storing cells if that file doesn't exist.

34
00:02:17,730 --> 00:02:19,680
We should probably create it for our users.

35
00:02:20,160 --> 00:02:22,860
So step one right here is to just make sure that file exists.

36
00:02:22,860 --> 00:02:27,390
If it does not, we'll create it for our users and maybe put in some default content into it as well.

37
00:02:27,840 --> 00:02:29,120
Let's put in a comment for that as well.

38
00:02:29,130 --> 00:02:38,220
So I'll put in if the file or if it does not exist, add in a default list of cells.

39
00:02:39,910 --> 00:02:44,200
And after that, once we are 100 percent sure that this file exists, we will read the file.

40
00:02:45,850 --> 00:02:49,180
Here's a list of cells out of it.

41
00:02:50,230 --> 00:02:55,630
And then finally send list of cells back to Browsr.

42
00:02:57,220 --> 00:02:59,260
That's pretty much it should be pretty straightforward.

43
00:03:00,340 --> 00:03:03,380
Our post method down here will be pretty straightforward as well.

44
00:03:04,030 --> 00:03:04,930
So in this case.

45
00:03:05,830 --> 00:03:08,500
We probably want to just make sure once again that this file exists.

46
00:03:11,720 --> 00:03:16,910
It is entirely possible that a user might delete the file in between save attempts or maybe rename it

47
00:03:16,910 --> 00:03:19,970
or something like that, so we might want to make sure that the file exists.

48
00:03:20,510 --> 00:03:21,530
And if it does not.

49
00:03:22,850 --> 00:03:23,750
We will create it.

50
00:03:24,960 --> 00:03:32,340
After that, we're going to take the list of cells from the request object because remember, our assumption

51
00:03:32,340 --> 00:03:36,300
here is that a user is going to be saving or sending us a list of cells that they want to persist.

52
00:03:37,390 --> 00:03:42,280
We're going to take the list of cells from the request object and we're going to serialize them or in

53
00:03:42,280 --> 00:03:48,160
other words, turn them into a format that can be safely written into that file, will then write the

54
00:03:48,160 --> 00:03:48,820
cells.

55
00:03:49,980 --> 00:03:50,910
Into the file.

56
00:03:51,820 --> 00:03:52,930
And that's pretty much it.

57
00:03:55,450 --> 00:03:59,320
So both these handlers are going to be pretty straightforward in nature, and the one thing I want to

58
00:03:59,320 --> 00:04:01,930
mention here that's just a little bit not standard.

59
00:04:02,390 --> 00:04:07,630
Remember, back inside of our inducts file, our search function has been given the file name and the

60
00:04:07,630 --> 00:04:09,010
directory that file citzen.

61
00:04:09,250 --> 00:04:14,290
So we really need to somehow communicate these two variables over to this cels router.

62
00:04:14,680 --> 00:04:19,600
So to do so, we're going to wrap up all this router creation stuff and all the router configuration

63
00:04:19,600 --> 00:04:20,589
in a function.

64
00:04:20,920 --> 00:04:25,330
And we're going to make sure that that function will eventually be coupled with the filename and directory

65
00:04:25,480 --> 00:04:27,990
that should be used for all this file related stuff.

66
00:04:29,650 --> 00:04:38,260
So right above construed right here, we're going to add in and export to create sales Rohter.

67
00:04:41,400 --> 00:04:45,960
I'll then place the closing curly brace on the very bottom line of the file.

68
00:04:47,080 --> 00:04:49,540
So all this stuff is going to go into the body of that function.

69
00:04:52,640 --> 00:04:58,550
Well, then make sure that we provide the file name as a string and the directory name as a string.

70
00:05:03,300 --> 00:05:08,250
So then finally, let's go back over to our next thoughts while we're going to import cells, Rueter,

71
00:05:08,550 --> 00:05:12,210
we're going to create the router and when we do, we'll make sure we pass in our filename and directory

72
00:05:12,540 --> 00:05:15,090
will then make sure we return the router at the bottom of this function.

73
00:05:15,120 --> 00:05:16,080
Let's do that very quickly.

74
00:05:16,110 --> 00:05:21,180
So down here, I'm going to return the router and we'll make sure we wire that up to our express app.

75
00:05:22,590 --> 00:05:32,730
Let's go back over to index at the very top, we will import create cells, rueter from route's cells.

76
00:05:37,180 --> 00:05:42,550
And then right above our existing return statement will do an app not use, that's how we wire up an

77
00:05:42,550 --> 00:05:48,340
express router, create sells router, we're going to call that function and we'll make sure we provide

78
00:05:48,670 --> 00:05:54,220
the filename and the directory and remember file name and directory arguments to our search function.

79
00:05:54,490 --> 00:05:55,360
They are right there.

80
00:05:56,980 --> 00:06:01,480
OK, so it looks good now all you have to do is a pretty good amount of work around these two different

81
00:06:01,480 --> 00:06:03,580
handlers and we should be all set.

