- Everything you see in the browser
of our shopping application is already prepared.
So if I click on manage items
I get to my item form that we saw already
and there I can try creating a new item.
So I put in an sku, item number,
some item name, let's say it socks.
And let's say they cost 12.99.
When you hit submit,
we see that I get the not implemented error.
So something is missing on the backend
that we have to implement it now.
So I'm heading into (mumbles) to decode
and there in my shop application
I open server and this is the route and it's the admin area.
And there it's the item.
And there the index.
And here you see that in this file
a lot is still commented out
because these are the things that will only work
when we implemented our database logic.
So let's remove the commented out stuff.
And also this return statement here
from the first route, starting in line six.
So the return statement can go away.
And on there
I have to remove the leftover comment in the line 21.
You see, again, how useful is linked is because it shows us
that we are missing the next argument here now.
So at this here on top of disruptive definition.
So the first thing we see here in line eight
we are missing the item service.
Of course, this is something we will implement next.
Then we also see that the item ID is optional for disrupt.
But first we seem to use
get all to fetch all items from the catalog.
And this might be because we want to display them
so that we can edit them later as well.
In line 12,
we see that if an item is past
we also fetch this particular item.
This might be when we want to edit a given item
so that we can fill out the form.
And later then we just render the page
and passing items and item.
Next here, starting in line 26
we see a post route.
This is the route we just used
when we tried to save the item.
And here you also see this not implemented
that we saw before on the screen.
So I will remove the return statement
and the comments here as well.
And let's crawl all of it on it's long product definition
and (mumbles) that we can here in line 26.
Now remove the next argument.
On line 28 to 30,
we just do some sanitizing of the data
that was sent a form.
We get this request.body.
And then in line 33,
we also do some sanity check that makes sure
that if sku name or price are missing
the user just will get
a warning displayed on the screen
and that we redirect back to the page.
And this is also the reason why we use request dot session.
So we write this warning into the session.
This is called flash messaging,
and we can real quick look
into our app JS file and then down there
and here in line 41,
you'll see that we write requests.session
messages in res.locals messages
and from their templates have access to it.
And they handle then printing out the warning message.
I head back into index and let's see what I'm doing.
If everything went fine in line 43
I'm making the distinction
if this is an existing item or a new item to be created.
So if the body does not contain an item ID
I will use item service dot create
and pass in the sku, the name, and the price.
Otherwise,
so if the body contains an item ID
we note this is an existing item
and we will use item service.update.
And there we pass in the item ID first
and then the item data
so that the update method
knows the item ID that needs to be updated with item data.
And then in the end, we use the same messaging system
as before to print out a success message to the user.
We also have some error handling here.
So you see
that we have have a try-catch block around all that.
And if there was an error, we will also let the user know
and we will also print out the error
to the console for us to know what went wrong.
Okay, what is missing here, here in line 71
we also have a route to delete items.
So let's remove the comments
and return statement here as well.
And let's we move this next argument here and there.
We simply call item service.remove
and we pass in the item ID that we want to be removed.
Again, the rest is our handling
or printing out the success message.
This routing file contains
pretty much all the basic operations
that database management system should have.
You need some way to display the data
need some way to create new data, some way to edit the data.
And also some way to delete the data.
In channel,
people speak about create, update and delete, in short CRUD.
So as I said before, we are missing the item service
let's crawl up and just bring it in now.
So add const item service equals require
and this file exists already
in three levels up services, item service.
And if we look into this file,
we see that the file is empty
because that's exactly what we will implement
in the next video.