[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 we can complete
our REST endpoint by adding in
the ability to modify an item and to delete an item.
Now, again, just like with Get,
which is an indication that, hey,
we want some information, and Post,
which is an indication that we want to create something new,
Put is what we're going to use to update an item.
Now, the interesting thing about Put
is the fact that we're going to have information in two places.
We're going to have the body that will be coming up via JSON,
and we're going to have the user that's
going to be specified on the URL,
so we're going to read both of those items in.
Then just like before,
just like if we were using a Get,
we're going to look to see,
"Hey, does this account exist?"
If it doesn't, then we'll go ahead and send down a 404.
Now, one very common thing about allowing items to be edited is
you frequently only want to
allow for certain items to be editable.
In our case, we only want to
allow the description and the currency to be editable.
I'm going to look for the other properties here,
if any of those exists,
then we're just going to fail out with a 400 saying,
"Hey, you can only edit those two properties."
Then we're going to look to see if you sent up a description,
if you sent up a currency, if you did,
we'll go ahead and store those on the account.
Because of the fact that we're working in memory,
that's all that we have to do to "store" the item.
But if you are going to be working with a database,
of course, you'd need to save this.
Then we'll go ahead and send down
our account that we have now updated,
because again, it's possible that
database might have updated the item as well.
The Delete side actually looks very similar to a Get.
What we're going to do is we're going to read
the user in from the URL,
we're going to look to see if the account exists.
If it doesn't, we'll send down a 404.
Where things are going to get different is in the logic.
That because of the fact that we're using
an in-memory object to store all of our data,
we'll go ahead and use the JavaScript Delete,
and then we're going to send down just an empty status here.
This just indicates, "Hey,
we did what you wanted to do," but there's nothing
for us to tell you that the user is now gone.
Now, let's go ahead and take a look at this in action.
We'll start off with the Get just to show
Christopher as he currently sits here.
What you're going to notice is that
the currency is set to US dollars.
Let's turn our attention to now doing the Put.
Our method is Put, and then our header is
going to indicate content type of application JSON.
Always make sure you set this whenever you're sending
JSON inside of the body of a request,
which is what we're absolutely doing here,
because we've got our currency set to euros.
We've got that specified,
we'll go ahead and do our send,
and now what you'll notice is the updated object comes back.
If we take a look at it though from the Get side,
just to prove it,
we go ahead and hit "Resend",
now you'll notice that our USD is in fact updated to Euros.
If we change our method now to Delete,
showing how we can in fact delete the item,
now what you're going to notice
is it's not going to give us anything back.
This is just a little quirk with the client that we're using.
If there isn't any information to display,
it just doesn't update that little box there.
But if I go back and update this to be Get,
now it gives us a 404.
That's how you can set up the most common operations
when you're creating a REST endpoint.
If you're curious about more,
you can check out the other videos in this series,
or you can look at the Node.js learning
path on Microsoft Learn.
Thanks for checking this out.