Learn how to tie the model classes and the data from the database together in a controller to return data to the client.
- [Instructor] Now that we have model classes that represent a room in the data base, the room resource will return to the client, and also, we have test data in our data base. Let's pull everything together in a controller. I'll open up the rooms controller, and at the top of the class, I'm going to inject the db context, so I first need to say declare a variable here for Hotel Api Db Context, we'll call this context, then I'll create a constructor for the rooms controller, which accepts a Hotel Api Db Context, and inject those.
Now, I'm gonna build a new route, I'll say below this one, and we'll call this one Get room by Id. This'll be an async route so we'll say async task. It'll return a room resource, so we'll say action result, action result of room, and it's called get room by id, there we go, this'll take a room id, I want the route to this to be, get slash rooms, slash room id, we're already getting slash rooms from the controller name itself so all we have to put here for this attribute is http get, and then in braces, room Id which will be replaced by the Guid room id.
We'll go ahead and use the same pattern we've been using to name this one, we'll say name is name of get room by id, and in the method here we'll say, try to grab the entity from the context, so we'll say await context dot rooms, dot single or default, async, let's grab, pull those async methods in from another name space, and x dot id equals the room id.
Now that may come back as null if that room doesn't exist, so we'll check for that, if the entity is null, then we'll return not found which is a helper method that just returns four o four to the client, because we're returning four o four here, let's annotate this and say we could produce the response type four O four, if we did find an entity, let's take that entity data, and put it into a room resource, say var resource equals new room, we'll need to import the model's name space, now, let's just build up these properties, Href is gonna be Url dot link, the route is this route, get room by id, and we need to add some route parameters, these are passed in an anonymous object like this, say room id which matches the name of the route parameter in the signature of this method, it comes from the entity dot id, and then the name comes from entity dot name, and the rate comes from entity dot rate, which if you remember is an in so we need to divide by one hundred point zero m, which make sure we do the casting to a decimal correctly.
Now that we have this resource built up, and our return type is action result of room, this is in fact a room, so we can just do return resource, directly referencing the db context in the controller, isn't a great pattern but we can refactor that out later. Let's just test out what we have so far. I'm gonna put a break point here on 33 right at the top of this method and let's try some requests with Postman. If we try slash rooms slash foo, which is an obviously incorrect room id, we should fall through to the error condition.
It's not even hitting the method, because foo is not a valid good. I'm gonna jump over to seed data, and pull a valid good out of my data here, just grab this one. I'm gonna try to access this one but change one character, so that it's invalid and see what happens there, so we fall into the method, we have that good here, which is not gonna be found in the data base, let's try stepping into this and seeing what happens. This should be null, entity is null, so we should get not found, and that'll return to the client as four o four.
Now let's try a real correct id. In this case, we should grab an entity out of the data base, build up a new room resource, and return it to the client, so everything's working. We've successfully retrieved data from our data base, and then returned it as an ION Resource back to the client.
- What is RESTful design?
- Building a new API with ASP.NET Core
- Using HTTP methods
- Returning JSON
- Creating RESTful routing with templates
- Securing RESTful APIs with HTTPS
- Representing resources
- Representing links
- Representing collections
- Sorting and searching collections
- Building forms
- Adding caching to an ASP.NET Core API
- Configuring user authentication and authorization