ASP.NET Core Web API .NET 8 2024 - 14. 1-To-Many CREATE (Post)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so we've already figured out how to get comments to read from the database we can get this data out and we can not only get data out but when we get our data out now it's actually tied to our stock and we are now aware that the stock is the parent and the comments are the children in this primary key forign key relationship but how do we actually create comments and how do we create comments so that when they are stored in the data base we automatically form this relationship well take a look at our pseudo code here we have some pseudo code and when we create a comet it looks a little bit different than our stock when we created a stock before we didn't have to have another stock or we didn't have to actually have a comet ID we could just go ahead and throw in our stock but when we create a comment we have to tie it with this parent so what we're going to do is we are going to pass up our our stock ID and there's no real way around this because whenever you are going to have this relationship whenever you're going to have comments on your stock or whenever you're going to have comments in your blog this ID is going to exist this parent ID is going to exist and this is pretty much how you're going to tie them together and when you go inside of your actual comment model take a look you have your stock ID we don't have to place the whole entire stock navigation object the reason that this stock ID exists is because this is the foreign key and I'm going to go ahead and highlight it so that you can see a little bit better this is the forign key and we are going to use this forign key when we actually form our relationship up here we will tie it to the primary key of the stock and we can input multiple comments we don't have to just stop at one comment we can add as many comments to this actual stock as we want to all that we need is the primary key but but let's go ahead let's hop inside VSS code and let's get coding so the first thing that I'm going to do is I'm going to go ahead and hop inside the controller and let's go ahead and start on the HTTP post here I believe yeah it's a post not a create so we're going to go down we're going to go public async of course and we going to go I action result make this a create and first things first trying to think what I want to do let's go from Route and we will go ahead we're going to have to add our stock ID because we need to have the parent remember that this is the parent and if we don't have the way to actually tie it or if we're going to create a comment we have to have the parent sometimes it is optional and you can go without this or there are certain circumstances but if you're going to have a comment it's going to have to be attached to a stock in our case so next thing that I'm going to do is since we're going to have to bring in this stock ID go ahead make our uh curly braces here go ahead add our stock ID we're going to go ahead add curly braces here then the next thing that I'm going to do is we're going to create a dto for this so we're going to call this create comment dto so this create comment is going to be very similar and I'm just going to go inside of my Comet dto and just get the title and the content we don't really need much more than that because whenever somebody is oops accidentally posted it in the wrong thing because whenever somebody is actually creating a comment all they're going to need is the title and the content and the other uh created on will be taken care of by the actual uh machine or the C code I should say all right so we can go ahead go back and since we have that now all that we need to do is paste it into here so create comment dto and we'll just call this Comet dto so the next thing that we need to do is we're going to have to check if this stock exists if the stock doesn't exist which is a very common scenario you immediately want to cancel out of it and the way that we're going to do this is I think it's better while we're here just to go inside of the iock repository so just go to your iock repository and we're going to create a little method that's going to do all the checking for us and it will say it's going to abstract away uh a lot of code and you'll always have this here because this is a super handy method to kind of have on hand whenever you're working on an API so we're going to go here stock exist and we're going to pass in the in of ID we're going to go back to our IO repository go ahead implement this interface we'll go down here and we're going to go ahead and implement it and it's very this is a very easy method to implement we'll go return uh the context we will have stocks and we will have any async so we'll have Nea sync n s um go Arrow ID is equal to ID and all that any is going to do is check if it exists and if it does exist it's going to return a null or it's going to return a bull depending on whether it exists or not so next thing that we need to do is go into here we're going to go ahead and bring this in so I stock repository and we're going to call this stock repo and we'll go up here make the private so private private please all right read only we're going to go I stock repository equal to stock repo then we're going to go down here do the same exact thing so stock repo is equal to stock repo looking good so now that we have that watch this this is way easier this is pretty sweet looking method as well too so go stock repo go stock exist and we'll pass in our stock ID looking good and if it is returned bad what we want to do we're going to go request is equal to stock does not exist okay now what we're going to do is we are going to map this back from the actual create comment dto and we're going to map it into a regular comment so we're going to have comment model and we need to take our comment dto and we need to create a mapper for it so I'm going to go up to the comment mapper I'm going to go up here I'm actually just going to copy this so go copy this down we don't we want this to be mapped to a comment so we're going to say to comment from create from create and we can kind of insinuate what we want because we're going to be we're going to be taking in the actual com we're going to be returning a comment and this is going to be a create comment dto so create comment dto we'll call this comment dto and then also here we can take in the stock ID and we can just kind of build this as we go so go here and change all this over so we have the ID and we don't we'll go ahead we'll get and we'll go ahead get rid of the ID we need the content we don't need the actual created on and we do need the stock ID and we're going to change this to the actual stock ID so stock ID is equal to comment dto and we'll go ahead change these out and we don't want to return we want to return a comment not a comment dto okay so let's go back so we're going to go to the comet dto and then we're going to map it to we're going to use the mapper that we just created so to comment from create and we're going to pass in our stock ID looking good then we're going to await we're going to go to our comment repo we're going to create a sync we haven't actually made this yet so we need to go make this very quickly so go here we're going to go task so task we're going to have comment and it's not going to be optional because creates don't really return optional got async and then we're going to pass in our comment our comment and we are going to pass in comment model there we go looking good okay so go to comment repository then go control dot now what we want to do is same thing that we've always done we're going to take in context uh comments so we're going to go comments and we're going to go add async add async we're going to pass in the comment model then we're going to go into here and we're going to go context. saave changes async and then after that we are going to return the comment model because it is a reference and we can do that okay so create a sync so then we're going to pass in our com comment model looking good then we will return created at action name of and we're going to pass get by ID then we will return new going to go ahead pass in our ID so that it will be able to find the actual comment then here go ahead comma pass in comment model and we'll say to comment dto so that it returns it into the form that we want and I think that's it so let's go ahead here and let's make one for the Tesla stock again so let's go back and make sure that we are within our API so net watch rut okay so let's go ahead into here and let's find a stock that we want to add to and we'll go 21 and we'll go to the comment go ahead looking good so it's attached to that stock let's go ahead get all of our stocks oh gosh look at that we now have our string string it attached to the right ID let's go ahead and test out Microsoft so let's go into here we'll do the same exact thing we'll make sure that's oh look at that we now have a comment with our actual Microsoft stock so that's create hope that you guys enjoyed this if you did Smash that like button smash that subscribe button and as always thank you for watching
Info
Channel: Teddy Smith
Views: 4,810
Rating: undefined out of 5
Keywords: software development, programming, engineering
Id: iUi-bQR-Ywg
Channel Id: undefined
Length: 11min 53sec (713 seconds)
Published: Mon Jan 15 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.