ASP.NET Core Web API .NET 8 2024 - 13. Comment GET + Include()

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay guys so this video we're going to be doing two things first thing is that we are going to be creating the get by ID end point for the comments previously we've only been working on the stock entity but now we have to start tackling some of the issues of introducing comments and how we're going to do so with a on to many relationship and that first problem or actually I guess that second problem that we are about to solve is how do we actually put the comments on the stock when we when we return stocks how are we going to attach the comments to the stock entity and we have to start thinking about that because Entity framework unfortunately is not going to do that so the way that we're going to do this is we are going to use what's called an include and an include is going to make it so that we will actually get comments like I said on the ACT on the stock entity so on our stock entity now instead of having a null here what's going to happen is we will now have our comment so our comment would be something like title uh we'll just say comment just like that and that's what it'll look like anyways all right let's go ahead let's hop inside VSS code and let's get coding this thing up okay so we are inside of VSS code right now and the first thing that we want to do is go inside of our I comment repository and we are going to start off with just making our actual interface signature so I'm just going to go into here we are going to return a comment it's going to be an optional of course and we are going to say get by ID async and we're going to pass in an INT because if you're going to get one you have to identify it somehow so we're going to make sure we have our int inside of there next thing that we're going to do is fix our red squiggly line so if you make changes to the interface or you add a method signature you're going to have to go into here and implement it because it is a contract all right so we're going to get rid of this we no longer want this so the first thing that we're going to do is go get our first comment we need to await comet. repo G ID async go ahead pass in the ID after that what we are going to do is check for null so go if comment is equal to null then we're going to go down here if it is null we're going to return not found then after after that we are going to return our ey action result which is our okay and we're going to return uh convert it back over to a comet dto just like this with our nice little uh mapper function okay so let's go ahead here and I'm going to test this to make sure that we are all good to go and that looks good so let bring Swagger over here and let's get our first comment got an ID here so if you don't have a comment I'll show you how to make one very quickly uh go into tables go into comments right click go to edit and what you want to do is just create another comment so go com title comment let's see here can't don't press enter too quickly we'll say title content and then go to created on just going to go 19902 02 and you don't have to type in the full date it will automatically do a portion for you and I'm going to link this stock to another stock that I had previously which is Tesla and if you don't know how to do that I'll show you how to do that very quickly so if you want to tie it to a stock that's already in your database just go into the actual stocks table go right here right click go select and just pick one and you want to connect it to this ID right here here if you actually try to get all of the stocks and you you want to be able to have the comments with all the stocks it's not going to show them unfortunately so we already have two comments that are tied to the actual Tesla stock but they're not showing up so we need to make sure that that is actually occurring and the way that we're going to do that there's many ways that you can do that but I'm going to go ahead and start with the stock dto and just go down here and put the actual comment so public we'll go list comment dto and we'll have the comments and also we will make sure to add our get and set so we'll have a get and set looking good we need to do we need to actually go over to our mapper and we need to map this so we're going to go comment uh so comments and we're going to go stock stock model we're going to comments we're going to select go C is equal to C do to comment dto looking good and then we need to convert this over to a list so we're going to go to list and we don't want the semic conin at the end of it okay let me make sure my comment dto is good the stock dto is looking good so now what we need to do is we need to go inside of our actual stock repository and we need to do the include so I'm going to go into here I'm going to go include go C is equal to C do comments go ahead to list that then we're going to go into here going to do the same exact thing for the git ID and we don't want find we want first or default because find does not work with uh the include so we're going to go I i.id is equal to ID that looks good the next thing that we need to do is we need to install two packages we need to install newtonsoft and we need to install the ex the extensions for MVC so go ahead let's go inside of actual Visual Studio here do control shift p open Nate gallery and we want to do Newton soft install just the latest Newton soft then we're going to go into here we're going to type in Newton soft again so Newton soft do the MVC ASP nit core MVC and make sure that you get the eight then what we need to do we need to go inside of our program.cs and we need to install this so if you want I'll leave this in a link down in the description if you don't want to type it out but I'm going to go ahead and just type it out but you want to make sure that you get the ad Newton Salt version so what I'm going to do is go into Builder and I'll just go ahead type it out so we're going to go Builder doservices equal to add controllers and make that a function we'll go add Newton soft Json and if make sure to install the right extensions like I said or you're going to get a bunch of Errors so we're going to go down in here we're going to go options uh serializer settings reference Loop handling and the reason that we're doing this is to prevent object Cycles uh object cycle object Cycles are a part of Entity framework core pretty much so we're going to have to install this so that it doesn't actually do object Cycles which we don't want all right so that is looking good close that out and what we're going to do is we're going to go ahead and we're going to restart this so go here net watchrun and let's test it out make sure that's working good and we have our stock ID and we have two stock IDs look at that that is beautiful and I'm going to go into here let's see I'm going to get pull the individual Tesla stock and let's see if we also get our comments with the actual ID and would you look at that we now have our comments 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,421
Rating: undefined out of 5
Keywords: software development, programming, engineering
Id: J1VuY2owXo4
Channel Id: undefined
Length: 8min 33sec (513 seconds)
Published: Sat Jan 13 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.