ASP.NET Core Web API .NET 8 2024 - 12. Comment System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so previously all we've been working on is our stock entity and truthfully we've been taking our sweet ass time the whole entire way because we've been learning that changes in this video we're going to start building out our comment infrastructure and we're going to start actually building it as you would in real life we're going to quickly throw up the controller we're going to start wiring up the interface at the same time and we're not going to start putting database code directly in the controllers we're going to go straight for the repository we're going to chop off the head of the Beast so that we can make our controller look absolutely awesome so let's go ahead let's jump inside vs code and let's get working on it okay so we are inside of vs code first thing that I'm going to do is create an interface for the comment repository so call it I comment repository I comment repository that looks like good spelling to me and we're just going to keep things light and we are just going to add one method so that we can just worry about getting the infrastructure built we'll worry about getting all of our methods one by one and I think this is more realistic of how you would actually do things in the real world so we'll try to model things as they are in a workplace environment next thing is we are going to create the actual physical repository interface is just an abstraction but now we need to actually create the comment repository repository so I comment Repository that looks good go ahead and inherit from the uh comment repository go ahead control dot implement the interface we'll go into here and also C create a Constructor and just like we did before we're going to bring in our application DB context that we made so application DB context we'll call this uh just regular context looking good good next thing go up here and we're going to create our private read only uh property to bring it into the top so that when initializes it IT stores it up here and not application Builder we want application DB context and we're going to underscore context this okay then go to here and we're also going to do the same thing that we've always done kind of repetitive but you got to do what you got to do all right so the get all is going to look very similar to what we had before so go here then delete this and then we are going to return and we're going to await going to have context comments and we are going to to list a sync okay that looks great so before we actually do anything else while we're here let's go into the actual program.cs and also add the dependency injection for this this I've actually seen this work without adding this but I think it's still recommended to add this so we're going to go into here we're going to do comment repository repository just like this and we are good to go next thing is we're going to create our controller and after this we are pretty much good to go in as far as infrastructure goes so we're going to say comment and we're going to go comment controller and remember as e with each n we want to create separates for everything because this is kind like I said it's kind of repetitive right now but once your code base gets growing this is going to be a godsend okay so we are inside the controller and the first thing that we want to do is add our controller base remember to add the controller base before you add these annotations to the top or else you're going to get little red squiggly lines that are really hard to go away that are really hard to make go away so that looks good then we're going to do API controller looking great so we've already got our actual repository wired up we've already once again taken care of a lot of what we need to taken care of get taken care of beforehand so we're flying here we'll go comment repository and we'll say this is the comment repo then we're going to go down here we're going to do our C and this is where we're going to bring in our I comment repository again and is going to be a regular comment rep regular comment repo not repository uh you could put repository but I think it's kind of it's a long word and it's very redundant opinion you can you can call it whatever you want all right so go down here we're going to go get and say public and a sync task and we're going to return an i action result then we're going to say get all then of course we're going to go down here so we actually have a lot of this code built out we're going to have to create another dto for our comment but that is also part of this infrastructure building process is to also create uh the dto for it as well too and let's go ahead first let's go down here and go to our dto we don't have any common dto we're going to go ahead in here and we're going to create our comment dto and our comment dto is going to be really easy to make because we can just copy and paste the model and that's exactly what I'm going to do I'm going to go inside the comment up here and copy this go inside of the comment dto and pretty much just copy and paste everything within here except we don't want the actual navigation property that's one thing that we don't want so let's go ahead get take out the navigation property and we don't want the navigation property because that's going to inject a whole entire object within our comment DT and it's going to make our comment dto look look really bad once it returns it would literally be like returning the actual stock each stock individual so you could do that if you want to but I think having the ID in there is a lot easier and probably a lot more appropriate the next thing that we need to do is we need to go inside of our mapper and we need to create a comment mapper so I'm going go here I'm going to call this comment mapper looking good and we are going to make a two com comment dto we won't have to actually change a lot like I said but it's good to just go ahead and create this dto beforehand because we're definitely going to be using a dto down the line so the first dto is going to be pretty difficult because we don't have anything to actually model it off of but if you want to just follow along I'm going to go ahead and just kind of type this whole entire thing out like I said not not the best I'm sure you could copy it but I think by the time we get done actually uh copying and pasting a stock mapper over it's just going to be just as good just to go ahead and type it out so I'm going to say this and to go comment we're going to be uh turning this to comment dto from a comment so say comment model then go down here wait a second do I actually do something up there no okay so we're going to go down here we are going to return a new comment dto and within this comment dto we are going to have ID comment model we going to have an ID we we do want the ID we also want the title we want everything except that whole entire navigation uh property so go here we're going to say title then go down we're going to go content comment model. content then go here we'll say created on go comment model dot created Created on and need to go ahead bring this in right here because I don't think I'm getting the type checking and yeah I've actually misspelled that because I it wasn't detecting it so go into here we're going to say created on and we do want the stock ID so go stock ID is equal to comment model. stock ID and go down here close that out and also I'm going to double check one more time to make sure that I got everything and yeah looks like everything is good to go and now we can go ahead in here and do our select map so we're going to turn these comments that we are getting back from the database we're going to turn them into dto and we we'll say comments and we will select into it and remember a select is just like a JavaScript map uh it's going to return a whole entire new uh data structure instead of actually manipulating the actual one so go into here and we are going to control Dot and what I need to do is go into here public static go ahead and make this static because this is not that wasn't working so if you add I forgot to add static to the uh comment Meer so go ahead and make sure to add static you might have already got it but that was what was causing that red squiggly line okay so now we're going to go down here we're going to go return go okay and then we are going to go ahead toss our comment dto in there and we should be looking good so before we do anything you don't need to seed this uh we're going to add a create here in a second but if we're going to test this we need to go ahead and add a couple uh or we need to add a couple comments to the actual database just to be able to test it in a second we'll actually add real comments but now we can just get away with just adding some test test comments so I'm going to go go test comment and say say content is equal to uh test content not the most creative and we'll add a stock ID of let's see here 21 we'll we'll say that this one is from Tesla so go into here also need to go ahead and add um a uh date into here as well too okay and it automatically does it too so let's go back and restart this so go ahead we'll go net watchrun let's go ahead test it to see if it is working so I'm going to go up here I'm going to go to my comment I'm going to go try it out and the moment of truth yes it works okay so we are good to go 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: 3,183
Rating: undefined out of 5
Keywords: software development, programming, engineering
Id: A3tdyk68KAw
Channel Id: undefined
Length: 10min 54sec (654 seconds)
Published: Fri Jan 12 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.