Okay.
Hopefully we're clear on the query string parameter basics.
And up next, let's kick it up a notch.
You see, our current setup has three major issues.
First of all, search parameter or any other parameter for that matter can be empty.
And if that's the case.
Essentially we'll get back empty array.
So it would be nice if we could add a search parameter based on the condition.
So essentially, if there's some kind of value, Yes.
Use it in order to search for a jobs.
If it's empty, if there's no value or it's not provided at all, well then don't use it then.
Basically get me back all of the jobs.
Now, second issue, we're searching for jobs based on the exact match.
So let's say I'm going to make some kind of typo over here and make the request notice I'll get back
empty array because there are no jobs where position matches exactly to this value.
And in order to fix it, we'll use regex.
So essentially, instead of looking for the exact match, I'm going to say, Hey, listen, if this
value is somewhere there, then return the job.
And lastly, I want to search both the company and the position.
So when I'm searching, not only I want to get the results from the company, but also from the position.
And I guess let's start with the empty value first.
So let me just showcase it just so you don't think that I'm messing with you.
If I remove everything from the search parameter value, check it out.
I get back empty jobs array and in order to fix it, we'll use following approach.
Notice how this is an object.
Correct?
The filter object.
So here's what I want to do.
I want to set it up separately above job dot find.
I'll add the search parameter and some other ones as well.
We'll set them up as properties and if it's present, then we'll add to the object and then we'll use
it in our search.
If not, then of course we will skip it.
So long story short, here's what we want to do.
For starters, let's start destructuring the parameters which are coming from rec dot query.
Now for now we only have search, but there are going to be more.
So I'll set it equal to rec dot query.
And then like I said, I want to construct the object and in my case I'm going to call this query object.
Now which property always is going to be there?
Well that's the created by.
Correct.
So I can just take this one out over here and then instead of hard coding everything, I'll say, you
know what?
There's going to be a query object.
So now I can set up a condition where I'll say, Hey, listen, if search exists, then add it to a
query object.
If it's not basically there is no value, then just skip it.
Now for now, I'll just use the exact match and then in a second we'll set up the rejects.
So I'll say if search exists.
So essentially, if there's some kind of value, then I want to go with query object.
Remember, we need to provide the properties which are on our model.
So this doesn't change.
Notice how this is created by.
So of course, if I want to look for specific property, it needs to match whatever I have in the model.
So in this case, we want to go with position and we want to set it equal to.
Rec dot query and search.
So we only add it if it exists.
If it doesn't, then of course we don't do anything.
So now let's navigate back.
Let's send it here.
Now get all the jobs because we don't add it to our filter object.
However, if.
Of course I'm going to provide some kind of value.
And send it notice.
I'll get that one job.
Okay, so we fixed the first issue.
Up next, let's work on that exact match.
You see, if I'll provide some kind of value, for example, a and if I'll send it, I'll get back.
No jobs because there are no jobs where the position is equal to a.
So again, a better approach is to use regex and say, hey, listen, get me the jobs where in the company
and position property.
The values have a So let's navigate back over here job controller and instead of setting query object
position to req dot query dot search, I want to remove both of them and then the syntax is following.
We're going to go here with dollar sign.
Or again, this is a mongo syntax.
We'll set it equal to an array and there's going to be two objects.
First of all, we'll say position again property, which we have in our model, and then we'll go with
dollar sign again rejects and we'll set it equal to our search.
So that's req dot, query, dot search.
And as a side note, of course in the previous example I could have just used search instead of req
dot query dot search.
Please keep that in mind.
And also I want to provide the options and I'll say I don't care about the casing, so I'll go here
with I let's save it.
And we want to do the same thing for the company as well.
So let me add a comma over here.
Copy and paste and instead of position.
We're going to go here with company and now we're going to navigate back and check it out.
Essentially, now I'm getting all of the jobs where there is a in the position or the company.
And of course, if I'm going to remove and if I'm not going to provide anything in a search, then I'm
going to get back all of the jobs.
Since now we're not adding the search param to our filter object.
And with this in place now we can move on to the next step.