>> Hey friends, we're continuing our ASP.NET Core 101 series.
We're going to continue to enhance our web APIs.
This is Part Two and the last web API video.
We did a get request to slash products.
We got back our products but we didn't actually
modify the data or do anything with the data.
In this one let's actually change it and
write it back into our database.
>> Yeah. I know one of the things I personally like to do
when I'm on Etsy or Yelp or something, I like to rate things.
If something is really bad no stars or zero stars.
>> Well, we should make sure that our API
supports you giving us zero stars.
>> Yeah.
>> So what we're going to try to do is make
a ratings API that's going to
basically go on top of our products service.
Now of course this isn't exhaustive.
This is not a 300 level Web API video series,
so in the future we'll do one of those,
but for now we're just getting a taste of
what you can do with Web API.
So let's do that. Let's also remind ourselves
what we were doing at the very beginning if we go back.
>> It's been a while.
>> Yeah, I guess. If you go back at
the very beginning of all of this,
we made a product class and that's the shape of our product.
It has an image,
and a URL, and title,
and a description, and we put in ratings,
but we never really did anything with it.
>> Maybe we should.
>> I was thinking about it though.
>> Take advantage of that.
>> So a rating for us is an array of integers,
and I think we were thinking like one to 10 or one to five.
>> One to five seems pretty good.
>> Okay, so one to five ratings,
so five stars and then just a reminder again,
our database, this is a real database by the way.
All of these things you'll be able to find up on
GitHub and these are real folks, Jen Looper,
Sailor HG, Selena, fog and code,
our friend April there.
These are real products, so you can go and
buy these products and support our friends who
gave us these images in their actual Etsy shops.
So what we're going to expect to do is have
all this stuff and then ratings at the end.
>> Yaks, and noticing ratings isn't currently there.
We're probably going to have to write that property again.
>> That is a great point because the property's there,
but right now it's null or it's just not available.
So if there is no array,
we're assuming it has no ratings.
So that brings up a good point.
We'll have to think about no ratings versus empty array.
What does "Null" mean will be a good question.
So under our JSON file products service right now,
if we go and close up the things that don't matter,
it basically just does get products.
But what we want to do is go and say "Add Ratings"
and we can do that in a couple of ways.
I'll say public void at rating.
This is not the Web API part of it.
This is the service part.
This isn't doing any HTTP stuff right now.
It's just a function.
>> I think in this case it would make sense to try to
retrieve the product by something that's unique to it.
So like an ID typically.
>> So we have a product ID.
Then let's say that folks are going to pass in the rating,
and then you said retrieve it.
So we'll say let's go and get our products.
Because of our service is building on top of ourselves,
we can just call ourselves.
>> Perfect.
>> Good products. Now we have in
products a list of products and we'd been saying var,
you're a fan of var.
>> Yeah. I prefer my strongly typed.
>> No.
>> I don't want. But it'll work still though.
>> That's much better. I could go and say
an explicit type which would like it to be.
What you're saying you prefer I thought
you were one of our var people
which is unfortunate. We'll still be friends though.
Then let's go and do a query.
Let's go and say "Hey products,
give me the first."
Let's go and get some products,
some theoretical Product X rocket-ship.
Let's get one where, I say rocket-ship but
people say is the case what goes to
or give me an x where it's ID is the ProductID.
>> Sounds like we're retrieving the first product that
we can find that is equal to the ID that we passed initially.
So this is some shorthand ripe.
Doesn't have to do with link queries.
So that's syntax, it's similar
to if you were to make a query with like SQL or something else.
>> That's a great point. This is a SQL
like or a Structured Query Language.
You might say SQL if you're doing databases,
Link is a thing that we're going to cover actually
in C-Sharp 200-level video.
So you can go and check that out with our friends in a little bit.
So if you in fact have no query at all and you have no ratings,
then that's a thing we've talked about.
We have nothing, we have no plan.
So if they're null,
you can do some stuff else do some other stuff.
So what do we do if they're null?
I would probably need to, I don't know.
>> Initialize.
>> Make some room, yeah.
>> Pretty good.
>> We'll say query dot ratings and then,
there's different ways that we could probably do this.
Maybe we could make our objects smarter
and be more explicit about not having a ratings.
We could potentially get rid of that.
>> It's true.
>> Maybe make it so the setter does it for us. I don't know.
But if you in fact found some ratings, then what should we do?
>> Let's convert them to a list.
>> We'll go and say query dot ratings.
That's again on an array.
The fun thing we can do, just say to list.
Now we have a convenient list and it's
a little easier to add stuff to list. That's right.
So then what's our rating?
Our rating coming in is this one here, rating.
So we'll go and we'll add that rating.
>> Again, each of our products has an array of ratings because
we're eventually going to take the average
of all the user specified ratings.
>> Now this is an interesting thing.
Notice how I said rating singular here and it
complained because that's a rating.
I wouldn't try to do it again.
Well, you can't have two,
that's one and that's another one.
I wasn't actually suppose say ratings.
Then we'll take that object and let's just go
and put our ratings back over the top of those ratings.
Now, we made this array a list,
and it's not letting me put that list back into that array.
>> Well, maybe if we convert
back to the initial array that we had.
>> So that's a thing we can do. We can go to array as well.
Now, if that's not available,
say int doesn't have it to array,
I'll do that control
dot or I can go and make
sure that that's available as a link query.
I'm going to hit "Control" dot and I didn't get any help.
>> Well, I think you used rating instead of ratings.
>> Oh, see. Maybe I should name
my variables better. Here we go. There we go.
Copies that element for the actual array.
So I went and said switch to a list,
did my convenience thing and then switched back.
>> We mentioned before in the JSON file that ratings
property isn't even there currently to begin with.
We're going to ultimately need to
update that database, the smock database.
>> But our products does know
what ratings are because we've been updating that.
So we need to go and serialize this.
We need to go and put this back into the file.
Now, this is a little bit more
complicated maybe than it needs to be.
So forgive me as we do this.
But what we're going to basically do is
we're going to go and open up that file.
Again, we're going to say dot open,
and you had commented when we were writing this
that maybe we could hide this.
>> Yeah. Something I'd like to do with
a function like this especially serialization or
deserialization which is ultimately going to
get used a lot if you're doing this thing is
just create either another method or
even a whole new class for this days nonstop.
>> I think you're right because when
we talk about responsibilities,
I think we were deciding that there's a lot going on and
maybe we have more here than is necessary.
>> Even if you do understand it,
chances are you're going to get tired
of writing this over and over again.
>> Yeah. That's a great point.
>> Read something.
>> So here I'm going to go and take
my product and I'm going to go,
and then this is a little bit interesting.
There's lots of different ways to go and write out some text.
We don't want to assume that our people are only speaking English.
So I want to make sure that if
our languages are maybe
Japanese or Chinese or something more complicated,
that we can do that.
So we do a thing called UTF-8
which is the I can speak any language.
So even though our products right now are only in English,
we're going to go and make sure our serialization
happens in any language.
so we've added these little options and this is
an option here that is being added onto this.
>> Yeah.
>> Probably, all of this,
the fact that we've gotten this far,
should be in another function. So I think you're right.
In the future, we'll probably hide some of this.
Now, notice that there's some little squiggles.
So I've probably missed something,
I think I did not want to put that there.
>> Yeah.
>> Then products.
So let's take a look and let Visual Studio do the work.
If you put your cursor,
it will try to figure out where things match up.
So here, we've got this,
and that goes to here,
and then products goes to here,
and that one goes to there.
So I think I've got one additional [inaudible] than more than I need.
This is a real common thing when you're doing any programming.
>> I think I found your problem. I think you used
an extra bracket here instead of a parentheses.
>> Which one?
>> The one at end of products listed there.
>> Okay. So I don't need,
so that's a bracket.
Look at that. Good job. Thank you for that.
Cool. So that is ripe for being refactored.
If I go and close that up,
that's not too scary.
Then the fact that I can even
close that in this collapsing editor,
is a nice reminder that maybe there should be another function.
So I appreciate that.
At this point though,
we haven't got anything webby here.
We've just made a new method on our service.
>> Yeah. So maybe we should tell
something like our controller that we created in the last video,
to actually call it and ask for that information.
>> So this is our APIController over, and ProductsController.
If you'll recall, if we go and run our application,
when we hit our app slash products,
that calls this Get,
let's do it again, just to make sure that everyone remembers.
Products, there we go.
There's our file.
Look, gratings null. That's cool, that's from before.
>> That's a good sign.
>> Okay. Now, you're saying we need to go and add
to this and make another call.
So we are doing this before with ProductService as a Get.
We could probably do like a Post or Put or a Patch,
but for the purposes of this,
let's try and get because I'm just learning.
>> Okay.
>> I don't want to have to install anything.
>> Fair enough.
>> So we're going to do a Get,
and it's going to have some action.
It'll result in some action.
We're going to go and get some stuff,
and then we were going to call ProductService, just like before.
We just added this. ProductService. Here we go.
>> AddRating.
>> Yeah. This is the AddRating from before,
and we'll pass in some ProductId that we don't have yet,
and we'll pass in a rating that we don't have yet.
Maybe we will need to get those.
So those would probably be passed into this Get.
So I'll say string.
Productize this string I think,
or Rating was a an int maybe?
>> Yeah.
>> Okay.
>> The reason why we're going to use
the string instead of an int for
the idea in this case is because I think we want to
retrieve it from the link query.
>> All right. IDs in this case,
are actually natural IDs.
So instead of a number or like a really big GUID,
Global Unique Identifier, we're actually using
the names of our friends for that as well.
>> Yeah.
>> Okay. Now here,
it's saying not all code paths return a value,
when you're doing something.
I'm going to go in and put that there.
I'm going to say return, okay.
You know how when you get like a 404, that's not found.
There's a whole series of these, and okay hides HTTP from us.
That's a 200OK.
>> Which is so much nicer.
>> Yeah. There's not found and redirect, and things like that.
Okay. Now, that doesn't really tell us if it's a Get,
or Post, or where we're going to get that data from.
For what we're doing, we'll see a Get.
Look. It can be a Post or Put.
You were telling me about the difference between Post and Put,
and Get and Delete.
>> Yeah. So lots of different options as you can tell.
In this case, we're using Get,
so we can test it out.
>> Yeah.
>> But the problem with this is since we're
modifying part of our JSON at the end of the day here,
you could end up with some weird side effects.
>> Yeah. You don't really want to have
a Get which is a harmless thing.
Okay. I'm going to get that. You don't really expect
a database to get updated just because I've got some stuff.
>> Right.
>> But again, for the purposes of what we're doing,
but in the future, we might Post,
which would make a new thing in the database,
or Put, which would update the database,
or Patch, which would be like I'm just changing a little.
>> Push out of this.
>> Yeah. So I'll comment that out, and we'll use [inaudible].
>> That sounds easy.
>> To get. Then right now,
we're hitting slash Products.
We had set that up in this route.
We can have a little sub route,
and we were going to do Ratings.
So why don't we say "RouteRate."
Okay. So this would be slash Products slash Rate.
But then, where do these come from?
If we were doing a Patch,
which we were talking about up here,
we would go and say, we'll get that from the body of the Patch.
I'm just going to put that in the comments here.
This comment is for some future.
But this is not going to come from there,
it's going to come from other places.
It can come from the Route, from a Form,
or from a Question, flat.
Yeah. It's pretty cool. Some people like to stack these up.
It's totally up to you.
Things don't have to be all on one line.
Some people like that, some people want to make it
on the line. That's cool.
So we do a Get. We're going to grab it FromQuery string.
What do you think the chances are
that's going to work on the first try?
>> Well, from my experience.
>> Chances are low.
>> Yeah.
>> Well, we can give it a try.
First thing to do is see if it
compiles, and then we'll try to run it.
Again, I'm hitting "Control F5."
What I'm going to try to do here is,
I'm going to pull that over to the side and snap it,
and then I'm going to do this because I'm confident.
What we're going to do is have the data on the left.
So this is our database in quotes.
Then on the right here, let's go and try to
say product slash rate.
Then again, we're doing this with a Get.
>> Yeah.
>> What did we say, ProductID?
>> Yeah.
>> I see.
>> So that's intact. The question mark is like the where?
>> Actually, that's a good point. Why do we do this?
Let's go into Notepad,
I will write that URL so everyone can see it,
and then I can copy it over.
So cactus and rating equals,
I don't know five, Jen's grade.
So that syntax there is what you're talking about.
>> Yeah. So in this case,
it's retrieve the ratings where the ProductId is equal to,
in this case, jenlooper-cactus,
and find the ratings that have very equal to five.
>> Okay. So we're going to go and put that in there.
I'm going to hit "Enter," and nothing happened.
>> Yeah.
>> But if we go back over here and click.
When I click "Visual Studio," notice that
our adjacent file changed and it shows up again.
Let's try to give Jen a four, and there's four.
Again, a Get is not a place to do this.
>> No.
>> But it makes it really easy for us to go and test.
>> Yeah.
>> We can see that we are successfully updating the ratings,
and we pick them up FromQuery string,
and then what we could do is switch that to a Patch.
These would go to FromBody,
and then we could go and hook up like
a mobile device or something like that,
and build out our API.
You could create Gets and Posts,
and Puts, and Patches, and Deletes, and basically,
make your entire database available via a web API.
>> That's pretty sweet.
>> That cool, very cool.
So let's take a break, and we're going
to come back and try something totally different
using a technology called Blazer to make
our Contoso crafts application even cooler.
>> Cool.