- [Instructor] Okay, now that we have our server
restarting automatically.
The last thing we're going to do on our server
before we get into adding database functionality to it,
is we're going to add two more endpoints,
one for adding items to the user's cart
and one for removing items from the user's cart.
Now before we code these out,
keep in mind that these endpoints
just like the get endpoints we created earlier,
are going to be a pretty simplified version for now,
just to get a rough idea
of how these endpoints are going to behave and interact.
So anyway, here's what our temporary endpoints
are going to look like.
Let's open up our server.js file again.
And we're going to scroll up just under our
endpoint for getting a single product data.
(keys clacking)
And the first endpoint we're going to define
is the add to cart endpoint.
So here's what that's going to look like,
we're going to say app.post
since this is going to be a post endpoint.
And the URL that we're going to use here
is going to be /api/users/
and then we're going to use userId as a URL parameter here,
and then /cart, okay.
So that's pretty simple.
Now for the callback,
we're going to have the request and response arguments.
And the next thing we're going to do
is we're going to get the information
that's provided by the request body.
Now the idea here is going to be
that the client is going to send along a request body
that will look something like this,
it'll be a Json object that will simply contain a product
ID property
with the ID of the products
that they want to add to their cart, okay.
So with that said,
the way that we're going to pull that out of the request body
is going to be simply by saying const
productID equals request.body, just like that.
Okay, and now that we have that, we're going to say const
product and we're going to say products.find
and what we want to do is we want to find a product
whose ID property, so product.id is equal to the productId
that we pulled out of the request body.
And then, just like up here with our endpoint
for getting information about an individual product.
What we're going to do
is check to make sure that that product exists,
so we're going to say if product
and if it does what we're going to do is we're going to to say
cartItems.push
and we're going to push that product onto the cart items array.
Okay, just like that
and then we're going to say response.status(200)
and we're going to send back the updated cart items
by saying .json(cartItems).
So that's if the product exists,
if it doesn't we're just going to do what we did before,
we're going to say res.status, we're going to send back 404
and we're going to say .json('could not find product!')
Okay, so that is
the endpoint for adding items to a user's cart.
The next endpoint we're going to add
is the endpoint for removing items from a user's cart.
So here's what that's going to look like.
We're going to say app and then instead of get or post,
we're going to use delete.
It's pretty common to use delete requests
to delete resources on the server as I mentioned before,
so we're going to use app.delete here
and the URL is going to be /api/users
and then the userId/cart
and then
we're going to use productId
as another URL parameter here, okay.
And for the callback request response,
for the callback body what we're going to do
is we're going to get the productId from the URL parameters,
so const { productId } equals req.params.
And then we're going to say cartItems
and the way we're going to remove
the cart item with this given productId,
is we're going to say cartItems equals cartItems.filter
and we're going to filter out all the items that have that ID.
Okay.
So here's what that's going to look like,
we're going to say cartItems.filter product,
product.id
does not equal productId,
that'll basically keep all of the products whose ID
is not equal to productId,
if you're not familiar with how the filter function works.
Okay, so now that we've done that all we need to do is say,
res.status(200).json
and then send back the updated cart items
by saying cartItems,
okay.
All right, so those are our two new endpoints.
Let's make sure our server is running
and with the updates that we made before
it should be updating automatically
whenever we make a change to those files.
So let's head over to Postman and test these,
the first thing we want to do is we want to send a post request
to /api/users/12345
or whatever other user ID,
we're not even using that at this point,
/cart and we want to add a request body to this
make sure it's a Json request body by the way.
And for that is simply going to be productId
and that's going to be equal to one, two, three.
Okay, so we'll click send.
And we should see that that item has been added to our cart
granted it's already in our cart, so it's adding again,
that's something will change when we add the database.
And if you want to add a different productId,
one that's not already in the cart,
you could always do something like five, six, seven
which doesn't look like it's in there yet and click send,
and we'll see that that's been added
to the very end of our user's cart items.
So that's the add to cart endpoint.
Now, let's do the remove from cart endpoint
that's going to look like this,
we're going to say api/users12345/cart
and then /123
and that should remove product 123 from our cart, okay.
As we see there's none of those left in here.
And that's really all we need to do
to implement those very rudimentary versions
of our final endpoints.