1 00:00:01,190 --> 00:00:04,820 Now that we've got a request handler to get a list of all the different tracks that have been created 2 00:00:05,030 --> 00:00:08,720 we need to make sure that a user can also create a new track from scratch. 3 00:00:08,820 --> 00:00:12,970 So inside of our tracker out Scott J.S. file right underneath our existing right handler. 4 00:00:12,980 --> 00:00:15,220 We'll put together one last handler. 5 00:00:15,280 --> 00:00:20,120 We're going to say that anytime someone makes a POST request to slash tracks. 6 00:00:20,120 --> 00:00:24,830 We're going to run an async function inside of here. 7 00:00:24,830 --> 00:00:29,820 We're going to take some information from that post request and use it to create a brand new track. 8 00:00:29,840 --> 00:00:33,860 So the first thing we need to do is make an assumption about the information that will be inside of 9 00:00:33,860 --> 00:00:35,480 that request body. 10 00:00:35,480 --> 00:00:40,370 So remember inside of our track dot J.S. file we said that every track that gets created is going to 11 00:00:40,370 --> 00:00:46,790 have a name and some list of locations and each of those locations was going to have a timestamp and 12 00:00:46,790 --> 00:00:49,160 coords object associated with it. 13 00:00:49,190 --> 00:00:51,570 So we're just gonna make a total assumption right now. 14 00:00:51,590 --> 00:00:57,020 We're going to assume that when our mobile device makes a POST request to create a new track we're going 15 00:00:57,020 --> 00:01:02,390 to assume that it's going to send us a name property with the name of the track and then an array of 16 00:01:02,390 --> 00:01:06,150 point objects under the locations property. 17 00:01:06,150 --> 00:01:11,030 So locations will be an array of objects and every object will have a timestamp in that coords object 18 00:01:11,300 --> 00:01:16,340 with the latitude longitude heading speed accuracy and so on. 19 00:01:16,340 --> 00:01:20,570 So in other words we're going to assume that the mobile device is going to send us the exact same structure 20 00:01:20,570 --> 00:01:27,770 of data as we expect our model to have inside of our Mongo database so that means inside of here we 21 00:01:27,770 --> 00:01:31,130 can take the location. 22 00:01:31,140 --> 00:01:38,390 Well as do names and locations off of wreck dot body like so then after that we'll just do some very 23 00:01:38,390 --> 00:01:43,520 quick validation to make sure that we were given some list of groups not names but just name. 24 00:01:43,520 --> 00:01:44,330 There we go. 25 00:01:44,330 --> 00:01:48,050 So we'll make sure that we got a name and list of locations to store. 26 00:01:48,170 --> 00:01:57,280 So we'll say if there is no name or if there is no locations then we will immediately return and we'll 27 00:01:57,280 --> 00:02:03,250 do a red dot status of 422 to say Hey sorry but you sent something illegible to us something that doesn't 28 00:02:03,250 --> 00:02:04,480 make any sense. 29 00:02:04,900 --> 00:02:16,590 And we'll send back a message of air with you must provide a name and locations. 30 00:02:16,670 --> 00:02:19,800 Now we've got at least a little bit of error checking inside of you before we attempt to create the 31 00:02:19,800 --> 00:02:20,740 track. 32 00:02:20,760 --> 00:02:21,990 Now of course take notice. 33 00:02:22,080 --> 00:02:26,920 We're not actually asserting that name is a string or that locations is an array of objects. 34 00:02:27,270 --> 00:02:31,590 The Mongoose model that we created we'll do it at least a little bit of validation and make sure that 35 00:02:31,590 --> 00:02:34,470 we only pick up the appropriate properties here. 36 00:02:34,470 --> 00:02:40,470 So if a user provides us some garbage name or does not give us an array of objects for locations mongoose 37 00:02:40,470 --> 00:02:46,390 will kick in and say Hey sorry but you did something wrong here so after we attempt to do that little 38 00:02:46,390 --> 00:02:51,520 bit of validation will then take the name locations and use them to create a new track. 39 00:02:51,640 --> 00:02:52,360 So we'll do counts. 40 00:02:52,360 --> 00:02:56,770 Track is a new instance of a track inside this object. 41 00:02:56,770 --> 00:03:00,050 We're going to put the different properties that we want this track to have. 42 00:03:00,070 --> 00:03:01,600 So I want the name to be name. 43 00:03:01,840 --> 00:03:07,920 We can of course condense that down to just name and then locations will be locations and likewise they 44 00:03:07,960 --> 00:03:10,050 can be just locations. 45 00:03:10,190 --> 00:03:16,780 And then finally remember our track model needs to have the I.D. of the user who created this track. 46 00:03:16,800 --> 00:03:22,860 So that's how we're going to track who created which track so I need to make sure that I pass in a user 47 00:03:22,860 --> 00:03:24,270 I.D. here as well. 48 00:03:24,270 --> 00:03:30,450 Remember that we can get the current users I.D. out of our request object so we can do req dot user 49 00:03:30,570 --> 00:03:32,610 dot underscore I.D. like so 50 00:03:36,350 --> 00:03:39,710 then after we create that track we can attempt to save it to our database. 51 00:03:39,800 --> 00:03:41,210 So we'll do a wait. 52 00:03:41,210 --> 00:03:48,380 Track dot save and then assuming that that went OK we can then send back some kind of response. 53 00:03:48,410 --> 00:03:51,110 So right now we'll just send back the exact track that we created. 54 00:03:51,110 --> 00:03:55,370 So we'll do a resort send of track. 55 00:03:55,450 --> 00:03:58,720 Now remember when we call save right here we could fail validation. 56 00:03:58,750 --> 00:04:04,240 In other words if a user passed in a garbage list of locations that is not an array of objects or anything 57 00:04:04,240 --> 00:04:08,490 similar to that the track dot safe call right here is going to fail. 58 00:04:08,560 --> 00:04:16,170 So to make sure that we handle that appropriately we can wrap those three lines with a try case so we'll 59 00:04:16,170 --> 00:04:17,110 do a try. 60 00:04:17,180 --> 00:04:18,410 Close closed off. 61 00:04:18,510 --> 00:04:20,880 We'll do a catch catch that air. 62 00:04:20,910 --> 00:04:27,480 So then if something went wrong during the safe process we can once again again do a rez dot status 63 00:04:28,290 --> 00:04:32,800 for twenty two and we'll just once again send back the error message. 64 00:04:32,820 --> 00:04:36,920 So we'll do air is e r r dot message. 65 00:04:37,100 --> 00:04:42,300 Now as we discussed before it inside of our authorized file sending back this message right here directly 66 00:04:42,600 --> 00:04:44,810 might result in something that's not very user friendly. 67 00:04:45,120 --> 00:04:50,720 But once again kind of a simple API here so we're not gonna worry about it too much okay. 68 00:04:50,740 --> 00:04:53,200 So that should be all that is required to create a new track. 69 00:04:53,210 --> 00:04:55,840 So let's now do a little bit of testing here. 70 00:04:55,910 --> 00:04:59,730 I'm going to flip back over to post van. 71 00:04:59,920 --> 00:05:03,760 I'm going to reuse the same request that I just made a moment ago when I tried to get a list of all 72 00:05:03,760 --> 00:05:05,300 of our different tracks. 73 00:05:05,400 --> 00:05:12,700 It's going to change this thing to do a post request to slash tracks all then go to the body tab. 74 00:05:12,820 --> 00:05:18,780 I'm going to select raw and I need to put some Jason inside of here for the track that we want to create. 75 00:05:18,810 --> 00:05:22,100 So unfortunately we're going have to write out a decent amount of Jace on this time. 76 00:05:22,220 --> 00:05:27,350 So I'm gonna put in an object this thing will have a name and remember that's gonna be the name of our 77 00:05:27,350 --> 00:05:27,990 track. 78 00:05:28,040 --> 00:05:30,240 So it'll be like my new track. 79 00:05:30,650 --> 00:05:34,550 And then in addition we need to provide the locations array as well. 80 00:05:34,640 --> 00:05:39,590 So we have to provide a locations property that isn't array that has some different objects inside of 81 00:05:39,590 --> 00:05:41,430 it for this test case. 82 00:05:41,450 --> 00:05:46,910 We'll only put one object inside there but that object has to have a timestamp the coords object. 83 00:05:46,910 --> 00:05:52,930 And I remember inside the cords object we have to have latitude longitude and so on it's a back over 84 00:05:52,930 --> 00:05:59,270 here we'll put in locations that will be an array they'll have one object. 85 00:05:59,270 --> 00:06:01,240 So I'll put that object inside there. 86 00:06:01,460 --> 00:06:05,750 It's going to have the timestamp property and this thing is gonna be a number remembered the number 87 00:06:05,750 --> 00:06:10,250 of milliseconds since 1970 where some specific day on 1970. 88 00:06:10,250 --> 00:06:18,630 So right now I'll just put in like some big number like so then also put in the cords object so I'll 89 00:06:18,630 --> 00:06:20,240 put in cords. 90 00:06:20,400 --> 00:06:25,780 That's gonna be a nested object and remember this thing is going to have that big different list of 91 00:06:25,780 --> 00:06:26,940 properties. 92 00:06:26,980 --> 00:06:28,870 So this is where all the writing comes in. 93 00:06:28,870 --> 00:06:29,680 We're gonna do. 94 00:06:29,780 --> 00:06:30,520 Latitude 95 00:06:34,220 --> 00:06:38,170 and I'll do 100 a longitude. 96 00:06:38,400 --> 00:06:45,090 I'll do one hundred I'll do an altitude accuracy 97 00:06:48,360 --> 00:06:51,590 heading and a speed 98 00:06:54,910 --> 00:06:55,250 okay. 99 00:06:55,270 --> 00:06:59,480 As usual got to beg you here please double check your typing inside of here. 100 00:06:59,650 --> 00:07:04,780 If you run into any air as he starts to make this request a very easy way to validate the Jason he just 101 00:07:04,780 --> 00:07:11,050 typed out inside of here would be to copy all this stuff take it over to a browser window open up your 102 00:07:11,050 --> 00:07:21,660 console and then inside of the console we can do a quick Jason dot pass and then inside of a double 103 00:07:21,660 --> 00:07:27,040 coded story single quoted string by mistake single quoted string so we got to do back to X here because 104 00:07:27,040 --> 00:07:32,590 it's gonna be pasted as a multi line so we'll do tactics and that's better we'll pass him that big block 105 00:07:33,310 --> 00:07:35,740 and when we pass in that big block we should not see any error. 106 00:07:36,130 --> 00:07:39,810 If you see an error that means you've got a typo somewhere inside of your Jason. 107 00:07:39,860 --> 00:07:44,980 Also please make sure once again that you double check all those coords properties so have the correct 108 00:07:44,980 --> 00:07:49,650 spelling on accuracy altitude blah blah and so on. 109 00:07:49,650 --> 00:07:50,040 All right. 110 00:07:50,070 --> 00:07:54,360 So back over and post man we've now got some correct Jason data that we're trying to use to create our 111 00:07:54,750 --> 00:07:56,160 actual track. 112 00:07:56,160 --> 00:07:59,580 Now we need to make sure over on the headers tab that we've got our authorization set. 113 00:07:59,610 --> 00:08:00,850 Yep we do. 114 00:08:00,910 --> 00:08:05,610 And we also need to make sure that we tell our server that we're going to send it some Jason data so 115 00:08:05,610 --> 00:08:07,670 remember to do so inside of our header section. 116 00:08:07,770 --> 00:08:17,070 We'll also throw in a content type of application Jason and that should be at. 117 00:08:17,080 --> 00:08:18,960 Now we're ready to actually test this out. 118 00:08:19,220 --> 00:08:25,110 So I can hit send and I get back the track that was created there is all the data for it. 119 00:08:25,160 --> 00:08:27,260 So my track has the name. 120 00:08:27,260 --> 00:08:29,710 It's got an I.D. that's been assigned to it. 121 00:08:29,960 --> 00:08:34,630 And then I see the list of locations in our case we've just got one location. 122 00:08:34,790 --> 00:08:38,840 Notice how the location was given its own internal I.D. as well. 123 00:08:38,840 --> 00:08:41,840 That's because we created a separate schema for it. 124 00:08:41,930 --> 00:08:45,980 Any time we create a separate schema that object will be given its own I.D.. 125 00:08:46,340 --> 00:08:49,310 We've also got the user I.D. inside there as well. 126 00:08:49,390 --> 00:08:53,650 It's not the last thing we need to test out is make sure that we can now fetch that track. 127 00:08:53,650 --> 00:09:00,490 So to do so I'm going to once again create a new tab we'll make a get request to a local host 3000 slash 128 00:09:00,580 --> 00:09:06,970 tracks and we just need to very quickly once again throw in our Jason web token underneath the header 129 00:09:06,970 --> 00:09:07,990 section. 130 00:09:08,220 --> 00:09:15,800 So as a key I'll once again do authorisation I'll put in bearer and then I'll just go and fetch that 131 00:09:15,800 --> 00:09:17,120 Jason token once again. 132 00:09:17,120 --> 00:09:22,680 Very quickly so remember if you lost the token again you can always make another post request to slash 133 00:09:22,740 --> 00:09:23,400 sign in. 134 00:09:23,580 --> 00:09:30,110 Make sure you specify a body of raw input in your correct email and password so once I got that token 135 00:09:30,440 --> 00:09:32,440 I can then paste it right after Bear. 136 00:09:32,630 --> 00:09:36,670 And once again make sure you have bearer then a space then the token. 137 00:09:36,700 --> 00:09:38,590 So make this request. 138 00:09:38,590 --> 00:09:39,400 And there we go. 139 00:09:39,400 --> 00:09:43,750 So I've now got my list of different tracks that are tied to this user. 140 00:09:43,780 --> 00:09:45,600 So this is looking pretty good. 141 00:09:45,670 --> 00:09:48,340 I think we've just about got this API put together. 142 00:09:48,430 --> 00:09:49,870 So let's take a quick pause right here. 143 00:09:49,870 --> 00:09:55,150 When we come back the next video we'll start working on our actual react native app because hey that's 144 00:09:55,150 --> 00:09:56,200 what this is all about right. 145 00:09:56,770 --> 00:09:58,780 So great pause and I'll see you in just a minute.