All right.
And up next, let's discuss how we can get all jobs from the database, as well as the getting a single
job from database.
And effectively we want to navigate to job controller first.
Let's fix that, create job.
So it's not going to be something actually we'll pass here.
Wreck that body and then when it comes to getting all of the jobs.
Of course, we're not going to use our local array anymore.
So we want to go above our response.
And I'm going to keep the same name since I don't want to change anything here in the response.
So I'm going to go with jobs.
And essentially we're looking for Await.
Again, this is going to be asynchronous.
Then job and notice we get a bunch of methods and the one that we're looking for is the find one.
So this gets all of the instances and here we pass in the filter object and if it's empty, then we
get all the instances.
But if we pass in some values, then of course we will find the instance based on that parameter.
Now, please keep in mind that of course whatever you pass in needs to match what we have in the schema
and we'll cover the basic option in just one second.
So first, let me save it.
Then let me go back to create job.
In between the videos, I created one job for the apple.
So you know what?
Let's do the same thing on Let's create Google.
And in this case, it's going to be a back end.
Let me send.
Yep, we successfully created this job and now if I go to get all jobs, if everything is correct,
I'm going to get back four jobs over here now again.
Little bit later, of course, we'll talk about more complex options.
But for now, if you want to get job based on specific company, for example, Apple or Google, just
type here company.
Just make sure that the property is actually in the schema.
And of course, in my case, I'm going to go here with Apple.
And if everything is correct, check it out.
Now, we have only one instance because there's only one job that matches my search criteria.
Now, this might be a little bit redundant, but I definitely want to showcase that.
Of course, now we're persisting everything in the database.
So what am I talking about over here if I stop the server?
And restart it.
You'll see that we still get back all of the jobs.
So first, let me navigate over here to job controller.
Let me remove.
Let me just send the empty one.
And you'll see that unlike the previous setup with our local data.
Now, of course, since this is persisted in database.
We're always going to have access to it.
Hopefully that is clear.
And now let's go to job controller and let's work on getting a single job.
And totally honestly, most of this functionality is going to stay the same.
So first, let me just remove stuff we're not going to use.
So this was just for demonstration purposes.
So we'll return to the errors in a second.
And also, of course, we're not going to be looking for that single job in our local array, but we'll
still use the ID so this stays the same.
Every time.
We'll want to get the single job.
Yes, we'll have to pass in the ID.
It doesn't matter that the first one was nano ID and the second one is going to be the Mongo one.
No.
That part is irrelevant.
We still want to grab that ID from Req.params.
The difference right now is that we'll create the instance and yes, I'll keep the same name because
why?
We need to retype stuff If we already have that name and we want to go wait again, it's asynchronous.
Again, we go with our instance and the method name is find by ID and you can probably already imagine
that in here we'll just pass in the ID extremely straightforward.
So we take the ID from the parameters we use find by ID.
Now, if we don't get back anything, we'll still get the null and therefore we'll still have this check.
So this doesn't change.
And if everything is correct, then we send back the job.
So let's save it over here.
Let's go to get single job.
And I guess let's start by grabbing all of them.
Now you can pick any job, but I'm just going to be sucker for Apple, I guess, during these videos.
And let's go to getting a single job.
Pass over here.
And like I said, if everything is correct, we'll nicely get back the single job.
Now, if the job doesn't exist, you know, let's log it.
Just so you don't think that I'm messing with you, I'm going to go with log and job.
Let's save it here and then change the last character.
So don't remove any characters.
Don't add any characters and we'll handle that.
Gotcha a little bit later.
So don't worry about it right now.
Just change the last character and if we send notice, we nicely get back this 404.
And like I said, the value is going to be null.
So if the job based on that ID does not exist in the database.
Yep.
We'll get back the error.
So let me go back to the previous one and we should see the job.
And with this in place, we are done with getting all the jobs as well as getting a single job.
And up next, we can work on update job and delete job.