In this video.
Let's get started with MVC design pattern.
Because of that, we are going to build a complete API for employees database.
In that way, you better understand how the MVC component come into play.
So let's get started.
Well, I'll provide the starter file for you and it contains a simple express server.
And inside the utils I have connected my application to MongoDB and I advise you to use your own connection
string.
Next thing is that we are going to use the MongoDB extension.
If you don't have it, go ahead and then install the extension and that is MongoDB.
After installation, click on the icon and add new connection and then click on Connect and here add
your connection string for this one.
We have done it many times, so let's move on.
I assume that you have your connection string ready.
Otherwise, there's a video to show you how you can get your own connection string.
So let me click on that to connect.
And indeed it has been connected.
All right.
So let's get started with the MVC design pattern.
First, we need to have the component that makes up the MVC design pattern.
And the first one is going to be the model.
And after that, we need the controller.
And lastly, they're going to be the view meaning how we are going to present data to the user.
For this particular application, we are not going to render the data to the user.
Because for this one, we have done it before.
That is how to render template engine in Node.js.
So let's see how we are going to develop this application using the MVC design pattern.
So we need to have the data inside the model.
So let's go ahead and then create our model and our code, this one as employee.
Don't.
Jess.
So let's go ahead and then have our schema for our employees.
If you use my setup code, you have Mongoose being installed already, but in case you are using your
custom server, you need to install Mongoose Express and Node one.
So first thing is that we need to bring in the mongoose and here we go require mongoose.
Perfect.
And here is our simple schema for our employees.
Cost employees.
Schema.
Is equal to new mongoose dart schema.
And for this we need the name of the employee.
And the type is going to be.
Our string and then is required, we will say is true.
Great.
And I've tried a name.
Let's copy this one.
And we want the city about the employee and also a string.
And we provide is married.
And you're going to be a Boolean value.
And by default, let's give it force.
And let's also add the salary about the employee.
And we're going to be of type number.
Let's maintain these properties because we are focusing on how we can make use of NVQ design pattern.
So lastly, let's pass in some additional configuration like timestamp and going to be true.
So lastly, let's compile our schema to form a model.
Great.
So the next step is that let's go ahead and then handle all the logic on the server file and then we
move everything to use the MVC design pattern.
So here let's add our middleware.
And the first one that we need is what we call express JSON and going to be as Express DOT.
Jason.
And that's what this one going to help us to pass the incoming data and for this one to we have done
it many times.
Yeah.
So next step is that let's create our route to create employee.
So here we go for this one.
We're going to maintain only two route here.
One is to create and one is to fetch all and that is it.
Because I just play around how we are going to use what is called the MVC design pattern.
So here are going to be.
The next one going to be the fetching of employees.
So let's add this one and here are going to be get requests and here are going to be get requests and
here are going to be as the same as employees.
And then the response going to be, gets employees, gets employees.
Great.
So let's collapse this one also.
So let's handle all the code operation, right?
So let's paste that one and now let's change this one to put requests and then going to be forward slash
some ID.
And here we're going to be put request and forward slash some ID and going to be as update great and
let's collapse eighths and next end points is going to be and here is going to be delete.
And here are going to be four like some ID and forward slash some ID and here you're going to be as
deletes employee.
Right.
And lastly, let's handle for a single employee, you're going to be get requests and then force leave
some ID and then here I would say fetch single.
Employee and now we are set.
So now let's test this endpoint by using this tool called ten dot line instead of postman.
So if you don't have it, go ahead and then install this extension called ten lines.
Right?
So let's see.
And this is extension.
So after that, you click on this icon.
And here let's add our first request.
Make sure that you add the part which is 3000.
So in here, let's go ahead and then add our requests.
Great.
So this I'm going to be as post requests.
All right.
So let's hit st and indeed we have the post requests.
So let's duplicate this request or we can rename this one and I'll call this one as.
Great employee.
So the next step is let's duplicate this one and let's rename this one to our employees.
Great.
And next one, I'm going to be.
So let's try to fetch when I get requests and let's fetch all and I have the route.
Then this one is going to be updated.
So here is going to be as update employee.
Great.
So here is going to be put request and going to be forced life.
Some ID it's st and we have it.
So let me save this one.
And next one I'm going to be the.
Delete.
So here is going to be delete employee and here is going to be delete requests and here we pass in some
ID it's sent.
Well, this route doesn't exist.
So let's check our server file if you have a route for deleting.
Oh, sorry.
Supposed to be delete here.
I believe you guys saw it back on my site.
So let's hit send, and now we have it.
Great.
So the next one is going to be as let me check all the endpoint delete update or create the last one
going to be as first single.
So here, let's rename this one as single employee.
Great.
Then here are going to be Gates, and he are going to be forced to have some ID and hit send.
Well, also, this one doesn't exist.
So where do I have first single?
Yeah.
Sorry.
It's supposed to be.
Gets better.
My site.
So let me see this one.
Let's get signed and everything works fine.
Onto this point.
This is a normal way we have been using.
Right.
In the next video, let's continue to implement our application using the MVC design param.
All right.
In this video, let's continue our implementation to use MVC design pattern.