Learn how to add a default sort term to a collection by extending the existing attributes.
- [Instructor] Right now, the default sort order is the order items were added to the database. If I make a call to the rooms route, you can see that the Oxford suite is the first room listed. But if I edit startup.cs in the add test data method and put the Driscoll room above the Oxford room in the list, the collection will return in a different order. It would be better to declare a default sort term. We can extend the sortable attribute to do this.
I'm going to open up the sortable attribute in the infrastructure folder and I'm going to add a bool attribute and I'm going to add a property called default, which is a boolean. I'll update the sort term model to include this property as well. Next we can update the sort options processor to look for the default flag. I need to update the get terms from model method to include the default property in the projected sort terms. I need to keep the name, but now I need to also set default property to be the value of the default property in the sortable attribute that's applied to the model.
The get valid terms method needs to be updated as well. And finally in the apply method, we can use this default sort if there are no sort terms in the request. I'll modify this check slightly. If there are no sort terms, I'll set the terms equal to get terms from model where the term is the default term. If there still is no terms, in other words there is no default term either, then I'll just return the query.
Next we can jump over to the room model and update the sortable attribute. Let's say we want to make the rate property the default sort. We can say default equals true on this attribute and that should do it. Let's give it a try. Previously the Driscoll suite was at the top. Now after applying a default sort, the Oxford suite is top of the list. The rooms collection will be sorted by rate by default unless the client sends an order by parameter. That wraps up the sort functionality for this API. You should feel comfortable implementing a similar sort syntax in your project.
If you want an additional challenge, try adding sort to the /rooms/openings route. In order to make the room field on the opening model sortable, you'll need to add a way to map a sortable attribute to an entity property with a different name. I'll include my solution to this challenge in the next set of exercise files.
- 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
Skill Level Intermediate
Deploying ASP.NET Core Applications (2017)with Nate Barbettini57m 57s Intermediate
1. REST API Concepts
2. Building a Basic API
3. Securing the API
4. Representing Resources
5. Representing Links
6. Representing Collections
7. Sorting Collections
8. Searching Collections
9. Forms and Modifying Data
10. Caching and Compression
11. Authentication and Authorization
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.