Now that we've got a request handler to get a list of all the different tracks that have been created
we need to make sure that a user can also create a new track from scratch.
So inside of our tracker out Scott J.S. file right underneath our existing right handler.
We'll put together one last handler.
We're going to say that anytime someone makes a POST request to slash tracks.
We're going to run an async function inside of here.
We're going to take some information from that post request and use it to create a brand new track.
So the first thing we need to do is make an assumption about the information that will be inside of
that request body.
So remember inside of our track dot J.S. file we said that every track that gets created is going to
have a name and some list of locations and each of those locations was going to have a timestamp and
coords object associated with it.
So we're just gonna make a total assumption right now.
We're going to assume that when our mobile device makes a POST request to create a new track we're going
to assume that it's going to send us a name property with the name of the track and then an array of
point objects under the locations property.
So locations will be an array of objects and every object will have a timestamp in that coords object
with the latitude longitude heading speed accuracy and so on.
So in other words we're going to assume that the mobile device is going to send us the exact same structure
of data as we expect our model to have inside of our Mongo database so that means inside of here we
can take the location.
Well as do names and locations off of wreck dot body like so then after that we'll just do some very
quick validation to make sure that we were given some list of groups not names but just name.
There we go.
So we'll make sure that we got a name and list of locations to store.
So we'll say if there is no name or if there is no locations then we will immediately return and we'll
do a red dot status of 422 to say Hey sorry but you sent something illegible to us something that doesn't
make any sense.
And we'll send back a message of air with you must provide a name and locations.
Now we've got at least a little bit of error checking inside of you before we attempt to create the
track.
Now of course take notice.
We're not actually asserting that name is a string or that locations is an array of objects.
The Mongoose model that we created we'll do it at least a little bit of validation and make sure that
we only pick up the appropriate properties here.
So if a user provides us some garbage name or does not give us an array of objects for locations mongoose
will kick in and say Hey sorry but you did something wrong here so after we attempt to do that little
bit of validation will then take the name locations and use them to create a new track.
So we'll do counts.
Track is a new instance of a track inside this object.
We're going to put the different properties that we want this track to have.
So I want the name to be name.
We can of course condense that down to just name and then locations will be locations and likewise they
can be just locations.
And then finally remember our track model needs to have the I.D. of the user who created this track.
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
I.D. here as well.
Remember that we can get the current users I.D. out of our request object so we can do req dot user
dot underscore I.D. like so
then after we create that track we can attempt to save it to our database.
So we'll do a wait.
Track dot save and then assuming that that went OK we can then send back some kind of response.
So right now we'll just send back the exact track that we created.
So we'll do a resort send of track.
Now remember when we call save right here we could fail validation.
In other words if a user passed in a garbage list of locations that is not an array of objects or anything
similar to that the track dot safe call right here is going to fail.
So to make sure that we handle that appropriately we can wrap those three lines with a try case so we'll
do a try.
Close closed off.
We'll do a catch catch that air.
So then if something went wrong during the safe process we can once again again do a rez dot status
for twenty two and we'll just once again send back the error message.
So we'll do air is e r r dot message.
Now as we discussed before it inside of our authorized file sending back this message right here directly
might result in something that's not very user friendly.
But once again kind of a simple API here so we're not gonna worry about it too much okay.
So that should be all that is required to create a new track.
So let's now do a little bit of testing here.
I'm going to flip back over to post van.
I'm going to reuse the same request that I just made a moment ago when I tried to get a list of all
of our different tracks.
It's going to change this thing to do a post request to slash tracks all then go to the body tab.
I'm going to select raw and I need to put some Jason inside of here for the track that we want to create.
So unfortunately we're going have to write out a decent amount of Jace on this time.
So I'm gonna put in an object this thing will have a name and remember that's gonna be the name of our
track.
So it'll be like my new track.
And then in addition we need to provide the locations array as well.
So we have to provide a locations property that isn't array that has some different objects inside of
it for this test case.
We'll only put one object inside there but that object has to have a timestamp the coords object.
And I remember inside the cords object we have to have latitude longitude and so on it's a back over
here we'll put in locations that will be an array they'll have one object.
So I'll put that object inside there.
It's going to have the timestamp property and this thing is gonna be a number remembered the number
of milliseconds since 1970 where some specific day on 1970.
So right now I'll just put in like some big number like so then also put in the cords object so I'll
put in cords.
That's gonna be a nested object and remember this thing is going to have that big different list of
properties.
So this is where all the writing comes in.
We're gonna do.
Latitude
and I'll do 100 a longitude.
I'll do one hundred I'll do an altitude accuracy
heading and a speed
okay.
As usual got to beg you here please double check your typing inside of here.
If you run into any air as he starts to make this request a very easy way to validate the Jason he just
typed out inside of here would be to copy all this stuff take it over to a browser window open up your
console and then inside of the console we can do a quick Jason dot pass and then inside of a double
coded story single quoted string by mistake single quoted string so we got to do back to X here because
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
and when we pass in that big block we should not see any error.
If you see an error that means you've got a typo somewhere inside of your Jason.
Also please make sure once again that you double check all those coords properties so have the correct
spelling on accuracy altitude blah blah and so on.
All right.
So back over and post man we've now got some correct Jason data that we're trying to use to create our
actual track.
Now we need to make sure over on the headers tab that we've got our authorization set.
Yep we do.
And we also need to make sure that we tell our server that we're going to send it some Jason data so
remember to do so inside of our header section.
We'll also throw in a content type of application Jason and that should be at.
Now we're ready to actually test this out.
So I can hit send and I get back the track that was created there is all the data for it.
So my track has the name.
It's got an I.D. that's been assigned to it.
And then I see the list of locations in our case we've just got one location.
Notice how the location was given its own internal I.D. as well.
That's because we created a separate schema for it.
Any time we create a separate schema that object will be given its own I.D..
We've also got the user I.D. inside there as well.
It's not the last thing we need to test out is make sure that we can now fetch that track.
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
tracks and we just need to very quickly once again throw in our Jason web token underneath the header
section.
So as a key I'll once again do authorisation I'll put in bearer and then I'll just go and fetch that
Jason token once again.
Very quickly so remember if you lost the token again you can always make another post request to slash
sign in.
Make sure you specify a body of raw input in your correct email and password so once I got that token
I can then paste it right after Bear.
And once again make sure you have bearer then a space then the token.
So make this request.
And there we go.
So I've now got my list of different tracks that are tied to this user.
So this is looking pretty good.
I think we've just about got this API put together.
So let's take a quick pause right here.
When we come back the next video we'll start working on our actual react native app because hey that's
what this is all about right.
So great pause and I'll see you in just a minute.