.NET Core 7 Entity Framework Project: Building Controllers and Endpoints testing with SSMS Swagger

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign in the previous lessons we have completed all unnecessary steps to prepare the project for endpoints and controllers including the SQL database migration and Creation in this lesson we are going to create endpoints and controllers to be ready to perform crude operations with the database in the project folder let's right click on the controllers folder and choose add controllers then select controller empty from the API section as you may remember from a few lessons ago these steps are pretty standard and well known to you we need to name the controller and the controller suffix in the name is required to have this file recognized by netcore as a controller I'll name it a solar systems controller I'll quickly remind you that the controller work in the attribute will be replaced with the actual controller name without the controller suffix from the file name and this is called token replacement so this endpoint will look like localhost then Port API solar systems Additionally the API controller attribute which is also provided during controller creation is responsible for automatic model validation if the incoming request data doesn't pass the model validation the framework will automatically return a 400 but request response with details about the validation errors now let's create the action method it will be a get all request so the action method named get all will also be decorated with the HTTP method type HTTP get and this method will be filled with hard-coded data to try out the endpoint in Swagger before we proceed with the endpoint any further I will create a list here and store it in a variable called solar systems and our list will contain all the necessary properties will be declared in the Solar System model if you cannot remember the list and its types you can simply expand your model and all items will be available here in the solution Explorer I will create the required item in accordance with their types and the image can be taken from any resource you like or skipped since it's nullable so the first set of data is ready and now let's duplicate it to add the second part of the data at this stage you can use any data you like since we will delete this data as soon as we have tested the end point and of course we need to return something so let's read on the arcade method including a message and the data itself then point logic at the moment is minimal we just need to test it first and then extend it with additional logic but let's build it and now we need to verify the result you can use any tool you like either Postman or a Swagger I will use Swagger so we now have these three controllers here in Swagger the first one weather forecast which was provided by the net core template when we started the project the second one planets which we created a few lessons ago and the current one we just created first we can click get and after receiving a successful reply we can click try it and then press execute then point has replied with the hard-coded data and the message so we are good to proceed and extend the controller with real logic to get the data from the database to do that we can use the injected High Kai talkdb context class which is available to use inside the project first we need to create a Constructor and a field we will get the high kaito give a context injected into the Constructor and then make it available using a private field we don't need the hard-coded data code any longer so we can delete it since we have an injection of our high calc DB context which is actually a reference to the properties inside the high kite ogdb context class these three properties are available for us by their names let's use them I will simply write High calc DB context solar systems to list also since the return statement already contains the variable name there is no need to make any changes there tracing the chain from the high k2db context class via the Solar Systems reference which leads us to the DB context and we can access the database directly let's open Swagger again press try and execute we received this reply and of course the data is empty since the database is empty now let's open SQL Server management studio and open the solar systems table just to confirm it's empty and indeed it's empty next we will test to verify that the empty reply in Swagger is coming from the database to add some simple test data we can write a quick query I have prepared this query selected and executed then select again and execute it now the database is updated with the hard-coded values if we go back to Swagger and send the request again we will get the reply accordingly and the data we have just hard-coded will be displayed so we have successfully completed all the steps for testing the endpoint including the database query in the next lesson we will extend the logic of the controller and Implement a search within the database using an ID thanks for watching if you have any questions or need further assistance feel free to comment below or don't forget to hit that like button and subscribe to our channel for more great coding content stay updated with the latest videos by ringing the notification Bell happy coding
Info
Channel: hikitoc
Views: 111
Rating: undefined out of 5
Keywords:
Id: pAzMexQ_fHE
Channel Id: undefined
Length: 5min 58sec (358 seconds)
Published: Fri Sep 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.