- [Instructor] Any shop needs a shopping cart
or basket for users to actually buy something.
And we also need some page like here in shop,
that lists all the items that are in the catalog.
In preparation for this video,
I already created three items
in my item management
so that we have something to buy.
And let's start now with the shop page
so that we get a list of items.
For that, I head into Visual Studio Code,
and in server, routes, shop, index.js.
We see that again,
a lot of stuff is commented out.
So now, we'll remove return statement
and the comments from this first route
we have here for the index of the page.
And you see that we are missing the item service now
and I can not simply bring it in
but const ItemService
equals require and it's two levels up services/ItemService.
You see I have the application still running with nodemon,
so I can now just head over to the browser,
and let's now click on shop,
and you see that we have now this very basic item overview,
but what we are missing is this add to basket function.
And for the basket,
we will now use Redis which is a very good fit for that.
So for that,
I head back into Visual Studio Code,
and now I head to services
and there is already a file basket service.
In the in-basket service,
we will now implement all the basket logic,
and let's start now by defining a class BasketService.
And again, in the end,
let's export that module that exports equals basket service.
This is something you easily forget in the end usually.
And in BasketService,
we will now implement the constructor.
In this constructor, now get a client,
so this is the Redis client
because the basket is always for a specific user.
So I can now,
in the constructor
create a member variable for the client.
This client equals client.
And then, and that's already specific for Redis.
I will just create the key.
Is member variable, this.key equals,
and this should be the key
that identifies the basket for a given user.
So, I add basket.
So this is basically the namespace, column,
and then I add ${},
so that's again string interpolation
and here a pass in the userId.
So the IDs basket, column
and then the user ID.
And this identifies now
all basket interests for a given user.
Next, what does the basket need?
Of course, a method to add an item.
So add, async add itemId,
and I'm going to easily route here
so you can always only add one item after the other,
so there is no quantity on this add method.
And here, I can now simply return this.client
which points to the Redis client.
And there is a method hincriby.
This method takes first the key,
then it takes some ID.
So some element in this collection basically,
and I want to increment it by one.
This will also create a new entry
if there isn't one already.
Next, I want to create a function
that gets me the whole basket.
So add async getAll.
And there, I return this.client
And there is a method hgetall by Redis,
and there, I can just
provide the key that I want to get,
and it will get me all elements under this key.
And also, each basket needs some way
to remove stuff from it so I add async remove,
and it takes the itemId again
and there, we return this.client.hdel
for delete(this.key),
and there, we want to delete the entry
for this itemId.
As you might guess,
I have to now head back into my shopping index page
and it will now also remove return statement
and the comments for this two basket route
which is used to add an item to the basket.
So here, starting with line 14,
I remove the return statement,
and also this leftover comment here at the end.
I can also remove the next argument here,
and let's see what we are doing here.
So we require users to be locked in
before they can add anything to the basket,
so we check before if there is a current user,
and we will implement the logic
so that we have this current user in a bit.
Now let's just look down here.
You see that we create an instance of BasketService,
and we pass in the Redis client
and the current userId.
For that, you see also
that we are missing the BasketService still.
So I scroll now up,
and for that, I just copy the ItemService line
const BasketService equals require BasketService.
And we also have to add config here
at this module exports,
because I am passing down the conflict to all my routes,
but we also have to bring in this argument of course.
And with that, we don't get an error anymore.
And with that now, everything looks fine.
So we are not completely done yet,
but let's head in the browser real quick.
And, here I go back
on my shopping list,
and then hit, add to basket.
And we see the warning that we have to login first,
and we will implement this logic now in the next video.