ASP.NET Core Web API .NET 8 2024 - 6. POST (Create)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so the whole entire point of any piece of software is to create data whether you are creating users whether you are creating stocks you want to create as much data as possible and when we create data in the world of web development in the world of apis it's simply called a post it's a fancy HTTP verb for data creation and just from a 10,000 ft overview I'm going to explain to you the process of how this happens within the context of apis because apis are a little bit different from most traditional MVC based web Frameworks when we create data with an API what is going to happen is that you are going to submit Json to the server and then the server is going to use the code that you wrote to put this data into a database and that's really all that a post is but there's a lot of logic in between there so let's go into a little bit of depth about what is really going on when we post so when you post more than likely you are going to be using Entity framework an Entity framework comes with this method called add you are going to post this Json data that you created and you are metaphorically going to put it within this ad what's going to happen is that Entity framework is then going to start tracking it but this is the key point it's not going to save it into the database I repeat you need to know this the ad is not going to instantly at the click of a button save it to the database it's going to start tracking it and when you start tracking it if you want to actually save the changes what you are going to have to do is run these save changes method and after you run the save changes method that's when it is saved to the database so just remember that middle part of tracking and everything will be okay anyways let's go ahead let's hop into vs code and let's start coding this okay okay so let's go ahead let's go inside of our controller and let's start building out the controller endpoint and the way that we're going to do that is very simple go here we're going to type in HTTP and remember we want a post do not post do not put a get because it will not work correctly if you put a git okay so next thing we're going to have public I action result same exact thing um we're going to create going to put from body now remember you need this from body because our data is going to be sent in the form of adjacent we are not going to pass the data through the URL we are going to actually be passing this in the actual body of the HTTP so remember that if you don't have the from body it's probably not going to work the second part is that we are actually going to create the request portion of our dto we are going to create a request dto as opposed to the response dto that we just created now remember this request dto we are doing this because we do not want the user to be able to either now or in the future submit all the data there are certain types of data we do not want from the user and you need to create dto because if you have the user submit this whole entire model like if the user tries to submit this whole entire model and your API says submit all this data your user the front-end developer whoever is in charge of sending data to this controller endpoint is going to freak out because a lot of that data they're probably not going to be able to get so this is why we create dto so first let's go into our dto right here we're in stock and I'm going to call this create stock request ETO create stock request dto and go ahead paste that in there and the data that we want to put in it is pretty much everything except the ID so let me show you if you don't know what I'm talking about just go ahead go into here we don't want the comments we don't want the ID so if you didn't see what I did there I went inside of my stock model got everything except the ID we actually do not want the ID in this case so we are going to go inside of our create stock request dto going to go here going to go ahead Spruce this up looking good we do not need the ID backspace that go ahead hit save now we have our request dto now what we need to do is before we do anything we better just create a mapper for this so let's go inside of our stock mapper here and what I'm going to do is go into here I'm going to say public static C2 stock from create dto and I try to make this named as appropriate as possible but some of these dto do have really crazy names I'm sorry okay so here we here we go we're gonna and we're going to take in our create stock request dto we're not going to take in anything else so go down here and we are going to return a new stock and we need to do this because once again our dto is in the dto type and when we actually submit data to our ad it has to be in the form of a stock model it's not this ad is not going to allow us to be able to submit the create stock request dto this context this actual method that Entity framework provides for us whenever you pass any data into it to be saved it cannot be in the form of a dto so this is why we have to make these mappers okay so we're we'll go symbol we go stock dto and we'll just go ahead and we also need to go into here we need our stock dto so we'll call the stock dto and also need to uh lowercase that so now we finally have our symbol let's go down here we say company name so we go down here we'll say company name go equals stock dto go company name go down we'll have our purchase so purchase is equal to stock dto do purchase and and last div is equal to stock d. last div go industry is equal to stock d. industry then we'll go into here we'll say market cap is equal to stock dto and we'll go ahead pass in our market cap and we should be good to go on that so we finally got all this figured out now what we need to do is we need to actually go within our stock now we need to go within our create and we need to uh go ahead and first we need to create our stock model so it goes stock model and we will take in the stock dto and then we are going to go ahead and flip this back over flip it right back on its back so now that we have it mapped over what we can do is we can actually just go ahead and add it so we're going to go ahead and go into our stocks here we're going to go add and then we are going to pass in our stock model after that can't forget we need to do our save changes save changes is not actually tied to anything it is not tied to any table it's just a part of the actual context and then once we get done with this what we're going to use is our Nifty created at function so if you don't know what to created at function let me just type it out a little bit and I'll explain it piece by piece so let's just go into here going to go ahead use get by ID we're going to have new here we're going to have ID is equal to stock model going to say ID go ahead pass that in and then we are going to say stock model. 2 stock dto and last thing I need to go up here and add dto to this so what created at action is going to do is that it's going to run this and is going to pass this data in the form of the ID so once this is actually done it is going to go up here it is going to execute this get by ID it is going to pass in this new object right here into the ID and after that it is going to return in the form of a two stock dtoo so let's go ahead and run this and let's see what it looks like also need to make sure I'm within my API so I'm going to go CD api. net watch run and there we go okay so let's go ahead into here and we now have our post API picked up so the great thing is that because we created the create request d as you can see here our ID and our comments are not showing up this is both a boon for us and a boon to the front-end developer or whoever is sending this data because it provides a clear specification of what we want and does not provide anything that we don't want so what I'm going to do is just go ahead and try it out and see if I can store all of this just like it is so go ahead here and yes we now have an ID of 1,000 I don't know why that ID is so high but it did store the rest of the data so let's go ahead in here see if it went ahead and stored it up at the top and as you can see our data is being stored here down at the bottom we've got our dto we've got our dto making look everything look awesome and that is going to be it for the create request and we're going to start moving on to update and delete after this anyways hope that you guys enjoyed this if you did make sure to smash that like button smash that subscribe Button as always thank you for watching watching
Info
Channel: Teddy Smith
Views: 2,322
Rating: undefined out of 5
Keywords: software development, programming, engineering
Id: yxQ9CSwc_uk
Channel Id: undefined
Length: 10min 0sec (600 seconds)
Published: Sat Jan 06 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.