All right.
And once we have the model in place, let's navigate to job controller and yes, one by one.
Essentially, we will refactor these controllers and eventually we won't have this local data.
Again, the main point with the local data was to show you the main idea.
We can operate as far as the controllers and requests are concerned.
And then now we want to, of course, connect everything to a database because we want to persist that
value.
We don't want to just start from scratch each and every time.
So this is really up to you.
But for now, I'll just leave these values here with no ID and all that.
And then once we are done refactoring all of the controllers, I'm just going to remove it one by one.
Let's set it up.
We want to start first with importing job.
Basically our job model from the models.
Now in my case, I'm going to call this job.
I'm just going to save a little bit on typing.
And that's why it's important over here to export, since we will use it here in the controllers.
Yep, it's located in the models job model.
But what's missing over here is the JS.
Like I said, as I'm recording, probably I'll forget few times.
Then let's navigate to create job.
Why do we want to work on this one first?
Well, because I want to show you what happens when we create the instance.
Otherwise.
I mean, what jobs are we expecting since we haven't created any.
I'm going to leave company in position because.
Same deal.
So we will send still the same values for now.
So I'm going to go to job recording.
And if you remember when we were creating the job, there was a company as well as the position.
So those are the two things we're sending.
So I'll leave this one.
I will remove this check because eventually there will be a layer, the validation layer, and therefore
I don't think there is any need.
And as far as the status and all that, we can keep it as 201 and Json job.
So eventually there's going to be a job to tell you.
Honestly, as far as the front end, again, we're really not looking for the value that we get back
because when it comes to adding a job, we add the job.
There's going to be a success message.
And essentially, if we want to see all of the jobs, we just navigate to all jobs.
So this is really going to depend on your application.
That's what I'm trying to say.
If you want to send back the exact job, you can definitely do so.
But in our case, technically it is optional.
And up next, let's create our first job.
So essentially we're looking for our model.
In our case, it's a job and it has a method by the name of create.
And in the create method, we pass in the object and we want to provide well which properties we're
going to use in order to create that instance.
Now, in our case, what do we have over here?
We have company and position.
And please keep in mind that when we were creating the job model, we already set some values as default.
Now eventually those values will be provided, but for now.
What's important.
But for now, we can skip the status type and job location because again, there are already some default
values in there.
So in here I'm just going to provide the company value as well as the position.
Now this is a synchronous, so we want to set up a wait in front of it and basically we want to get
back the result and therefore I'm going to go with const job is equal to again await job create and
we provide these two values.
So now let's navigate to create job over here and let's send it.
And if everything is correct, we should be able to see in our dashboard a new job instance and check
it out.
So this is what we're getting back.
Notice So we have company position, then job status, default value pending type, same deal, default
one.
Then we have the ID, so this one gets created by MongoDB.
And also remember I mentioned Createdat and Updatedat.
So those two values are set because we have over here this timestamps.
And if we navigate back to our dashboard and refresh, we should see something pretty, pretty cool.
So notice, like I said, this is going to be our database and since in my case I provided value of
Java file, of course that's the value I see over here.
So this is my database.
And then as far as the collections.
Why it's called jobs.
Well, because over here I went with Mongoose model.
So this creates that collection.
Then we need to come up with a name and essentially it just sets it up in the plural.
So I went with Job and it created jobs and then we just provide schema.
So again, this is going to be structure for our data.
And whenever you see collections, just think table.
So this is where I'll have all of the job values and same deal we can see over here the ID which gets
created.
So we don't need to use the nano ID and then one by one we have all of these properties.
Now one last thing that I want to mention in this video, please keep in mind that when it comes to
MongoDB.
Essentially we can pass here the entire body because I know that it's going to be an object.
And in there I'll have the properties.
So basically I can do something like this.
I can go with rec dot body and we can remove this line of code.
And if you're wondering, well, what is going to happen, if I'll add some extra property that actually
doesn't match my schema?
Well, we'll see that it nicely gets ignored when we're creating the job.
So let's say I'm going to go with some value and I'm going to set it equal to random value.
So once I send again, I'll get back the job.
But notice the property basically is not in my instance.
So essentially we only add the values that are in our schema.
So again, same company, same position, all that, but you don't see anywhere that some value and
random value.
Now, like I keep mentioning, yes, of course, eventually there's going to be a validation layer where
we'll check for exact values and we will discuss this later.
Therefore, we're skipping over it right now.
Just wanted to mention that you can skip the destructuring part if you want to, since there's going
to be a validation layer sitting on top of the controllers anyway.