- [Instructor] So that about does it
for our GET endpoints.
The next endpoint that we're going to convert
over to use MongoDB is the endpoint for adding items
to a user's cart.
Remember this app.post endpoint.
So there's going to be one or two new concepts here
but for the most part,
the logic dealing with connecting to MongoDB
is going to be pretty much the same.
So here's what all that's going to look like.
Again, we're going to start off with the async keyword,
adding that to our callback.
And then I'm just going to copy and paste the connection logic
into this endpoint
right underneath where we get the productId.
And then we're going to delete this part here again
where we say const product equals products.find.
Let's delete that.
And instead, we're going to make a MongoDB query
that's going to look something like this.
We're going to say await db.collection users.
And then instead of saying find, we're going to say .updateOne.
Okay, so we're going to update a single user.
And the first argument that we pass here
is going to be an object for filtering the users, right?
So we can say we want to update the user
whose id matches the userId.
Remember, we're getting the userId out
of the URL parameters here.
And actually, while we're at it, let's add that.
Const userId equals request.params.
Okay.
And after we've done that,
we actually need to pass a second argument.
This is how this updateOne function works.
And that's going to be an object as well
that contains the updates that we want
to sort of commit to this user that matches this id.
So what that's going to look like
is we're going to have this $addToSet thing as a key.
And what this means is that whatever comes after it,
we want to add that to the given array
on the user without duplicates.
So here's what's that going to look like.
We're going to say cartItems: productId.
And what this will do is it'll add the productId,
which we're getting up here from the request.body,
to the user's cartItems array property without duplicates.
Okay, so if we try to add a productId that's already
in that array, just nothing will happen.
And now that we've done that,
we're going to actually get the updated user,
which we can do by saying const user
equals await db.collection users .findOne id
equal to userId.
And then we're going to want to get the cartItemIds,
just like we did with the get cartItems endpoint.
Okay, so we're going to say const cartItemIds
equals user.cartItems.
And then what we're going to do
is we're going to convert each of these IDs
into an actual product
that it represents, like we did previously.
So we're going to say const cartItems
equals cartItemIds.map
and just like we did before,
we're going to map each of the IDs to the product
that it represents.
So products.find product, product.id equal to id.
Okay.
And then the last thing we need to do here
is simply send back those cartItems to the user,
so we'll just delete this existing logic that we had.
And after that, we want to call client.close,
just like always.
So that's all there is to it.
Assuming we've typed everything correctly here,
which we may or may not have,
we should be able to test this endpoint using Postman.
So we already have a productId in there
that we want to add to the cart
and that's not one that's currently in the cart
as far as I know.
So let's send a POST request to api/users/12345/cart.
And we're going to click Send.
And that'll send us back an array of all
of the products that are currently
in the user's cart, right?
And we see now
that this includes this 567 cart item as well.