.NET Core web API Realtime Example - Invoice / Sales order API using VS Code | .NET Core 6.0 course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome to nigeria techies this is our asp.net core tutorial so part of this video i am going to create one real-time application the application is sales order apa the same service you can utilize for this kind of sale application also after applying some customization for this development i am going to use completely studio code if you are using visual studio 2022 also the application creation and debugging only different other than that the steps are almost similar so let me create my application so in the command prompt we have the command for creating this application dotnet new and our application is baby apa that's fine so basically this command prompt will create the application okay great the application is created now let me open the application using visual studio code so i created in the folder of sales order apa let me run the application we can see the basic output in the terminal we have the command is called the dartmouth run okay our application is running the port number is seven double one eight so in the output we can see the default controller name and also the method name so now we can start our development so before going to the implementation let me explain my database structure so in the database i'm having the tables are the category customer product role sales order header sales product information and the user so basically this user for uh login purpose and for role based authentication i'm using this role and these two tables are very important one first one is the sales header and another one is the sales product information the first table is a table sales header basically it will store the summary of the invoice it include the customer information and the second one is product information so it will store the product information the single invoice may have more than one products that information we supposed to store in this table so other tables are just a master table for getting the product information we have in product and the customer we have the customer table this category we are mapping into the product table that's what we have created our table so now let me go to the application here i am going to install this entity framework because connecting to the databases i am going to use this entity framework online so just to press ctrl shift p so it will open this tab so here you supposed to select this new get package manager add package okay it will open the windows like this the first thing is you how to install entity framework select this entity framework core and the version you have to choose the six and follow the same step the next one is design so design is included and the third one is sql server so a skill set also got installed the file only stools okay great the packages are gets included now we need to restore everything so let me use the command is document restore okay the packages are restored now we have to generate our database models using this scaffold command so the command structure is first dotnet entity framework then db context scaffold so then you have to provide your connection string so once it's done you have to provide the provider name microsoft entity framework or sql server and the output path where the output supposed to generate the models folder even they we don't have this model folder it will generate the model folder basically so let me execute the command so the bullet gets started okay great the model is generated but it's giving the warning we need to move the connection string into absolutes that's what the warning is saying so let me go to the folder here we have the database context sales underscore db context so what are the tables we have the tables are it's just included here so based on the tables we have the individual models also so in the db context we are having this connection string we supposed to move into app settings.json so let me create the section for the connection string the steps actually be already seen in the more videos so again i'm following the same step only okay great it's added and also let me remove this section it's not required and the second step is we have to include in our program.cs file here i'm going to use some name spaces sales order ap dot models and using entity framework or microsoft dot entity framework and now let me register the database context here so builder dot services dot add db context so our database contact name is sales underscore db so options.ucsql server so here we supposed to pass our connection string so the connection string is already available in the app settings.json so let me access it so builder dot configuration dot get connection string off we need to provide our connection string name that's it now this is fine here after we can inject this database context in our controller and we can use it so now i'm going to create my first controller the controller name is customer first i am creating the services for the customer one of the master service after that we can implement for our invoice so let me copy the same content from here and we can replace it based on our requirement so instead of this weather forecast i am going to give the customer and the constructor said also i'm going to change so i am not going to use this logger option so we can use little later so i plan to cover in another video and also this private section is not required let me remove this guitar method also so it's saying the already contains definitions so actually we need to restart our omnisha that's it now we have to check our database connectivity so let me inject the database context here and we can verify whether it's working or not sales underscore db context so actually we need to include the namespace using sales order ap dot models okay fine so let me inject here so this dot database conducts decoder okay we have done now let me create one get all method so i'm going to use this synchronous programming also so the attribute routing also i'm going to provide the same name guitar here i'm just returning table customer dot dot to list a synchronous so the synchronous is not coming way because we need to include one more namespace using microsoft.ndt framework that's it it's coming and also this one all right okay actually we are returning the list of the records so let me include that also that's fine okay now our error got resolved so let me run the application and we can verify whether this connectivity is working or not okay create application is running so let me refresh it so here you see uh the new controller customer is added and also we have the one more method so let me execute it so i'm getting one customer name i hope we have only one record in that table that's what i'm getting like this so let me verify it yes so this is working fine and very important thing is we cannot call this database directly in the controller this is not a standard way so instead of that i'm going to create one container so let me stop the going to create one new folder is called container so in the container i am going to create my first container is customer container so click class container that's it so here only i am going to follow the same steps for doing the connectivity for and everything so let me create one constructor here for doing this dependency injection and now i can copy the same database injection and i have to include here and also i need to inject it and the name space also missing using sales order ap dot models okay this is fine finally i have to set the values like this okay this is fine now let me create the function desktop table customer get all so as i mentioned this is the synchronous programming i'm going to use so i have to follow the steps same and these are synchronous we have to include one more namespace hope you all know we need to include this microsoft dot entity framework or that's it okay now the function is ready and for doing the dependency injection we supposed to have one interface also so let me create the interface also so the interface name is i customer interface dot cs public interface so here also i need to include the name spaces okay fine i just copied the declaration and i included here so now let me implement the interface here so basically we have to define our interfaces after that only we supposed to implement here for doing the first run i just did the reverse order that's it customer control okay this is done and also we need to include in our program.cs file for registering interface basically we have the three ways i am going to use this add transient the first is interface name a customer container and the implemented class okay we have done it so let me inject it so in the controller instead of this database context i am going to inject this container class container so let me replace the namings also so get done next okay now we have implemented this container logic so let me run the application once again okay great it's working fine the next very important thing is we cannot use this model classes for any time so in case in some situation we supposed to define some custom properties so whenever the database changes happen it automatically get changed that means whatever we did in the customized manner that changes or gets removed so instead of that we have to use the separate entity so let me create one separate entity class entity and my first class is customer entity so the properties basically we can reuse all the properties if you have any custom changes you just include it so here the code name address phone email these all are fine but this create user modify user this properties are supposed to not required and instead of this effective we need a field name is status name if it's active is true we supposed to give active if it's false we supposed to provide us the indictive that's it so we have created it and also i'm going to change the return type in our controller customer entity and we need to modify the container also and also the interfaces so the next step is be how to map the data into the object of tbl customer into customer entity so basically we can use this for each concept but in this dark net core we have the auto mapper so we can use it bar customer data so we are getting some data now what i'm going to do if not equal to null and the count of more than 0 then i'm going to convert it if it's not i will just return the empty object basically in this place we need to use auto mapper okay for using this auto mapper the first thing is we have to install the packages so let me go to the new get package manager and here this auto mapper so the first one we supposed to select and we can use the latest version that also fine and let me restore it so the registration also gets completed and we have to create on handler so in this handler i'm going to create one profile mapping profile so let me define the classes pop like so in this class i am going to use the name species using auto mapper now let me create one constructor so here i am going to implement on class is called profile so it's available in our auto mapper now let me add the x create map so in the create website we have to configure our from under destination classes so let me include one more namespace the apa.models so from classes table customer intro destination class name is the customer entity okay we have added so our next step is we how to register this mapping profile in our program.css bar automapper i hope yeah we need to input the namespace here also using automapper and then we have to use this mapping configuration add profile of new mapping profile and also i am going to use this eye mapper interface finally i have to include in the services so here after i can inject in our controller and we can use the functionalities of this mapper so let me inject it high mapper private read-only okay again the name space is missing and also let me inject here i am a part mapper so this dot map are equal to mapper that's it now what i'm going to do this dot mapper dot map so basically it's returning the list of data so list of table customer enter list of customer entity and here we supposed to pass this data customer data okay this is fine uh so let me return it here the idea is first we are fetching records from our table customers in the directory database so here the class is stable customers i am just checking whether we have the data's or not if it's don't have any data i'm just returning an object if we have a data i'm just mapping into another class is class entity and i'm returning it that's all about the concept so this is done and let me verify it basically these are the configuration related changes that's what i'm doing step by step if it's working fine for one controller we are not going to worry about this configuration related thing instead of that we can concentrate our logics okay great it's returning so you can see right uh instead of returning all the columns it's retaining the minimal columns i on the status name it's just written in null uh basically i supposed to return as the active or recite only so that also possible from our mapping side uh the concepts already i've explained in my previous video also for that we have the option is farm member so using this form number option we have to do some over custom logics so basically they contain two sections the first one is for destination so item dot status name for the status name column i'm going to ascend the value from i can item item dot map from yes start so here our field is is active basically this is set to use the boolean field but we are assigning value into string so based on the conditions i'm just checking if it's true i am going to provide the value as active in case if it is not equal to true then i have to provide inactive okay great so now we have done our changes so let me run the application okay the application is running so let me verify one second see now i am getting the status name value also ok now we have completed only one method in our master services now i am moving to the very important part that's all about our invoice controller so let me start it from our container side here i am going to create one new classes for invoice container so the public class okay this is fine and we should use our namespaces so the first namespace is sales order api dot models and also we need to use the mapper and the final one is microsoft dot ntg framework okay we have done it the similar way i am going to create the interface also public interface okay great now let me define the declarations in our interfaces the first method is for get all in my sweater and the entity should be invoice header actually i need to create it it's not there the first one is invoice header that's yes the similar way i'm going to create one more class invoice detail okay the two models are generated now we have to include the properties so let me copy the existing models so here also the create user app to create user let me take it the invoice header the name date customer tags up to the created it's fine included the same way i'm going to include this for our product information this product information up to the total it's enough okay invoice header the first method is declared and the second method is header by code but this is for getting the individual uh invoice details so it gets phone input the input is invoice number and the third one is invoice detail by code so it returns the multiple products for the particular invoice so let me change this window list off invoice detail and the third one is same method and also the end we should have one delete method also so save and the final one is removed for the save invoice we have to pass both header and detail also so let me create one more model class for this one so public class the first one is invoice cutter so this should be a header and the similar way we how the next parameter is detail but this should be a list okay this is created and also i'm going to create one more model is for the response type so it contains two properties the first one is a result and the second one should be key value so for if you are created the invoice as of now i plan to pass the invoice number directly in case in some logic will generate the invoice number once it's generated it will return automatically so we need to get the response so that's what i'm going to create one more property is key value so let me make both the properties that available okay then let me move on this container so in the interface the same method how so invoice entity invoice entity and the response type also our class name is response type the same way i'm going to change this for our remove method also so the remove method has the input is invoice number that's fine so let me format it now our idea is we have to implement these five methods so let me go to the container class so in the container class let me implement this interface and also we need to include in this program.cs file so in the program.cs file already we have included this customer container the same way i'm going to include this i invoice container also it's showing the error because we need to implement these five methods here so let me start from one by one so first i am taking this invoice header so i plan to make in the synchronous way only and we have to inject our database context so let me take it from our customer container and i need to inject like this and also i need to set the values so these are the very basic steps and this getall method is almost similar to our customer getall method so we can copy the content okay just we have included so instead of this table customer we are getting the data from our table sales getter okay data if data multicultural null and its count is more than zero then we have to use this mapper option and also i need to return invoice header for this conversion also i'm going to use the table name is table sales header can be added and this input also we need to pass this data okay we done it and also the very important thing is we need to include in our mapping profile also otherwise it will create an error so here our table name is table sales order header and also this invoice header so in this case we have to do the reverse map also the same way i'm going to create for our product information also okay we have done for one method the similar way i am going to implement the second function so this is the get by code function again the procedures are almost the same so let me copy the content once again so here we are going to take only one record so let me use first star default asynchronous item dot invoice number doubly called our input is invoice number and this is the single data it's not having the collections value this mapper also the list is not required because we are doing the map for single object okay we have done it finally even if you don't have the data also we need to return single object only okay great we have completed our second method now let me move on over that method get all invoice detail by code so i'm going to follow the steps same would like and let me copy the whole content once again here i am going to use the wire condition so here instead of this first or default i am going to use the wire because i will get more than one record and the table also table sales product information so let me convert into list okay we have done and now i am checking for this null and also i am going to check for the count for the conversion also i'm going to use the table sales product info into list of invoice details okay we have completed our third method the fourth one is very important one the same method here only we are going to include our most of the logics and also i'm going to do the dummy declaration for our delete method also synchronous for the save obsolete we have to interact with two tables the first one is header and also detail so let me create the individual functions for both header and detail so for this one let me create in the private method save header so the invoice header so result string dot empty and let me return it the same way i am going to create one more private method for saving the details okay we have created the dummy methods so let's start our implementation here so the first thing is i am checking this invoice detail is empty so if it's a yells we supposed to return as the the sierra type and the next step is i am going to check if invoice entity dot invoice header dot header not equal to null here let me declare one string also if invoice entity not equal to null so let me begin this transaction this start underscore database context the database that begin transaction so inside that let me call the methods so result equal to this dot save header here let me pass the input is invoice entry dot header that's it and we need to check the null that's what it's saying article tunnel so this is the first step and the second if condition should be first we need to check this result value not equal to nullar empty and also we need to check this invoice entity dot invoice detail i mean the details not equal to null and invoice entity details dot count more than zero if all the conditions are matched here i am going to call our detail say method so in the detail same method basically i plan to written the boolean so initially i'm having the value as false okay right and at the top of that i am going to declare one more variable process count zero now here i am going to generate the for loop here i am going to pass so this dot save detail just item if save result is true then what i'm going to do just incrementing this process count so once the for loop gets completed i'm going to check this invoice entry details dot count doubly counter our process count if everything is fine so let me finish the operation so this dot save changes are for synchronous so if i am using this asynchronous i supposed to use the submit also and also i'm going to commit it with the committee start coming so let me check yes here also i need to use the submit yes it's coming okay we have done so once it's done we have to create one more object the response sequel to new response type of so in the response type the result should be pass and the result key value is our invoice number okay we have done it's the else scenario been just need to revert it i mean we need to do this roll back and also for this response either we should have not returned anything or else we can give string.empty that's it so finally we can return this response so this is all about the concept now individually we have to implement both the same method the header and also the detail methods so currently b how the value is invoice header here i am going to declare one object table invoice header equal to this dot mapper dot map and our input is our invoice header object and we need to convert into our table invoice data and the input should be invoice cutter that's it okay this is fine so here first i'm going to check already we have the records or not for the particular invoice number bar head recorder the same code we already used in our get back code so let me copy it so instead of this invoice number i am going to use invoice header dot invoice number if it is already having the records it's here just update scenario otherwise we need to create the new record so here the concept is already if you haven't record we just update it if you don't have any record we need to create a new record that's all about the concept and also what i'm going to do i just create this try catch black because if there is any error we don't know what actually it's happening here so if we have this strike then we will know that's it first let me do the updation so header dot customer id equal to invoice header dot custom writing so anyway we have to give all the fields the customer name and the delivery address total remarks tags the total modified date and also the modify user so the modified date should be today date and the modify user should be our created user online and then that totally sneak total and the tags we have the field is tax then remarks and also total delivery address and this customer name for this ad scenario add asynchronous okay this is done so if everything is done we have to save it so currently we are using this uh transaction rollback and commit so in the main method will take care of all the things so in the individual methods we have to just pass the parameter that's fine okay great we have done our save header method the similar way i'm going to complete for this details so let me copy the items from our header table product so instead of header it's a detail so invoice detail and table sales product information so now we have the complete detail information so in this same method basically we have only saved there is no update scenario because every time we are updating the invoice uh user may remove some product also the possibility so in the header save time and plan to remove all the products belongs to the particular invoice so we can create the new records that's all about the concept so let me do it okay once it's done everything's goes fine i'm plan to return true and here also i'm going to use the try and catch black return or else throw the exception that's it okay we have done it as i mentioned i have to remove this detail records from our update side here what i am going to do first i need to get all the details so that's what we already did in this place so let me copy everything so instead of this invoice number invoice header dot invoice number so finally what i'm going to do so this dot underscore database context dot table and remove range of so that's it so now let me move on this delete scenario so in the delete scenario also procedure is almost the same first i have to remove the header after that the detail so anyway we have the code in handy so let me do it already i am written for this detail delete the similar way i have to do for our header also so in the header we have the get back code method already i'm just taking this one okay instead of this mapping so let me remove it and here also i am going to use the try and catch if there is no error so let me return it from here the top of that here i am going to provide the result equal to pass and also the key value should be invoice number okay great we have done our changes so let me stop and run once again okay great our application is running so let me refresh it okay actually we have completed everything from our container side but we not included anything in our controller so let me create the controller first so our controller name is invoice controller so let me copy the whole classes add everything from our customer controller so instead of this customer i'm going to provide the name is invoice and this i customer container i am going to provide i invoice container and also i am going to change this constructor name also and this one is invoice header get all head up so the first method is fine now let me create for the second method get all header by code so let me change the naming also get all invoice header by code and here we have one input invoice number and it's returning a single object only not a list okay great we have completed two methods now let me move on the third one get detail by code so here the return type is invoice data and also it will return the list of records because we have more than one product for the single invoice okay great the next one is our save method so in the same method we supposed to pass the form body and the input object is invoice entry and our return type should be a response type save let me pass the parameter also for this remove method we have the single input just invoice number online so once we have done these changes we can verify everything currently we not implemented this okay great we have completed all the functionalities let me format everything and also i'm going to run this application.net run so in the database let me format it both the tables okay application is running so we can test all the methods one by one so here the first three methods are fine all arcade method only but the fourth one should be the post method and the fifth one should be the delete method so let me change the http web also http post and also http delete and now we are good to go and let me refresh once again okay it's all are changed first i'm going to check this guitar header method so currently we don't have any data it's that's what it's returning the empty array now what i'm going to do i'm just to create my first invoice here the invoice number is inb 0 001 and it was dated 5 and this customer id is supposed to get from our customer table so this is my customer id and the customer name chris matthew so delivery address let me keep the cmt as of now and remark also empty the total value should be 100 tax is zero so then a total also thousand hundred only then the create user is admin user and the create date is today did only that's fine in the details section the invoice number i'm going to use the same invoice number and the product code i can get it from our product table p101 and my product name is cricket bet the quantity is one the sales rate should be thousand and the total is thousand i'm going to include one more product so here the product code is 102 and also the bar the amount should be 100 only quantity is 1 and the total also 100 so let me execute the function so here i'm getting the responses the results the result is passed and also i'm getting the key values is our invoice number so let me check from the databases so the database the invoice header i'm having all the details the delivery address and remarks i am not provided any value and the modification user and the data that also is null that's fine and the detail table we have all the information the only created date and modify user we not updated other than that we have all the values so that also we can manage so the detail say method before going to the save we can update two values so detail dot create date equal to date time dot now and also the creative user is b how to pass one more parameter is string user okay we have done so let me change the save detail method also actually we called in our main service so in the header information we will have these informations like a create user so let me pass from here okay this is done so let me run the application once again okay great anyway currently we have this invoice number so let me create one more new invoice so here i am going to include one more product here the product code should be 1.3 and the name is pad so the amount is 250 so let me change the quantity as 2 so then value should be 500 in the total side i'm going to provide 1600 even the net total also same okay let me execute once again okay here also i'm getting the positive response totally so we can check from our database okay currently we have created the two invoices now let me check the edit scenario i'm having the same record i'm going to remove one product okay and also the remarks i'm going to provide a remote button so this is the command and the amounts also varied 500 only so let me execute it so again i'm getting the positive response totally see now currently i'm having only two products for this invoice002 and here also the totals it's updated you know also the invoice number two so we can see the remarks also gets updated and we can see this modify user and also modification date so this is all about the same method so the same way so now let me execute this get all method for this header so expected result should be we have two records so yes we can see the both invoices and now let me execute this get by code method so i'm copying this invoice number okay i supposed to get one record yes i am able to get the complete information now let me move on our third function this method basically return the product information for the particular invoice okay this invoice having the two products okay we are got it one is product one or three and another one is product or not one and if i'm providing one okay here also two records the first one is p101 but the second one is p102 so now let me remove by one of the invoice so i'm getting the positive response only so let me check from the database hopefully both tables the record should be deleted but unfortunately it's not deleted so let me verify it i think i made some mistake here okay just removed the both the tables but i'm not saved anything that's what i'm getting this error so let me complete this from shorty where db connection db transaction otherwise so avoid and database start begin transaction so the big internet section asynchronous function so once all the removes are completed we have to save it so a white save changes the synchronous and also i need to commit it okay we have done it i hope it will work now so let me execute it once again so i'm getting the responses current totally so let me check from databases see now the invoice002 has been deleted in our summary table and also our product table so i'm going to create one more invoice so it should have true i'm going to provide three and this thousand five and i'm going to just a thousand and also i'm going to remove the product from here this should be three okay looks like it should be created yeah yes because this is by zero zero three have one product so this is all about my invoice services so here i am going to create two more services one is for product and another one is for doing some changes in our customer services so using this service i can get all the customers but in the real-time application i supposed to take the individual customer information so i need one get by method so i need one get by core method the similar way i am having the product services so in the product services basically i need three services one is uh returning all the products and another one is written in the single product the final one is returning the product based on the category code the steps are almost the same so let me complete from our customer side because here we just created one method now so it should have one input parameter i think i am not included in our interfaces so and this mapper also the list is not required so because we are returning the single object online okay we have done it hopefully in our get by code method is ready so let me apply the changes in our controller side also okay this is done now let me create one more container for our product one the first one is interface and also i'm going to include the required namespaces and i need to do the declarations and also i need to create the adt classes product entity so the purpley class and i'm going to the model so let me copy all the properties so here just four properties i am going to include the four now in our container section i'm going to change the class name that's it here i am going to include one more function get by category so these all the methods so let me format it and also i need to create the implementation class so that's our actual container class here also we can follow the same steps in our customer container i need to implement this i product container so it gives us the three methods so this dependency injection and methods so everything let me copy it from our customer controller so here we just need to change the names that's it other than that it will work fine the first one is this product entity and also the table name should be table products so this object name that also it's fire only and here table product product entity and the second one is get by code here also i'm going to do the changes let's see okay this is done and the final node needs to be how to create one more method get category by code here i supposed to return the list online get by categories i need to get the name so here i'm going to use the where item dot category kodi culture our category finally i need to convert into two lists and this my upper side also i'm going to include this list so we have done it so the product basically uh we are not going to do this grunt operation so whatever methods is required for creating one invoice so that's what we generated okay still it's saying get by category not implemented so let me check so it's returning list only and get by category that's fine and then why it's still showing an error okay actually it's written in the list online let me change it okay now we are good to go and also i need to include in our singleton i product container it's going to implement this product container that's it ctrl c again dotnet so see now we are able to see our customer so the customer the get record method also gets included but this product is not showing i think i am not created the controller so let me take this code so if i'm providing the customer i am able to get the customer information so if you're providing the wrong customer name so i'm getting the just null value okay and let me create the product controller also yes i've completed this container side but i'm not created the controller so let me copy everything from our customer controller here the first thing is this is the product controller and the interfaces also product container and this constructor also and again the second interface so the dependency part we have done the next one is entity so product entity the same way again it should be a productivity and the final one is get product by category that also i need to implement get by category so integer get by category and it's going to return the list of records let me change the routing name also hope this is done okay application is running so let me refresh once again okay see in the product controller we have these three methods the first one it will return all the products okay the conversion in this auto mapper i am not included okay let me do it actually i just forget it for doing it faster so table product into our product entity okay this is fine so let me run it once again i hope this is our last it will work fine okay fine it's returning the products and the next one is get by code okay fine at the final one is get by category so if i'm providing the category id only means so it will return all the products if i'm providing true so it's not going to return anything so let me directly change it from our databases so instead of one i'm going to provide true so now let me verify one second so now it's returning the one record even if i'm providing the category is now if i'm providing the category radius one so it will return two records so this is all about the services so totally we have the three controllers the three controller the invoice is main one we have all the methods and this customer and the product we are just having this uh what is called our get methods only for doing the authentications for everything already i'm covered in my previous videos you just refer it and there are two things i'm not it covered the first one is handling this log and also more thing how to debug from our visual studio code these two topics as of now i'm not covered i'm planning to cover on my upcoming videos still if you have any doubts or clarifications please post in the comment box and also please don't forget to subscribe my channel so if you really want to support our channel for doing the encourage you how to join our membership thank you thanks for watching our video
Info
Channel: Nihira Techiees
Views: 5,357
Rating: undefined out of 5
Keywords: Dot NET Core Web API using VS Code, create ASP.NET Core web api using visual studio code, Dot Net core using VS Code, Add Entity framework using VS Code, how to install packages in Vs Code, how to run dot core application in VS Code, how to develop dot net core application in VS Code, real time dotnet core web api, sales order service using .NET Core service, invoice service using .NET Core service, how to use rollback transaction using .NET Core services
Id: t_dSvSZ0f1A
Channel Id: undefined
Length: 94min 46sec (5686 seconds)
Published: Tue Jul 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.