From the course: Introducing ASP.NET Core

Enhancing your web API, part 2 - ASP.NET Tutorial

From the course: Introducing ASP.NET Core

Enhancing your web API, part 2

- Hey friends, we're continuing our ASP.NET Core 101 series. We're going to continue to enhance our Web API. So this is part two in 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. - [Leslie] Yeah, so I know one of the things I personally like to do when I'm on Etsy or Yelp or something, I like to write things. - [Scott] Exactly. - If something's really bad, - no stars or zero stars, so... - Well, we should make sure that our API supports you giving us zero stars. - Yeah. (Leslie laughs) - So what we're going to try to do is make a ratings API that's going to basically go on top of our product's service, okay? 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 kind of what we were doing. At the very beginning-- - [Leslie] It's been a while. - [Scott] Yeah, yes, if you go back to the very beginning of all of this we made a product class, and that's the shape of our product it has. Like an image and a URL and title and a description. And we put in ratings but we never really did anything with it. - [Leslie] Oh yeah. - [Scott] Yeah. - Maybe we should-- - We were thinking about it though. - Take advantage of that. - So a rating for us is an array of integers. I think we were thinking like one to 10 or one to five. You know what I'm saying? - Five seem pretty good. - So one to five ratings. So like 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. jenlooper, sailorhg, selina, vogueandcode, our friend April there. These are real products that 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. - [Leslie] Yeah, because I'm noticing ratings isn't currently there. So we're probably going to have to write that property end. - [Scott] That is a great point because the property's there but right now it's kind of 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 no mean will be a good question. So under our JSON file product service right now. If we go and close up the things that don't matter, it basically just does GetProducts. 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 AddRating. And this is not the Web API part of it. This is the-- - [Leslie] Service. - [Scott] The Service part. - [Scott] So this isn't doing any HTTP kind of stuff right now it's just function. - [Leslie] And 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 simply. - [Scott] Yep, so we have a product ID and then let's say the 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. And because our service is building on top of ourselves, we can just call ourself. - [Leslie] Perfect. - [Scott] GetProducts. - [Scott] So now we have in products, a list of products and we've been saying var. You're a fan of var? - [Leslie] Yeah, I prefer my strongly typed. - [Scott] No. - [Leslie] I'm not going to lie there. Var works too though-- - [Scott] Much better. See, I could go and say an explicit type, which would look is what you were saying you prefer I thought you were one of our var people which is unfortunate. We'll still be friends though. And then let's go and do a query. Let's go and say, "Hey products, give me the first." And 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 or it goes to or give me an X where it is. It's ID, is the product ID? - [Leslie] So it's like we're retrieving the first product that we can find that is equal to the ID that we passed initially. - [Scott] Exactly, all right. - So this is some shorthand, right? It doesn't have to do with LINQ querying. So that's a kind of syntax that's similar to if you were to make a query with like SQL or something else in a database. - That's a great point. This is a SQL like or a structured query language. You might say SQL if you're doing databases. LINQ is a thing that we're going to cover actually in C-sharp 200 level videos. 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, whoops. Then that's a thing we talked about. We have nothing, we have no plan. So if there no-- - [Leslie] Then we need to-- - [Scott] Do some stuff else, do some other stuff. So what do we do if they're no, we probably need to, I don't know. - [Leslie] Initialize. - [Scott] Make some room. So we'll say query.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 rating so we could potentially get rid of that. - [Leslie] That's true. - [Scott] 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? We should... - [Leslie] Let's convert them to a list. - [Scott] All right, so we'll go and say query.ratings. And that's again on an array. Fun thing we can do just say "Tolist" And now we have a convenient list, and it's a little easier to add stuff to list. - [Leslie] Yeah, that's right. - [Scott] 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. - And again, each of our products has an array of ratings because we're eventually going to take the average of all the users specified ratings. - Now this is an interesting thing. Notice how I said rating singular here and it complained because that's a rating. And I wouldn't try to do it again. Well, you can't have two that's one and that's another one I was actually supposed to (mumbles) ratings. Okay then, we'll take that object and let's just go and put our ratings back over the top of those ratings, okay? - [Leslie] Mm-hmm. - [Scott] And now we made this array a list and it's not letting me put that list back into that array. - [Leslie] Well, maybe if we convert back to the initial array that we had. - [Scott] So that's the thing we can do, we can go to array as well. And if that's not available, say Int doesn't have it to array. I'll do that control. or I can go and make sure that that's available as a LINQ query. I want to hit control. and hey, I didn't get any help. - [Leslie] Oh, well I think he used rating instead of ratings. - [Scott] Oh, see, maybe I should name my variables better. There we go. Copies that elements back to an array. So I went and said, switch to a list, did my convenience thing and then switched back. So at this point-- - Yeah, so we mentioned before in the JSON file that ratings property isn't even there currently to begin with and we're going to ultimately need to update that database, this mock database. - [Scott] But our products does know what ratings are because we've been going and 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, ".Openwrite." Now, you had commented when we were writing this that maybe we could hide this. - [Leslie] Yeah, something that I'd like to do with a function like this especially serialization or de-serialization which is ultimately going to get used a lot. If you're doing this kind of thing is just create either another method or even a whole new class for this JSON stuff. - [Scott] Yeah, and 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. - [Leslie] And even if you do understand it chances are you're going to get tired of writing this over and over again-- - [Scott] Yeah, that's a great point. 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 texts. And we don't want to assume that our people are only speaking English, right? 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. 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. And I noticed that there's some little squigglies. So I've probably missed something. I think I did not want to put that there and then products. So let's take a look and let Visual Studio do the work. See how 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 dearly than more than I need. And this is a real common thing when you're doing any kind of programming. - [Leslie] I think I found your problem. I think you used an extra bracket here instead of a parentheses. - [Scott] Which one so we shouldn't have? - [Leslie] The one at the end of products listed there. - [Scott] So that's a bracket and then, "Oh, look at that." Good job, thank you for that. Cool, so that is ripe for being refactored. And if I go and close that up, that's that's not too scary. And then the fact that I can even close that in this collapsing editor is kind of a nice reminder that maybe that 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 API controller over in products controller. And if you'll recall, if we go and run our application when we hit our app/products that calls this Get. Let's do it again just to make sure that everyone remembers. Products, yep, there we go. There's our file. Cool, look ratings and all that's from before. - [Leslie] Yeah, that's a good sign. - Now you're saying we need to go and add to this and make another call. So we or doing this before with product service was a Get and we could probably do like a post or put or a patch, but for the purposes of this let's try a Get because I'm just learning. And I don't want to have to install anything. So we're going to do a Get and it's going to have some action. It'll result in some action and we're going to go and get some stuff. And then we were going to call product service just like before. Now, product service, we just added this, ProductService. - AddRating. - There you go. - So this is the AddRating from before and we'll pass in some product ID that we don't have yet. And we'll pass in a rating that we don't have yet. Maybe we'll need to get those. So those should probably be passed into this Get. So I'll say string, ProductId string I think. The rating was an Int maybe? - Yeah, and the reason why we're going to use the string instead of like an Int for the ID in this case is because we want to retrieve it from the LINQ query that-- - Well, our IDs in this case are actually, now they call natural IDs. So instead of a number or like a really big, good, a global unique identifier, we're actually using the names of our friends for that as well. Now, here it's saying not all code paths return a value when you're doing something. Oops, I'm going to go ahead and put that there. I want to say return, OK. You know how when you hit like a 404 that's a not found, there's a whole series of these and OK hides HTTP from us. That's a 200 OK. - [Leslie] So much nicer. - Yeah, there's like not found and redirect and things like that. Now that doesn't really tell us if it's a Get or a post or where we're going to get that data from. For what we're doing we'll see a Get and look it can be a post or put. And 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 and in this case we're using Get just so we can test it out. 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 if you are not careful. - Yeah, if you don't - really want to have a Get, which is kind of a harmless thing like, "Hey, let me get that." You don't really expect a database to get updated just because I got some stuff, but again, for the purposes of what we're doing but in the future, we might post which would like 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 of it. - Yeah, so I'll comment that out and we'll use-- - That sounds kind of easy. - And then right now we're hitting /products. And 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 route rate. So this would be /products/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, look from other places. It can come from the route, from a form or from a query string. Yeah, it's pretty cool. And some people like to stack these up. It's totally up to you. Things don't have to be all in one line. Some people like that, some people want to make it all in one line, it's cool. We're going to grab it from the query string. And what do you think the chances are that's going to work in the first try? - [Leslie] Well, from my experience-- - [Scott] 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. And again, I'm hitting Control + F5. And 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. This is our database in air quotes. And then on the right here let's go and try to say products/rate. And then again, we're doing this with a Get. What'd we say product ID? - [Leslie] So that syntax raising the question mark is like the where? - [Scott] Actually that's a good point. Why don't we do this? Let's go into Notepad and I will write that URL so everyone can see it and then I can copy it over. So cactus and grading equals five Jen's grade. So that syntax there is what you're talking about. - [Leslie] Yeah, so in this case it's retrieve the ratings where the product ID is equal to in this case jenlooper-cactus and find the ratings that have very equal to five. - So we're going to go and put that in there. I'm going to hit enter, and nothing happened. But if we go back over here and click, when I clicked Visual Studio notice that our JSON file changed and it shows up again. Let's try to give Jen a four and there's your four. Now again, a Get is not a place to do this. But it makes it really easy for us to go and test. And we can see that we are successfully updating the ratings and we've picked them up from the query string. And then what we could do is switch that to a patch. These would go to a from body and then we could go and hook up like a mobile device or something like that and build out our API. You could create a Get, and posts, and put some patches and deletes and basically, make your entire database available via a Web API. - That's pretty sweet. - Isn't 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 Blazor to make our Contoso Crafts application even cooler. - Cool.

Contents