All right.
And up next, let's fix the lead job as well as update job controller.
And you know what?
Let me start with delete, since I think it's going to be easier to grasp.
So first, let's navigate back to job controller.
Leave these ones for now.
So I will remove them at the very end of this video.
Effectively.
We're looking for delete job again.
Same deal.
We're not working anymore with the local array, so pretty much remove all of the code that in some
shape or form is working with that local array.
So I'm talking about this one over here and also these two lines of code.
And first, let's create a variable.
And in my case, I'm going to go with remove job.
We want to go with Await again the model and we're looking for find one and delete.
So that's the one we want to use as far as the difference between delete and remove.
The remove is being deprecated right now, so stick with delete.
And you want to go over here and pass in the ID, which again is coming from params and same deal.
If Mongoose cannot find the specific job.
Well, we want to send back the 404.
Now in this case since the variable name is removed job.
I'm going to change it here to remove job.
And you know what?
I will send back job deleted.
But I'll also add the job just so you can see what's happening.
So we're going to go here with Job and we're going to go with removed job.
Let's save it.
We're going to navigate to all jobs again.
Let's make a request.
Because why not?
And I just want to delete the first one over here.
So we're looking for delete job.
You want to pass here the correct ID?
If everything is correct, this is the job we just removed.
So if I go back to all jobs now, I have only three of them.
Again, this is going to depend what are you doing on the front end?
So most likely you'll just send back message.
You'll say, Hey, you were successful.
That's it.
We're good to go.
Move on to the next task.
If for some reason you need the info about the job you just removed, well, you have access to it and
just to showcase.
And I don't think I'm going to do that with the updated job, but this one, I'll still do it.
So I'm going to go to remove job and I just want to showcase what happens if again, the ID doesn't
exist.
On a side note, you can actually use the existing value.
Why?
Well, because we just removed that job, correct.
So of course there is no more job.
And again, same deal.
We get back this null.
So hopefully that is clear.
And now let's move up and we want to handle the update one which usually contains more logic.
And actually, since we'll have the validation layer, we can remove all of these lines of code.
So we will check for empty or wrong values in a different way.
So pretty much everything starting from the company and then the 400 and all that, just remove it.
So we're still looking for the params.
That doesn't change, but of course we're not going to be looking here in the local one and we're also
not going to update in such a way.
So what are we going to do?
Well, we're going to set up a variable.
I'll call this updated job in here.
Again, we go with Await, then Job Dot and find one and update.
That's the method.
And in here, of course, we need to provide more value.
So the first one is going to be ID, which is coming from params.
And yes, I probably have said this quite a few times.
I fully understand that it might be annoying for somebody, but I just want to make it clear.
So it's in the params that's our ID.
And second argument in this case is going to be well what do we want to update?
And again, since we'll have the validation layer, what's really cool, we can basically pass in the
entire thing.
So again, you have multiple options.
If you want to go here manually, set up the object and let's say pass in the property with some kind
of value, you can definitely do so.
But in our case, since we know that Req.body is already an object, I can simply do it over here.
And again, yes, we will be checking for those empty values.
And we'll set it up a little bit later.
And the last thing over here is going to be the options object.
And we want to go with new equals to true.
So by default, it's not going to send you the updated job.
It basically is going to send you the old job before the update.
But we don't want that.
So we want a new one.
That's why we go here with new.
True.
Same deal.
If.
There is no job which matches the ID, we'll get back the null and therefore, of course we can set
up this check as well with updated job over here.
And if we're successful, we'll send back the message job modified.
And when it comes to my job property, I'll just set it equal to updated job.
Let me save this and let's try it out.
So I'm going to go to edit Job.
And you know what?
First, let me get all the jobs.
Let me see what I have.
So I have.
These three jobs.
And you know what?
I'm going to change this one.
The second one, and I'm going to go from frontend to DevOps.
So let me navigate right now to edit job.
First, let me set up.
The over here.
And as far as the company.
Well, since I'm not updating, I mean, we don't really need to use it, so I can just remove it.
And as far as the position, I'm going to go with DevOps and check it out.
This is going to be the new updated job.
And if I take a look at all the jobs, of course the value is going to be DevOps in the second one as
well.
And just to reiterate, of course we can provide other properties and values as well.
So this was just to demonstrate that of course our added functionality in the database works and at
the very end let me navigate to job controller and notice we're not using the local jobs anymore.
I can also remove the nano ID and up next we can talk about the status code.