ASP.NET Core Web API .NET 8 2024 - 26. Portfolio GET

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so we've gotten our many to many relationships set up in our database we've modeled the relationships now we need to start figuring out how we are going to actually crud this data and we'll go ahead and we'll just start with read because it's probably the easiest we have a join table right here and the way that we fetch join tables is not really going to be that much different and we'll just talk in a little bit of pseudo code just to not confuse people that much first thing first is we're going to get the user from the claims when the user sends the claims we're going to reach into the claims we're going to get the username and with that username we can find all of the users associated with the user ID and the stock ID and when we find the user we will find all the users associated with that stock unfortunately though our data is going to be returned as a join table so it's going to look a lot like this we can't really do much with it fortunately for us we can use n framework core to find all of the stock data associated with the stock ID so essentially we're going to get all of the users with their stocks and then we're going to transform all the data into actual stock so let's go ahead let's hop inside vs code and let's do some coding okay so we are inside of vs code and the first thing that I'm going to do is I'm going to go into my controllers file and I'm going to make a brand new controller for our portfolio Okay so first things first as always we need to go into here we're going to bring in our controller base and we are going to set up the API endpoint so um this is the way that I do it but feel free to do it any way that you like so I'm going to say portfolio and I'm going to give this API controller attribute all right so we're going to need a couple things so let's go ahead bring in our Constructor we need the user manager and I'm going to say app user and I'm going to call this user manager then I'm going to go down here just to bring it down because we're going to have a lot and I'm going to bring in the stock repository because we're going to have to bring in some stocks of course okay that looks good go ahead control. that so next thing that I'm going to do is do my private read only so I stock repository or actually let's make sure they're in order let's make it look good so user manager say app user and go ahead and sign this underscore user manager then we're going to do the same exact thing for the stock repository say read only stock repository it's equal to stock repo okay then go down here going to say set up the user manager user manager and stock refill it's equal to stock refill okay so we're looking good on our Constructors we don't really need we don't really need anything else and we're going to make our portfolio repository here in a second so we'll worry about that later and I'm going to say this is going to be a g we're reading and we're going to need to authorize so whenever you have authorized it's going to check the claim and any endpoint that requires a claim you're definitely going to need authorize but feel free to set the authorization however you want so say I action result so now we're going to say get user I'm going to call this get user portfolio and we actually don't need to pass in anything because maybe later if you want to customize it but the only thing that we need to worry about right now is going to be our user claim so I'm going to say uh user. user name just like that and we don't have it because we need to make it but let me explain what this is this is actually the user and it's being inherited from the controller base so whenever you actually utilize an endpoint an HTTP context is going to be created and this user object is going to allow you to reach in and grab everything associated with the user and the claims which is another great part so just if you ever see this just realize you're probably going to be searching for claims or within the HTTP context and we need to create an extension so if you don't have an extension folder already that may be left over so I'll just go ahead get rid of it and uh read it so go into your API folder and we are going to create a new folder and we're going to add some extensions and uh claims extensions are very common so I'm going to call this claims extensions because if you're working on this project after this you're going to be us utilizing claim extensions a lot so it's probably best just to make their own uh class and we're going to add static and we're also going to add static string we're going to call this what we just named it before get username and we're going to pass in you're not actually passing in this is the actual type that it's going to be attached to and it's going to be a type claims principle so claims principle was brought in now what we're going to do is we're going to reach into the claims through this user object that we have we're going to say claims I want to say there's already methods to reach into the claims that's already pre-built for you which is very nice X going to say type and this is just kind of gibberish and it's not going to make a lot of sense because this is just how you reach into claims it's kind of got a lot of of weird verbage or a lot of weird words but this just kind of how you do it so we're going to go into here I'm going to type in HTTP and this is literally just a random string from probably like 2008 and it's going to say XML soap and.org WS uh WS not w25 05 identity claims given name with two ends and I will type this down below and put it down in the description uh so you don't have to type it and to make sure that it is the way that you want but it's actually not HTP uh s it's actually HTTP so that was one err and let me just check really quick to make sure that it is correct so I'll leave that down the description but that is it right there it is kind of a little bit of gibberish kind of strange looking but this is how we reach into the claims where do we actually get these claims we get the claims from our token so if you go inside of our token service we added the email in the username and that's actually what we're reaching into when we actually create the token we're going to reach into that token we're going to uh not the actual token itself but the HTTP context and the claims that were given to us through the token so that is what's going on but enough chitchat let's go back in here we're going to go back into our controller and bring in our API EX extensions so the next thing that we have to do is we have to get the user and now that we have the username we can just quickly use the user manager in order to find the username and there's already a method attached to it called find by uh name async not find by email so find by name or you could find by the email if you want to if you want to search through the claim the email is actually in there if you want to but I'm going to do username so the next thing that we need to do is we need to find the user portfolio and the way that we're going to find the user port portfolio is we are going to reach into the database and we are going to pull out all records that are associated with the given user that is logged in with the data that we got from the claims and then we're going to return all these stocks that are associated with that user how do we do that well first things first is we need to go ahead we need to create an interface I'm going to call this I portfolio repository and we're going to add a method in here that's going to be tag ask it's going to be a list Doc and we are going to call it get user portfolio and we're going to pass in an app user and we'll give it the name user okay so now what we need to do is we need to actually implement it so go into our repository folder and we're going to create bi portfolio repository pretty simple go ahead Implement our interface so uh portfolio repository and then we're going to go ahead we are going to implement it of course we're going because it's a repository we're going to have to bring in our application DB context so go into here just go ahead bring that in I think my caps lock is on so application DB context call this you guessed it context do our privates our private readon I should say application DB context the application DB context and context then here of course he guessed it no surprises here okay so let's go ahead start building this method first thing is return now that we have the context we're just going to filter all of the portfolios by the user so we're going to say you and we're going to return everything but even though that we're getting back all this data we don't want want the user data all that we want is the stock data so all that we're going to do is we're going to go down here and also I need to make sure that this is async so I don't forget and we're going to go up here and this is where we are going to transform the data and all that a select is is a map so if you come from the world of JavaScript a select statement is basically going to iterate over it apply these changes to it and then return a new data structure and here's how we're going to do it we're going to go stock say stock ID and then this is pretty much the blueprint for how we're actually going to return it so stock and we're going to pass in the symbol and we need to return it exactly like the stock type so company name is equal to stock stock company name and purchase it's equal to stock stock. purchase is getting some good practice in last div is equal to stock stock last div and Industry is equal to stock do stock industry then lastly we have our market cap so market cap is equal to stock stock. market cap and of course we need to go ahead and execute this and we're going to say to list a sync and we can't forget our away so we need to of course await this because it's a sync so now that we've gotten this built let's go ahead let's go back and bring in our portfolio repository so I'm going to say I portfolio repository and we'll call this portfolio repo then we're going to bring it in up here so private readon I portfolio repo is equal to portfolio repo and of course of course we can't forget we need to add the portfolio repo and assign this to the actual parameter passed in okay and the last thing that we need to do is we need to bring in the portfolio repository execute our method and we will good to be good to go after this so go ahead pass in the app user and we're going to return the user portfolio and one thing we need to do before anything else is we need to add the dependency injection before we forget so go inside your program.cs file we're going to copy this bring it down and we're going to bring in our I Portfolio Service so I Portfolio Service or I portfolio repository then bring in your portfolio repository go ahead make sure to rerun it and in the meantime before we do anything else we need to go inside of our database and actually create manually create a portfolio because we don't actually have one yet so first thing go to your stock database add a stock get the stock ID of the stock that you want to add to your portfolio then go to your users uh once you inside the users you need to make sure that first thing you there has to be a user in there so if you don't have one already just go ahead and create one and then go to the portfolios and then go to edit the top 1,000 or top 200 rows if you don't if you didn't see that edit top 200 rows so it's going to be right there then we're going to go make sure you delete the null and then go inside the app user go ahead paste that and then we're going to paste the stock ID of the one that we want to associate it with and if you did everything correctly and it's pasted in there as a string and if this is a uh integer you should be good to go and just make sure to press enter and it's going to execute it then what we want to do is we want to go back to Swagger so now that we're done with all that let's go inside of swagger make sure that you're logged in I can't show you my login but whenever you put the token in there just remember that the little a lot closes and that means you're authorized and then what we want to do is go to our portfolio we want to execute and what you should see should be the stock that you associated with the user in the database anyways hope that you guys enjoyed this if you did Smash that like button smash that subscribe Button as always thank you for watching
Info
Channel: Teddy Smith
Views: 3,397
Rating: undefined out of 5
Keywords: software development, programming, engineering
Id: wbD-XUmoeqw
Channel Id: undefined
Length: 14min 16sec (856 seconds)
Published: Sat Jan 27 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.