Hey there, everyone.
Thanks here.
And yes, quite a lot of in-depth talk.
But this talk is really necessary that you understand everything in detail.
And that's what the exact goal of this course to make you pro.
Now let's go ahead and finally export this.
We actually forgot to export this class.
So all we got to do is module dot export.
So let's go ahead and do that.
We're going to go ahead and say module dot exports and we're going to export this where clause where
we go.
That's all what we got to do.
And now we can import this class anywhere we like.
We're going to go into the product controller itself.
We have worked on adding the product.
Now we need to work on how we are going to get all the products.
We haven't adjusted the route on this one.
We will do that in the next video.
But right now let's go ahead and export another method.
So this one is going to simply get all products.
Now technically the name get all product is not to be honest, really correct.
Because you're not getting all the product all the time.
But again, if you don't pass me any query or any pagination or something, I'll give you all the products
here.
But if you do pass me that my helper method is all ready to actually go ahead and import all of that.
So let's go ahead and wrap this up.
So we're going to go ahead and say exactly same.
Give me a big promise.
I'll be using a sync and inside that we'll be having request response and the next, just like always.
And there we go.
Now, in case it would be a normal thing, then I would be saying something like this Hey, we have
all the products, products, products.
And then we this is going to be actually an array.
And all we've got to do is simply use the model.
So we're going to say, hey, product, let's use that and we are going to go ahead and say find.
And inside the find, we don't pass on anything further down the road.
We just say this response dot status that you have received something to 100 and a dot JSON and we can
go ahead and say simply success is going to be true and we go ahead and just throw it on all the products.
That's it.
That is usually the case, but not in this one.
We have to actually go a little bit smarter because the product can be a really long list.
So we need to define a few variables and work on with that.
But again, this is also fine in case you want to just define a separate method for getting all the
products, and that's pretty much it.
Now let's go ahead and define this one and let me walk you through with a couple of more stuff that
you can do.
The first thing is going to be simply result per page so that how many results you want per page, maybe
you want just the two.
Maybe you want to show two products for product, six product or maybe five products.
So it's up to you.
I'll go with the four because it makes sense to have one, two, three, four.
So I'll have a big columns.
Maybe I'm assuming in the front end, two products in one row and two products another row.
Maybe you want to show six, product six makes sense.
So let's go ahead and throw up six products in this one, three in the top row, three in the second
row.
I'm kind of assuming that.
Now, in case you want to find out that how many total products you have got, you don't need to hold
this one and need to calculate like products at length.
Surely that is also a way, but just optionally telling you that there is also a way in this you can
just count the product.
So we're going to say count product or product count, whatever you say, you can go ahead and request
that, hey, I want to again, this is a database stock, so we need to go ahead and await.
In fact, we need to go ahead and talk and wait here.
But we'll change that in a minute so we can go ahead and say, hey, I want to use a product.
And this product has a by default built in method which says count documents.
As soon as you run this, this will give you.
But we are not going to need it just in case you want to have it.
Again, I'm going to comment this out just to make sure that you know about it.
Okay.
What we're going to do here is we are going to import a class where class.
So let's go ahead and say that I want to bring in.
So I'll be saying simply where class.
Let's import that.
There we go.
Nice and easy.
It comes in.
Now, the idea behind having this very clause is to accept that there might be more things that might
be coming into the picture.
So rather, what I'll be doing is I'll be creating a new object from this very class.
So let's cut this out and in fact, remove this one here.
Let's go ahead and say that whatever the products are going to come in, they are going to come in as
this class result.
So this one is very close.
It takes two parameters.
The first one is the base itself.
The base in this case is going to be product dot find.
So it just finds everything.
And further down the road we can just pass on whatever the query user is giving is.
So request dot simply query.
There we go.
Now in case there is no query at all and the request then.
Okay, we just find a pass on the product, find it will give me all the results back and I'll store
that and I'll just throw them out.
Okay, that is nice, but it's not going to do it automatically because in case you remember, if we
are working on to this where clause by default it doesn't do anything.
This is just a constructor, so it's going to throw it back as it is in case the big query doesn't exist.
Because all these methods the search, the filter and the page, nature or the pager, whatever you
want to call this one page nature is, by the way, a term for these Python based Django framework.
So that's why I call them as page eight or someti.
So all these method actually works on this query itself.
So let's go ahead and work on with that.
So what we're going to say that, hey, once I have passed you on to this one, then I would run the
method onto this one.
So this method will give me that.
Hey, just go ahead and give me a search result.
But there might be a case that you might also have given me some of the filter data as well.
So let's just go ahead and say, hey, you might be running it through the filter.
Let's go ahead and save this one.
And there we go.
So now this class will give me back a result.
All the products will be coming up, so I'll be just sending the products just like that.
Okay, so that is nice.
But the thing is now there might be somebody who who is at the front end might be looking for that,
hey, how many total products you are giving me because I have to set the pagination on the front end
UI as well.
I can give you the list of all the products by using the first method here, but this is the all the
count.
I don't want all the count.
I want the count after the filtration process has been done and there is no shortcut for that one.
We have to give the result of this one.
So we're going to go ahead and say this one as let's call this one as filtered product number.
Maybe just to clarify that what we are actually giving him.
So we're going to go ahead and say products dot length.
There we go.
Now, obviously, this is designed just to we throw it out here.
So we're going to go ahead and say, hey, all these products here are and this is the filter number
of product.
Maybe you want to have this count product or total count as well.
Total product count.
This actually makes sense to have a total product down.
Maybe you want to throw that out what it's doing up here.
Let's go ahead and throw that out also.
So there we go.
We are going to put up a comma and throw that out also on the front end.
Okay, this is nice, but you might be wondering that, hey, why you haven't actually applied this
filter?
You have applied the filter, but why haven't you applied this pagination?
We have worked so much hard on that one.
Let's go ahead and also apply that.
No big deal.
So once this is all done, we have gone through with all these things.
Now let's go ahead and see that how the filter product works on.
So we need to modify a few things based on that.
So further down the road, I'm going to go ahead and say that, hey, let's go ahead and create this
one, the result of this one, which is products.
And now whatever this product is, I can actually run a simple pager on that.
So did I call it as pager?
Yep.
This is pager.
Since this is an object of this class, I can go ahead and actually run this pager directly on this
one because ultimately, end of the day, this is a kind of array obviously, but being created from
this class.
So I can run this one, but this actually expects a parameter.
This is the way how we have designed it result per page.
So that's why exactly the result per page was designed.
So go ahead and copy and paste on this one.
So results per page actually goes in there.
There we go.
Now, what you might have received back from this one.
So right now this has given us us everything.
So we need to store that into some of the value as well.
Let's go ahead and do that.
So this has gone through with the values.
Let's go ahead and store that again into the product so we can actually do it here as well.
But anyways, so we're going to go ahead and say, hey, products, now you need to update it if you
have something into the page itself and we're going to go ahead and say a weight and products.
And we will be extracting the base from it.
Now the reason why we are extracting base from this one and not anything else, because if you go up
into the where clause, remember we are adjusting the base.
The only thing that it is doing is adding a dot limit and dot skip and the proper values on this.
Now in case in case this is looking up too much for you that hey, these lines are actually confusing
to me.
All you can do is you can actually come up here and can simply say, Hey, products.
There we go.
I want to add a limit on to you and I want to add a skip onto you so you can go ahead and provide these
methods.
And again, you have to do the calculation of how many values to escape, how many values limit that
means how many results per page I have to give so you can go ahead and add this one.
This is exactly what is happening in this one method.
So behind the scene in the page at itself, it is just adding a dot limit and skip and whatever the
result is, we are actually awaiting that.
So it is going to go ahead and calculate and run these queries obviously because this product is actually
hold now all the values and the base especially.
So it's going to just go ahead and run the result and store that into this one.
So again, in case this is too much, go ahead and add this.
But I'm pretty sure after this explanation it should be all easier for you.
And end of the day, we are not doing anything else.
We are just throwing up the product's filtered product number and the total count product.
So that sounds okay.
And there we go.
I know this is a little bit of a complex work, but again, this is a pro work and you'll be doing this
one repetitively in many of the model itself.
So let's go ahead and catch up in the next video.