Learn how to build attributes to annotate collections and describe which fields are sortable.
- [Instructor] We'll add sort functionality to the API in a couple steps. First, let's extend the models and controllers to make them sortable. I'll start on the room model. I'm going to add an attribute here called sortable. And I'll add it on both of the properties that we want the client to be able to sort on. Now this attribute doesn't exist yet so I'll need to go create it. I'll do that in infrastructure, and create a new class called sortable attribute.
This needs to derive from attribute. And I don't need to add any actual code to this attribute. All I wanna do is add a attribute usage flag that says that the target should be a property and that I can't use it multiple times on the same property. That's it for the attribute. Over in the room model I need to import the infrastructure namespace so that shows up correctly. Now I wanna create a model that will hold the query string arguments that are passed into the controller from the client.
We'll do that in models and create a class called sort options. Sort options T comma T entity and you'll see why in a second. We'll use generics T and T entity, and we'll make this implement the interface I validatable object, which is an interface that ASP.NET core understands. That means we have to implement this interface method. This method is something that ASP.NET core will call internally when it's trying to validate the parameters passed into the controllers, so I'll just add a comment here.
ASP.NET core calls this to validate incoming parameters. I wanna make one more method here, this one's gonna be used internally by our code. This will produce an I queryable of T entity, and I'll call this method apply and it will take in an I queryable of T entity. Both of these will just throw exceptions for now.
And there's one more thing we need, I'll make a comment here. This is going to be called our service code, to a database query. Now this sort options class also needs to hold the actual sort options so we'll declare a string array here called order by. This will be bound to the actual incoming data in the request. We'll come back to this class, but let's jump over to the rooms controller right now.
Over in the rooms controller on the get all rooms method. If you take a look at the get all rooms method you can see that I've updated it with the solution to the challenge I posted at the end of the previous video. I've added pagination to this method, now we can extend that even further and add the sort options we just defined. This will be another from query. This one will be sort options of room comma room entity, we'll call it sort options. All we have to do is pass the sort options down into the service code and that takes care of updating the controller.
Now this is gonna bug us until we update the service interface. Then over here in room service, in I room service we'll say that this takes a sort options of room comma room entity, just like before called sort options and finally in the default implementation, sort options of room room entity called sort options.
Now we need to apply those sort options to this query before it goes and hits the database. We can do that here right below the part where I create this query I'll say query equals sort options.apply, and then use the apply method to update this query with any additional stuff we need to add sorting to the query before it goes to the database. All right, back to sort options. We've got everything hooked up internally. But these methods throw exceptions which means incoming requests will now fail.
We'll take a look at adding this validation code next.
- 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