Hi. Welcome to CodingDroplets. In this video
we are going to create a simple asp.net web api microservice which uses mongodb database
to save the data. We are currently developing a demo microservice solution in which there are
multiple microservices which uses different databases. So first let me give you an overview
of the solution that we are developing. W`e have already created the customer service which uses
sql server database. This service is only having the customer module in it. We have also created
a product service which uses mysql database. Now in this video we are developing the third
service named order service, which uses mongodb database. So this microservice will only be having
the methods to manage the orders. In the next video we'll create an api gateway which will route the
api requests to these three services. Then finally we'll create a web application which will
only communicate with the api gateway from that application we'll be able to create
customers, products and orders. We'll be able to do the CRUD operations in all the modules from the
web application. CRUD means create, read, update and delete. In this video we'll also add container
orchestrator support for the new service which we are going to create and configure the
docker compose yaml file. So before starting you should know one thing. This video is part
of a series named micro service architecture. You can find the playlist link in the
video description to watch the other videos in this series. So now let's start developing
the dotnet core web api with mongodb database. I have opened the demo microservice solution which
we have created in our previous videos, In the solution explorer you can see the solution folder
named micro services and inside that we have the customer web api and product web api. Also we have
the docker compose project which will get created while adding the docker orchestrator support.
Now inside microservice folder I am creating a new project. Let me select asp.net core web api
project template and clicking on next button. Let the project name be order web api. Then
clicking the next button again. As done in the previous video, I am choosing dot net 6 framework.
Then unchecking the configure for https checkbox. Also unchecking the open api support. Y`ou
can enable this if you need swagger. Now the project got created. Let me remove weather
forecast controller and weather forecast cs files. Those are the demo files created by visual
studio. We don't need them. For the mongodb database read and write operations, we need to install a
library named 'MongoDB.Driver'. So let me install that from nuget package manager. N`avigating
to the browse tab and searching for mongodb. The library is listed here. Let me install it. It is installed now. We can see the
library now in the installed tab. Next we can create the moodle classes for our
entities. So let me create a folder named models inside the project. Now let me add a new class
inside the models folder and naming it as order. The first property we need in the order entity is
the order id, which is a unique id of the entity. The data type is string. Next property is customer
id which is integer data type. In this property we'll map the unique id of the customer in sql
server database. Then we need the date and time of the order creation. The property name is 'OrderedOn'.
Next we need to include the products in the order. Also we must save the quantity and the unit price
of the product. In a normal relational database like sql server or mysql, we used to create a
different table for saving that information. But as mongodb is a non-relational database,
we don't need a separate collection for that. If you are new to mongodb and you need to learn
csharp mongodb implementation, we have a series named mongodb with csharp beginners tutorial.
That is an old series. Hence the sound quality and the presentation style are not good enough. But
we have covered most of the things what you need to start developing with mongodb. In mongodb we
can include array of objects in the same document. For that let me create one more model class
and name it as order detail. In order detail class, first we need the product id which is the
unique id of the product in mysql database. Next quantity, which is of decimal data type. Then
the unit pricem which is also a decimal data type. Now in the order model class we can create a list
of order detail objects. Naming it as order details. Next we can add some attributes. First I am adding
serializable and BsonIgnoreExtraElements attributes for the class. Then BsonId, BsonElement
and BsonRepresentation attributes for the OrderId property. BsonId denotes that
the property is the unique id. BsonElement attribute is used to mention a
custom field name in the database. BsonRepresentation is the data type of the
field in the database. So OrderId will be an object id in the database. But in the
application we'll get it as a string. Next the attributes for the customer id. The
field name will be customer_id and the data type will be int32 in the database. Then
attributes for OrderedOn property. Finally the attributes for order details. In the same way I am
providing the attributes in order detailed class. Next we can create an api controller inside the
controllers folder. Choosing api from the left side menu and selecting empty api controller
template. Let's name it as order controller. First let me create a constructor for this class.
Inside the constructor I am hardcoding dbHost and dbName. We'll be changing this
while configuring docker compose. Then creating a connection string using
the dbHost and dbName variables in it. Next creating a mongo url object by
passing the connection string as a parameter. After that declaring a mongo client object. We
can pass the mongo url object as a parameter here. Then calling 'mongoclient.getdatabase' method to
fetch the mongoo database object. Now I am creating a private read-only mongo collection object.
The object name is order collection. From the mongo database object, we can call getcollection
method to receive the mongo collection object. As a parameter we need to pass the collection
name. Now we can create the api methods. First let me create a http get method with the
name get orders which returns all the orders. Inside the method we can just call the find
method in mogo collection object. It will return all the orders in the database collection.
Here we don't need to apply any filters. That is the reason I have given 'Builders.Filter.Empty'. Next
method is again a http get method which accepts the order id along with the url. The method name is
GetById. This method is for returning the order which only matches the provided id. In the
method I am creating a filter definition object for filtering with order id. Then we can
just call the same find method by providing the filter definition as a parameter. Next is a
http post method for creating new orders. This method accepts the order object as a parameter.
We'll pass the order details as the request body, in json format. We can just call the insert one
async to save the order in database collection. Then just returning an ok response. Next is a http
put method to update an existing order. Here also I am creating a filter definition. We need to
update the document that matches the order id. Next I'm calling the replace one async method. This
method will replace the document with the new one. After that just returning an okay response. The
final method is http delete for deleting orders. This method also accepts order id along with
the url. So in this method also we need the same filter definition. Then calling the delete
one async method to remove the order from the database collection. After that returning an okay
response. So that's it needed in the api controller. Bow we have methods to
create, read, update and delete. Now we can test the api methods using post man.
So let me set the new project as start a project and run it now. The application is running and
you can see that it is listening on port 5117. Let me copy the base url. Just before testing with
postman. I'm opening studio 3t which is a graphical user interface tool for mongodb. Currently
there is no database for our older service. So let's open postman and test the api methods. On
the left side pane named my workspace, it is already showing the collection named demo microservice
solution that we have created in the previous videos. Under that collection there are two folders,
customer web api and product web api. Let me create a new folder under demo microservice solution
and naming it as order web api. Now let's add a new request and name it as create. Then changing
the method to http post. The post method is used to create new orders. I am pasting the base url here
and providing the complete url of the controller. In our controller, the route attribute is for
mentioning the controller url. It is api slash controller name. For the post request we must pass
the order details as a request body in json format. Let the order id be empty text. Order id will get
created automatically while adding a new document in the database. Then customer id be 10. After that
order on value. This is a Date-Time property. Next is the order details which is a list of
order detail objects. We can provide the values in an array. So the first order detail
with product id 2. Then let the quantity be 10. Unit price be 100. Now let me copy this and
create one more order detail. Just changing the values of product id, quantity and unit
price. I am clicking on the send button and you can see that we have received a 200 okay
response. That means api execution is successful. Let's open studio 3t again and check now. I'm
just refreshing the list and you can see that the database got created. Now inside the
database we have a new collection named order. Let me right click on the collection and
choose open collection tab here. We can see the new order which we have created. We can change
the view here. I'm changing it to json view. Now it is showing in json format. Let me
create one more order by changing some values I need only one product in this order. Again we
got a successful response. Let's have a look in studio 3t. The new order is also listed here. There
is another view named treeview. In treeview we can see the details like this. We can just expand the
documents or array fields which we need to see. So post request is working fine. Let me save it.
Now I'm adding another request and naming it as get orders. Let the method be http get. Then
providing the complete url of the api controller. Now this should execute the 'GetOrders' method
and list all the orders in the database. Let's try it. We can see both the orders which we have
added, in json format. So we can save this request as well. Next creating another request 'GetById'
for fetching the order details by filtering with order id. The method is again a http get. But we
should also provide the order id along with the url. We can copy the order id from studio3t and
provide it along with the url. Now you can see it has only displayed the order which is having
the provided order id. Let me change the order id. Coping the order id of the other order. It is
working fine. So we can save this request as well. Now another request to update the order. So the
method should be http put. Then providing the url for this method. Also we must provide the order
details in the request body. Let me copy the request body from the post request. But here we
also need to provide the order id. It will update the order which matches the provided order id.
Let's make some changes in the order details. Let this order have three products in it. Now clicking on the send button and we have
received the successful response. So let's have a look in studio 3t. The product details got
updated. I am saving this request. Now the final request for deleting the orders. The method should
be http delete. For this request also we must provide the order id along with the url. The order
id of the order to be deleted. I'm just copying the order id from studio3t. Now let's see whether
it is deleted or not. It got deleted successfully. So all the methods are working fine. Let
me save this delete request as well. Next we are going to add container orchestrator
support for this project. Just before adding that, let's open current docker compose yaml file. We have
done this configuration for customer microservice and product microservice in our previous videos.
Let's add empty lines between each services. So that it will be easier for us to understand.
I'm also making some changes in port numbers. Some port numbers mentioned in this yaml file is
already getting used by some other applications in my machine. We can just build the docker compose
project to ensure that there are no errors. Build succeeded. So now let's add containers
for the order service and mongodb. First I am creating a new service in docker
compose yaml file for mongodb. Container name is order-db. Image is mongo. Let me assign
the port mapping. Port 18005 for 27017 which is the default port number for mongodb. This container
also should be under the same backend network as we did before. Next I am adding the container
orchestrator support for the order web api project. To add container orchestrator support, we can right
click on the project. Then go to add option. There we have container orchestrator support. Selecting
docker compose as the container orchestrator and linux as the container operating system. Now
a new docker file got created in our project. Here we can see the container will expose port
number 80. Visual studio added the order api container details in our existing docker
compose yaml file. You can see a new service named order web api. First let me provide a
container name for the order api container. Next assigning the port mapping and providing
18006 for port 80. The application will be listening on port 80 in the container. Then
adding the backend network in the network section. This container will be also under the same network.
Now we can provide some environment variables for constructing the connection string as we did for
sql server and mysql. First environment variable is db_host. Then the second one is
db_name. Let it be dms_order. Now let's modify the connection string to use the
values from the environment variables. So using the 'Environment.GetEnvironmentVariable' method
to fetch the value of the environment variable. Now we can provide the environment variable
names. DB_HOST for dbHost. Then DB_NAME for dbName. So that's
it. Let's make the docker compose project as the startup project. I've done one mistake. In the
DB_HOST environment variable, we need to provide the orderdb container. Now let's run and
test the docker compose project. So visual studio is preparing the containers to debug. Now you can
see there are six containers running. customer api, customer db, order api, order db, product api and
product db. You can see the containers in docker desktop as well. Now in studio 3t, let
me connect to the container mongodb service. Clicking on a new connection. We can manually
configure the connection. Port number is 18005. Let's test the connection and
it is showing that the connection is successful. So let me open it. Now let's create an order using
postman. We need to change the port number to 18006. Got 200 okay response. Now in studio3t, we can
see the database. The order successfully saved in the order db container database. Let's also
test the get orders api method by just changing the port number. That is also working fine. So
now we have created our third microservice. Next we'll create an api gateway to communicate
with all the three micro services and finally a web application that performs the CRUD operations
in the upcoming videos. So that's it for this video. Hope you liked it. Please subscribe, like and share
this video. See you all in the next video. Thank You!