We were just able to console.log out the product ID that the user was trying to add to the shopping
cart.
So now we need to add in some additional logic to our route handler and make sure that we take that
product ID and actually assign it to a cart stored inside of our carts repository.
So in other words, we want to find the cart associated with this user.
Take a look at that item's array and add a new item or new object inside there that represents the product
that the user just clicked on.
Now, unfortunately, going through this is going to be just a little bit challenging.
Let me tell you why.
There are really a couple of different side cases or corner cases that we need to consider when all
this logic gets executed.
The first thing we need to consider is the case in which a user comes to our application for the very
first time and clicks on Add a product.
If a user comes to our application for the very first time, there's not going to be any cart inside
of our cart suppository that corresponds to that user.
So in that case, we'll need to make sure that we create a cart.
The other scenario is whenever a user has came to our application and added a product before.
In that scenario, when a user makes this request inside the cookie, we're going to have some cart
ID because this user has already been to our application.
They've already created a cart.
We already have something inside of our repository corresponding to this user.
So in this scenario, we're going to have to go through the carts repository and find the appropriate
cart.
So again, two scenarios to consider.
There are an additional two scenarios even after that.
The first scenario after we finally figure out, okay, all after all is said and done, we've got a
curve for this user.
We then have to decide whether or not there's already an item inside this item's array that has the
same ID, because a user might try to add the same item to a cart twice in a row.
And then the counterpoint to that is the case in which there are no items yet, and we need to take
the product ID and add it in fresh to the items array.
So in total, like four different scenarios, they are not mutually exclusive.
So we really need to kind of balance all these different scenarios and really think about what is going
to happen.
So to help us get through this, we're going to write out some comments just to guide ourselves and
really figure out exactly what we need to do.
So we're going to first have some logic to figure out the cart.
So that's going to be okay.
Do we have a cart for this user or not?
Do we need to make one or are we going to retrieve one out of our carts repo?
After that.
Well, then write out some logic to either.
Increment quantity for existing product or add new product to items array.
So the second common right here is the case in which we are trying to add a existing product inside
this item's array for a second time, in which case we would want to update the quantity to two.
Otherwise if the product doesn't exist inside of here yet, so like no items whatsoever, then we're
going to want to go ahead and actually add in the item in the very first place with a quantity of one.
So that's what this if statement right here or this comment is talking about.
Okay, so let's deal with the cart first.
So again, we don't know if there's a cart.
So to decide whether or not there is a cart, we're going to say if.
Not req session cart id.
So remember wrecked session is managed by cookie session.
So that's essentially our cookie right there.
We can kind of imagine.
So we're going to store whether or not the user has a cart on that cart ID property.
So that's going to be the actual ID of the cart stored inside of our carts repository.
So if we enter into this if case right here, that means we don't have a cart.
We need to create one.
And store the cart id on the req session cart id property.
We can then enter the case.
So inside of here, we have a cart.
Let's get it from the repository.
Okay.
So this is the first two cases we're going to consider.
So let's take care of the if not card ID case first.
So in this scenario, we need to create a cart.
To create the cart, we're going to use our carts repository that we just put together a moment.
Go.
So let's require in.
KURTZ repo.
From up one directory carts.
Then inside of our if statement, let's make sure we mark the enclosing function as async because we're
definitely going to use some a async await syntax here because we're making use of our cards repo.
So inside of here, I'll say Kurt is going to be a wait cart's repo.
Create.
And remember, whenever we call creates, we're going to pass in some starting object for the record
that we're trying to create.
That object is going to be assigned an ID property.
So our starting object, like our default value, is going to be just the item's property and it's going
to start off as an empty array.
So essentially we're going to always have an item's property by default, it will be an empty array.
And so we can add in some items to that thing as time goes on.
So inside this creed object right here, I'll say items is going to be an empty array.
Okay.
So then after that, we're going to take the cart that was created.
We're going to take the ID off of that thing and assign it back to our requisition cart ID property.
And that's what's going to associate this cart with whoever just made this request.
So we'll say req session cart ID is cart ID.
So now we can handle the case.
So in the case, this means that we do have a card ID, so we're going to take a look at our repository
and try to retrieve it.
So let's say a cart is a weight.
Hertz repo.
And we're going to use that.
Find one or get one.
I forget the name of it.
Off top of my head.
We can find it inside of repository.
JJ's gets git one.
We're going to pass in the ID of the car that we're trying to retrieve.
So we'll do get one.
And the idea the cart that we're going to try to retrieve is wrecked session card ID.
So rec session card ID.
Now in both cases, for both branches of the IFF statement, we've now assigned Kurt to a local variable
that has been scoped into this if statement.
That means that we can't read the cart later on down here, which is where we actually care about it.
So let's do a quick refactor here right above the first if statement, I'll say let cart.
And then rather than putting a cast in front of both these cart keywords or variable names, I'm just
going to delete the content.
So now we're just going to update the cart variable that we declared up there.
So now, either way, after that big if statement, we should have a cart.
We should be able to do a console log of cart and see some cart appear inside of our console.
So let's save this again, run this and see what happens.
The very first time we run this, we should see a console log of a cart that is coming from this branch
of the if statement.
Because at this point in time, we just wrote this code.
We don't have a cart associated with us yet.
But then the second time that we attempt to access this endpoint, we're going to enter the LRS case
and so we will see a console log of a cart that already exists.
So we're going to save this.
I'm going to go back over to my browser.
I'm going to go back to my products listing and then click on any of the add to cart buttons.
And when I click on it, my browser is going to hang.
That's totally okay.
Let's take a look back at our terminal and see what we got.
So back over here.
I've got a little typo.
KURTZ Repo create is not a function, so I probably just made a really bad typo at some point in time.
Let's see.
I've got Curt's repo.
Up one directory, KURTZ.
Oh.
Okay.
My mistake.
I made a little typo on my require statement right here.
That should be up one directory.
Repositories.
Hurts.
That's better.
Okay, so let's save and refresh tries again.
Add to cart.
There we go.
Product added to CART.
If I go back to my terminal, I'll see a console log of the cart that was created.
And so notice that it has an ID of PHP 1e6.
So this is the cart that is assigned to me in theory, my browser.
So like specifically Chrome, this browser right here has a cart ID property inside that cookie encoded
inside the cookie.
And so whenever this browser window makes a follow up request to the server, the server is then going
to enter into the case and it's going to find the existing cart as opposed to creating a cart.
So if I now do go back to this page and click on Add to Cart again, I can go to my terminal and I'll
see the same exact cart printed out right here.
And I know it's the same because it has the same ID as the first one.
Okay.
So that's pretty much it.
So either way, we're now getting a cart available to us.
So now we just have to implement this other case right here, the case in which we already have added
this product and we need to increment the quantity or it is a new product and we need to add it to the
items array of the cart that we just got during step one.
Okay.
So quick pause right here and continue in just a moment.