In this video, learn how to pull a set of resources from the database and return them to the client as a collection.
- [Narrator] Let's implement the get rooms route on the rooms controller. I'm gonna rename this method to get all rooms, just to be a little bit more clear about what it does. I used control RR in visual studio to rename it. Since we know this is gonna produce a 200 OK, we can add produces response type 200, and I need to make this async now. This will return a task of action results.
The actual response type is gonna be collection of room. Inside this method, we'll call a new service method that we need to write, say var rooms await the room service, get all, or say get rooms, async, we'll need to write that method. Then we'll just build up a collection response. We'll say var collection equals new collection of room. The self link will be a link dot to just this method, get all rooms.
And the value's gonna be what we get back from the room service get rooms async, just dot rooms dot to array. The very end here, we need to return this collection. All right, we have some changes we need to make to the interface. Let's go to I room service and add a new task of I enumerable of room, called get rooms async.
And in the implementation, we'll generate a stub. What we need to do here is pull all the rooms out of the context and then map each one of them to the room resource. Instead of duplicating the code that we have in get room async over and over, we can use something in auto mapper called queriable extensions to project everything all at once. To do that we'll do var query equals context dot rooms. Then we'll say dot project to, pull this in from auto mapper dot queriable extensions, and say we want all of them to end up in a room resource.
Then we just have to say return await query dot to array async to materialize all of those and push them back to the client. This needs to be async. There's one more change we need to make here. We need to provide the mapping configuration to project to. In order to do that, we need to inject a new type. We need a type called I configuration provider, which comes from auto mapper. We'll call this mapping provider.
Or let's call it mapping configuration. I'll inject that here, I configuration provider. (typing) Down in project to, we can pass this along, and that'll satisfy this method. We don't need to inject both I configuration provider and I mapper, so we can get rid of I mapper. And in this line, in get room async, we can say var mapper equals mapping configuration dot create mapper, and change this line to mapper dot map.
All right, with that refactoring, the service code is done. In the ion spec, a link to a collection should include the rel attribute, or relations attribute, with a value of collection, so let's make a new helper method on the link class to make it easy to create links to collections. We'll put this underneath link to. We'll call this one link to collection, and we'll have the same parameters here, route name, route values. (typing) This one also creates a new link object.
Copying over everything from the original. Adding get method, and setting relations to a new string array with the value of collection. Let's go ahead and test this out. I'll send a request with postman and request the rooms route Now we're returning all of the rooms, and each room is projected to the right resource type. One thing is missing here.
It's not adding the collection relation. That's because I forgot to add it here in the rooms controller. I can do link dot to collection, using our new helper method we wrote. And then I can do the same thing over in the route controller as well. Do for the rooms link I'll do to collection. Now we should get the right response and the collection relation. There we go. Right now, we're returning every resource in the collection in one big response.
That might be okay for a small collection like this, but it's gonna start to be problematic when there's hundreds or thousands of resources in a collection. Next we'll take a look at adding pagination functionality to the API.
- 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