In this video, Nate explains how to add support for additional comparison operators such as less than and greater than.
- [Instructor] To add support for comparison operators like less than and greater than, we can refactor the code that creates the comparison expression and delegate it to the proper search expression provider. In the Apply method, we can update the code that creates the comparison expression and delegate that to term.ExpressionProvider.GetComparison, which is a new method that we'll write. We'll pass in left, term.Operator, and right. Now let's go add this to the interface. I'll open up IsearchExpressionProvider, and add a new method that returns an expression called GetComparison.
This method will take a MemberExpression on the left, a string for the operator, and a ConstantExpression on the right. In the default implementation, we'll simply return Expression.Equal. I'll make this method virtual, as well. And here, we'll just return Expression.Equal, left comma right. Now we can modify the derived classes from the default implementation. Let's open up the Decimal.toInt provider.
We can override the GetComparison method and add a couple of more operators. So let's say Switch on the operator to lowercase, just to make it case insensitive, and we'll say in the case of the greater than operator, we'll return Expression.Greater, left comma right, and we can duplicate this for greater than or equal to, and we might as well support less than and less than or equal, as well. So, we'll call that LT and LTE.
For LessThan and LessThanOrEqual. Now, if the operator doesn't match any of these, we'll have a default case that falls back to base.GetComparison. Since we're falling back to the base implementation, if the operator doesn't match any of these we've defined here, we should also go back and update the base implementation and throw in Error if the operator is one we just simply don't understand. So, let's update this method one more time, and we'll say If not op.Equals to eq, make sure that's StringComparison.OrdinalIgnore, then we'll throw an argument exception that says Invalid operator, and we'll put in the operator itself.
If that's not the case, we can return Expression.Equal, as we were before. Alright, let's try this out. So, our previous search on Rate equals 101.19 should still work fine, and if we make a bogus operator, like XX, we should get an error saying that that does not work. Now let's try something like greater than 101.19, so now we see the Driscoll Suite, which is more expensive. If we say greater than or equal to, we'll see both, and we can try less than or equal, which should only show the Oxford Suite.
That wraps up our look at building collections with search functionality. However, you may have noticed. There's no way for clients to discover the search or the sort parameters dynamically. In the next chapter, we'll look at ion forms and see how they can help our API be more HATEOAS compliant. If you want an additional challenge, try adding search to the slash rooms slash openings route. You'll need to add an expression provider for day / time fields. I'll put my solution to the 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
Building Web APIs with ASP.NET Core (2016)with Chris Woodruff1h 7m Intermediate
Deploying ASP.NET Core Applicationswith 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.