In this video, Nate will explain how to pull a set of resources from the database and return them to the client as a collection.
Let's jump over to the rooms controller and implent the GetRooms route. Instead of throwing an exception, we'll use the RoomService to get a list of rooms. We'll create this GetRoomAsync method on the RoomService in a moment. Since we're using await, we should make the method Aysnc and return a task. We should also accept a CancellationToken as one of the parameters and finally, since it is an Async method, we should rename it to follow the Async pattern. I used Visual Studio's control R R refactor rename in order to rename this method everywhere.
Once we have a list of rooms from the RoomService, we can create a collection link using Link.To and passing the name of this method, GetRoomsAsync. Then we can create a collection object of type Room, set the Self link to the collection link we just created and set the Value to the rooms we got back from the RoomService and we'll call ToArray to make sure it's an array. Then we just have to return the collection.
This looks good for the controller. Now we need to go over to the RoomService and add the GetRoomsAsync method. So I'll switch to the IRoomService interface first and add a new method on the interface that returns and Ieumerable of Room called GetRoomsAsync. This will just take a CancellationToken and that's it. Since we updated the interface we also have to update the implementation. In DefaultRoomService, we can use Visual Studio to help us implement the interface with the correct method and inside this new GetRoomsAsync method we can create a query that'll hit the context, get the rooms, and then project using AutoMapper.
This ProjectTo method is from AutoMapper.QueryableExtensions and it allows us to project this to the destination type automatically. Then we'll return await query.ToArrayAsync. I'll need to make this method Async. The nice thing about AutoMapper's ProjectTo is that it'll automatically project every single entity in the response to the room resource. In the ion spec, a link to a collection should include the rel 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.
Going to switch over to the link class and copy this to method and create a new method called Link ToCollection. Same parameters, the only difference being in the relations property we'll automatically add a new string array with a value collection. Now back in the rooms controller we can use this ToCollection method. We need to import the System.Link namespace to see the ToArray method here. All right, so let's test it.
We'll build and run with IS express and switch over to Postman. We can hit the Root route first and then navigate to the Rooms route. So in the response from the Rooms route we see that we have the self-referential href of the route itself, we have the rel attribute that tells the client that this should be parsed as a collection, and then we have the value property, which contains the two resources that currently exist in the database in the Rooms collection. This works but right now we're returning every resource in the collection in one response.
This might be okay for a small collection where we have two rooms but it would be really problematic if there were hundreds of resources in the collection. Next, we'll build pagination functionality into the API.
- REST vs. RPC
- Using HTTP methods (aka verbs)
- Returning JSON
- Creating a new API project
- Building a root controller
- Routing to controllers with templates
- Requiring HTTPS for security
- Creating resources and data models
- Returning data and resources from a controller
- Representing links (HREFs)
- Representing collections
- Sorting and searching collections
- Creating forms
- Caching and compression
- Authentication and authorization for RESTful APIs