REST API in .Net Core 3.1 || CRUD APIs .Net Core 3.1 with Entity Framework || Interview Point

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends this is dk bottom and thank you for joining interview point so in this video we will see how to create apis in asp.net core so first i will create a database in the database i will create a table in that table i have to show you how to create how to perform cloud operations on that table using web apis okay so first i need to create one database so i will use this command i have to open my database sql server i have open the sql server ide so i will use this command and i have to execute this command okay the database is created now i want to use this database to use this database i have to use this command use database name okay and after that i have to create one table with the name of the table is employee with the primary key so id is a primary key and it is the auto incremented by one and name and we are having three fields name department and salary okay so these three fields are there in that table now execute it so this is done now now i will uh insert by default one data in this table so i will show you that i'm just going to insert this data in that table now we can see the data is available in the table with the id1 okay so in that i have not used i have not inserted id so id is the auto incremented field now i have to create one project called asp.co api okay so i will give the name of that project is uh web api core okay so first i need to open the visual studio in that i have to select the option create a new project and after that you have to left side panel you have to select ac dot core web application here in that in the right side you have to select asp.net core web applications so here you can create project templates for creating asd.net core web apps and web apis for windows linux and mac os using.net core or dot net framework okay so you have to select this template and click on the next button now give the name of that application so i will give the name of web api code i have to select the location okay so this is the d in the d drive tutorials folder so just click on the create button now you have to select the api template here and click on the create so it will take few seconds to create it it's done what i have to do here is i have to create two folders so right now we can see the structures dependency and properties folder and controllers two folders are available and here there is no web dot config file available in the dot net core so here we are having app settings dot json file so we are here so in the dot net core we are having app settings dot json file instead of having web dot config file so there is one program files and a startup file so this starter file is very important and this is the default uh available we are having that weather forecast so they are creating one by default one controller so i am not going to use it i will create another one okay so i have to create two folders here so i will give the name as models and another one is actually i have to create three folders in this so let let me create the three folders in it so add folder so first one is model second one is services services folder i will use to create like my business logic okay so guys uh this uh i am going to tell you that how we are working in the real time so you have to follow these steps okay in the real time you have to create our your application structure like this so that the same structure i am following here okay so i have to create i services so i will create all the interface in this folder and models classes will be here and services classes will be in this services folder okay so after that what i have to do here i have to uh install some packages okay for that i have some like now add required references to work with sql server data so we have to install these packages okay this one so now i will show you the steps so go to the tools here and go to the new package manager and here select package manager console okay and here you will see in this window here type here the install package microsoft entry framework core dot sql server okay so first i have to install this one now click on the enter button so it is going to be installed now it's successfully installed now use the second second one so it is required so i am done with this okay after that what i have to do is i have to uh like create a services class in the service folder and model class will be in the border folder and interface before that i will use a connection instead in this app settings dot file so before that i have to create the model class okay so for that i will use one command so the command that i am going to use is this one so in the in this console manager you have to type this scaffold db context here is the your server name sql server name that i got from here like connect this is the server name okay so here login and password is also required so after that uh database name is api code db that i have just created api code db okay and this is the sh user id and one two three four five six is my password so i'm also providing the output directory will be the models folder okay so that i have already created okay now click on the enter button so it will create motor classes for us now you can see there are some changes in the there is nothing in the models class available now let's see after building it is started building now uh it is succeeded now you can see in the models folder we are having two or new classes that first one is api code db context class okay and another one is employee class so now you can see the employee class is having four fields id name department and certainly that the table that i have created with the four fields so all the fields are available in that class okay okay guys so these are the so this db context class is very important for us we are not going to use this so i can uh delete delete this one okay guys i can delete this one now i have to do the changes in my startup.css file so guys before going to do the changes in this file i have one suggestion for you like if you are looking for minor and major project ideas and technical videos then you can check this channel my digicraft there are hundreds of video on various topics on my digicraft okay so basically the channel my digicraft is about the latest project ideas for computer science students they are also making the videos on html css wordpress development like java fx design programming languages like java php reaches and so on okay so and we are having like uh major products ideas minor project ideas and how to do the various things in the computer fields and technical video related to the programming and the computers okay so you can subscribe this channel there are hundreds of video that will help you for like you have you can check the playlist so that will help you in your careers before going to do the change in this file i have to use the connection string in this file okay guys i have that copied the my connection string so you have to use like this so this is the connection entering my server name sql server name and database name and user id and password that i have just created after adding this connection string i have to add one class for the services folder and one uh interface for the i services folder okay so i will create one class uh the name will be like employee service so right click on the services folder go to the ad and select the class and give the name as employee service and click on the add button or just click on the enter button okay so your employee service class is created now i have to add the interfaces so add go to the new item and select the interface template and give the name as i employees so naming convention is very important okay so now we are done with this so make it public i have to implement all the method of this interface in this i service class so i have to inherit this i employ i employ service okay so nothing is there as of now so i have just creating the inheritance okay now i have to write some logic inside it and also i have to write the function that function we are going to work with so now our app settings dot json file work is completed so now i am not going to use it now i have to work on the startup.cs file so in this file as you guys know that asp.net core is working on dependency injection so we have to use the dependency injection and we have to use it so to do this i have to inject something here so for that i have some written some code and i am just going to use it so like i have already created it so after this line i have to add this so this is a services dot add http client okay so so that we can work with the http client and after that i have to use this uh api code db contact class so that is available in this model folder this one api dot okay so i have to use this context class okay and so that it can inject the dependencies so that we can use it for getting the database connection so we have to uh required some namespaces so we have to use these name instances okay so now it is added now so this is the interface and this is the server service class that we have created so we have to add the name issues for them or as well okay so now it is done now error will be gone so now you can see now uh there is no error now build this project now i have to add the survey so build is successed now what i have to do is i have to create some uh service code here so before that i have to create some functions inside it okay so i need these five uh function in it so these all the five function must be implemented by this class because this class is inheriting this high employee service interface okay so in this class so let's uh do one by one let me comment this one as of now i'm not going to use so it is giving me a error so i have to use the name space for the model okay so now it's done error is gone now what i have to do is i have to write i have to implement this method okay so this method what is it written type is uh employee so i have to create this function so guys before going to implement this function in the employee service class first i have to create the constructor of this so i have this api code or db contact class so i have to create this object and we have to assign this object okay so i am just creating the instance of this okay so this is now ready to we you so now we can use this tv context as a reference and what is the error here okay so now we have to implement this method unlike this one so that's why it is giving error now you can see now we are having this error when i will create this function here like now error will be removed from here like i have to use public so now you can see the error is gone here so what i have to do here so it is required uh return type as employee so i have to write code as so we have to create one variable called employee okay so we will use db context class dot employee dot to list okay so we will get this list return employee now error should gone you can see now what i have to do is i have to create one control i think i have not created it so i have to create one controller called employee i am creating empty controller name it as employee employee controller okay so guys this high employee service this is the interface so i will use this interface and to use this in the controller i have to create the instance for that so first i have to create one reference variable and that reference variable i have to assign by using the constructor so to do this i have just written the score private i have created one private variable called employee service so i have to use the reference for this as well now error should have gone now you can see error is gone so this employee service this reference is available for us so now we can use this reference to call this services okay so now i have to implement the function uh this one get employee to do this actually i have already written the code to speed up the process so i'm just going to copy and paste this code so now this is the http get method so i this is the employee class that we are using so innumerable employee will be the return type so get employee so now we can now run this product and see that we are getting the expected output or not so run it we will see this so i will use this url here this one so by default we are getting the weather forecast report paste the url now we can see the controller jump to this get employee method and now we should get the employee list so right now we are having only one record so we can see we are getting one record now the same thing i have to test on that uh api part by using the postman so this is the get employee method the get employee api so i am using that local host so the same url i have to use it so my port is changed okay now choose this now let me check the breakpoint is coming here so now we can see one record is returned okay so the employee name is zone department is dotnet and salary is 50 000. okay so now i have to implement this method add employee to do this so what i have to do this first copy this and paste it here and now you have to change it here post okay so the add employee name will be first rest of this so now we have to add this as where now we need to implement this add employee so first we need that employee class and we because we want this employees should be the so we have to add this employee record before that what i have to do is now i have to implement this interface add employees now we have to implement this one to do this just copy paste it add it here okay now return type will be the employee so make it public now so now we have to add the code here to add the employee into the database so if object is not real then we have to add this employee record into the database and after that we have to save the changes and we have to return that employee the currently added employee okay now let's check this one as well into the database so add employee return table with the add employee so it's the written type is employee okay so we have created this post api in the ad employee api and this is done now we have to run it and now i am going to check it in through the postman so now let's check open the postman add employee so i am providing the by default data is name is lucy department in java sally is 35k okay so by default zone employee is there there is nothing in the database okay okay now i am going to one more record the called lucy okay now add employee click on the send button so you can see employee record is here by default id is zero so it will create the id i hope record will be created now we can see the record is created with the id 2. now we can see the same record into the database execute it now you can see lucy java 35k is a cell now i have to implement this uh edit and delete api as well first stop this program now uncomment these services this function and we have to implement these functions into the service first let me copy with this paste it here now and employee so here will be also the edit employee and this will be the put http port and for this one it will be a delete and change the name it as delete copy this one paste this one now we have to add this function as well edit employee and here will be the delete employee for deleting the employee we need id so i will make it integer type and id okay and here will be id will be the parameter for the edit employee i think this is not available so actually we have created this update employee so that's why let me edit this one now implement these functions update employee and delete employee so to implement this function in we have to implement in this class so that's why we are getting error so uh copy this one copy this one and paste it here actually we don't want he this thing okay so what i have to do here here update employee and here the employee record so we have removed the unwanted code now what i have to do is i have to just update this request what i have to do with context and we have to use the entity tv context dot entry entry of the employee this object we have to pass here dot state is equal to state entity state dot entity state entity state dot modified okay so actually this name space i don't want to use it i will use on the top of here to do this just so portion potential fixes and just click on it so it will be the reference will be added here now we have to delete the employee to delete the employee we have to create one method called delete employee to delete the employee we need id of the employee so integer id and id so entity state will be the deleted first what i have to do here i have to get the employee based on the id so create this fair employee object is equal to dbcontext dot dot employee at first or default and get the employee x dot id is equal to id okay so we will get the employee based on this id provided id and then we have to deal with this employee okay so we are done with this update and delete employee now we need to check this apis as well okay guys so this is now done now run it so it is running now by default we will get the weather forecast okay now i will check this in the postman to do this for edit employee what i will what i have to do here i have to add that loose record lucy salary is 35 initially now i will make it 40 000 okay c is in java and her id is 2 okay so now just click on the send button so make sure this is the edit employee id so we can see the sentry is updated and we can see the same thing in in this okay salary is updated now i will delete this same employee okay same employee i am going to delete so now change the port api employee and delete employee and id will be the two so the employee id of blue cs2 so i'm just going to delete this based on the id so we can see id this record is deleted successfully so now we can see in the database there is no record available with the name lucy okay so this is how we can work with the apis now i'm just going to create this api as well so this one get employee by id so it is very important api as well so let me create the function for this and i will say need to stop this now make sure the name will be the same otherwise you will get the error id and you have to get the record based on the id by using this line okay and that the same record you have to return to the user okay so this is now done and we need to do the same thing to into the controller employee controller to do this and add one more function action method here get employee by id id so here same get employee id okay now we need to check this as well and this will be the type of get okay so actually this is not added into the postman so i will show you how to add and call this first need to copy this url and now add one request here request api name get employee by id save to test api so this is the get tab enter the url but my url will be the api dot employee dot get employee by id to paste it here and i want to pass the id of this employee so id is equal to let's suppose id1 okay so i i want to get the record based on the id so let me check this is running now hit this api so let's see we are getting the record or not we should get the record of zone so now we can see we are getting the record of zone okay so like this we can perform and we can create the apis in asp.net code guys if you have any doubt you can ask me through comment i will respond to you as soon as possible i hope you enjoyed this video we'll see you in the next video guys if you haven't subscribed my youtube channel please subscribe my youtube channel for more videos you can follow me on my social media facebook linkedin and blogspot all the links are available in the video description thank you guys
Info
Channel: Interview Point
Views: 13,909
Rating: 4.9170122 out of 5
Keywords: Core API 3.1, Core CRUD API, Web API in .Net Core 3.1, Web API in .Net Core, How to write API in Core, How to write API in .Net Core, Writting API in .NET Core 3.1, Writting API in ASP.NET Core 3.1, .net core api with entity framework, .net core api authentication, asp.net core 3.1 web api authentication, how to write .net core api, REST API in .Net Core, REST API in .Net Core 3.1, Create REST API in ASP.NET Core 3.1, Create REST API in ASP.NET Core, .Net Core API
Id: p0bfCl0xvDY
Channel Id: undefined
Length: 30min 42sec (1842 seconds)
Published: Thu Jul 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.