ASP.NET Core Web API .NET 8 2024 - 18. Filtering

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in our previous video we talked about two list and its importance whenever you are getting data back from the database 90% of the time you are going to do so in the form of a list that is one reason why two list is very important T list is also very important because it is what generates the SQL and pretty much fires the SQL gun at the database to get data back you see whenever you get data from a database it's not doing so through magic it's actually generating SQL and calling to list is what fires the SQL mechanism gets the data and brings it back but once you fire the metaphorical SQL TW list gun that's pretty much it you've got your data it's generated the SQL everything is over with but how do you defer this maybe you want to filter maybe you want to limit maybe you want to add more link commands to your stock or your Entity framework mechanism this is where as queriable comes in as queriable is going to delay the firing of the SQL gun so you can do things like filter you can do things like limit and after you get done building your SQL query and fine-tuning the actual data that you want back from the database that's when you can fire the two list send your your data to or send your SQL to the database and get the data back in a more filtered concentrated form but this is what we're going to use when we do the actual filtering for our API so let's go ahead let's hop over into vs code and let's do some filtering so we are inside of our stock controller we don't really need filtering for our comments we need filtering for our stocks and the reason why we need filtering is because you want to be able to filter by the name sometimes people call it a search sometimes people call it a filter but either way we need to be able to get all these stocks by a particular name and the way that we're going to do that there's many ways that you can go about this but one of the best ways is to create an object so that we can just insert this object right here just like this so query object and we'll call this the query and we going to get this from query and this from query annotation is going to allow us to insert query parameters so the next thing is that when we actually sech this is the way that we want to do it we don't want to pass in through the route we want to pass in through query parameters and query parameters look like this if you've ever used um Google search you will see query parameters used and query parameters are a way to pass in key value pairs using this right here so we're going to have one query parameter for symbol we're going to have one query parameter for the company name and I'm going to show you guys how to do it so query we're going to create this object called a query object and there's no real place to put this so what I'm going to do is just make another folder folder I'm going to call this helpers and I'm going to call this the query object and I think I accidentally made an interface on what to create a class so query object and the way that we're going to do these key value pairs is we're going to create a property called symbol and we're going to create a property called company name and we're going to put them within the query object so first one's going to be prop we're going to be passing a string it can be optional and it's going to be a symbol then also we're going to make this null right here we're going to go a prop string and we're going to say company name and we're going to give this a no looking good all right so now what we need to do is we need to number one bring this in right here and now what's going to happen is that we can pass in our query programs through this query object and instead of having it passed in individ visually we could put the company name and we could put the symbol directly into here but with a query object it'll being wrapped in a nice little object and it'll just be one parameter that we pass in okay so next thing is that we need to redo the get all async the get all async doesn't actually pass in anything and we need the ability to be able to pass in our query just like this and the way that we're going to do that is we are going to modify our actual interface so I'm going to go into the interface right here we're going to go into our stock and instead of passing in nothing we are going to pass in the query object and we're going to call this query okay now what we need to do is go into our stock repository and we're going to go ahead and fix this so instead of uh control dotting instead of using control dot you probably don't want to because it's going to create another get all async we're just going to go ahead and add it into here just like this so we're going to say query object and we're going to pass in the query okay so as you probably guess we have to use our as queryable and instead of having the two list async what we're going to do is we can probably just get uh no actually let's not do that let's do as queriable so we're going to make this as queriable then go ahead pass that and because we are no longer returning right here what we're going to do is we're going to save our stocks is equal to A8 and we're going to go down then we're going to get rid of this o08 because it's no longer async and here's where we're into chain on logic so now that we have S queryable we can add more logic to our actual uh database query so we're going to go is nor whitespace and we're going to pass in the value that we get back from our query and it can be company name and you could you could add more here if you want to but I'm just going to have the company name in the symbol that's another reason that I chose to do an object is because you can add stuff to it later but just for right now we'll just have the company name and we'll have the symbol so company name is equal to contains so contains is going to do a search and we're going to search for the company name okay that's looking good then we're going to go down here we're going to do the symbol so we're going to go is string is null or whitespace so is null or whitespace and we're going to pass in our query. symbol and we'll go down here we're going to just add some more additional logic so what it's going to do is it's going to check the actual object and when we pass in this query right here it's going to pass in the uh values that we pass in through the actual query prams just like this and what's going to happen is it's going to go inside our actual uh logic right here and it's going to check and see if there's actually anything inside of it so we're going to go where and all where is doing is filtering so s do symbol contains is equal to query okay that look look good then we're going to go down here and this is where we're going to actually execute our await so we're going to say await stocks we're going to say to list async okay and I think we are looking good so let me make sure that the controller looks good and I'm going to go over here I'm going to do a quick cod restart we're going to go do nut netwatch run and I'm going to go into Swagger let's check and see so make sure that we can execute and return the whole entire list and let's return Microsoft so we'll go here we'll say Microsoft and we get Microsoft back and let's go back and let's search for Tesla so we'll say Tesla and we also get Tesla's back as well too so we can now search by the company name and the symbol so we are correctly uh so we are correctly filtering through our API anyways hope that you guys enjoyed this if you did make sure to smash that like button make sure to smash that subscribe button and as always thank you for watching
Info
Channel: Teddy Smith
Views: 2,754
Rating: undefined out of 5
Keywords: software development, programming, engineering
Id: vszGxxxfp6s
Channel Id: undefined
Length: 9min 56sec (596 seconds)
Published: Fri Jan 19 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.