[MUSIC]
>> Hi there. I'm Christopher Harrison,
Senior Program Manager inside of Microsoft Cloud and AI.
What I want to take a look at in this section is how you can allow
a client to send information up to the server to create new items.
Now, when you're talking about a REST endpoint,
REST is typically going to define
the different operations available
by the different methods or verbs.
To create new items,
traditionally, this is done with POST.
Now the other big thing about POST is that
you'll send information up inside of the body.
Typically, the first thing that you'll do is go
grab the body from that request object,
because that's going to contain the JSON payload that
was sent up in the bodyParser that we configured earlier,
will automatically convert the body that they
sent up into JSON for us.
Now, we always need to validate our data,
and this is going to take a little bit of code,
so to simplify things,
I'm going to use some copy and paste.
The first thing that we're going to look to see
is that we got a user and a currency.
If we didn't, we're going to send out
a 400 error saying that those items are required.
The next thing that we want do is to ensure
that the the account does not exist.
We're going to now look at that
and if it turns out that there is an account with that name,
then we'll go ahead and send down
a 400 message to say that the account already exists.
Now the last thing that we'll need to do is to ensure that
the balance is an appropriate value.
We're going to look to see is there one?
If there is, is it a number?
If it's not, we need to try and convert it.
We will check to see if it's not a number.
If it's not a number will try to do
the conversion with a POST parseFloat.
Now one thing about parseFloat is the fact that if it fails,
it will give us back and not a number,
so we're going to check that.
Basically, this is a few lines to
ensure that if they gave us a value for balance,
that it's a number and we'll have converted it.
If it's not, we're going to return back an error message.
Now we can finally create the account.
We'll go ahead and set up the user,
setup the currency, setup the description or provided default.
We'll set up the balance,
if we just didn't get a balance value we'll set that to zero,
and the transactions will be empty.
Last but not least,
we'll go ahead and then store that inside of our database.
Now, traditionally, whenever you create a brand new item,
you'll return this back to
the client because a lot of times you'll
have generated maybe an ID or
some other information on that account,
especially if you're going to be storing this in a database.
Now let's go back to our little client here,
and I'm going to send a GET request here for Steve,
and it's going to tell us that that user does not exist.
This is basically proof that there's nothing up my sleeves here.
Christopher does exist.
Again, just proving that it works.
Steve, again, 404, Steve does not yet exist.
Let's create Steve here.
We're going to set our method to be post.
We'll set our application content type to be application JASON,
and then we'll send up Steve,
Euros and a balance of 500.
We then get that value back,
and now we can prove the fact that
Steve does exist when we do our GET request again,
you're going to notice that,
that was sent down.
That is how we can allow for new items to
be created inside of our app by using POST.
In the next section,
we'll take a look at a couple of other verbs to see how we can
allow for deleting and modifying of data.