- [Tutor] After lots of preparation, we are finally ready
to implement the service that is responsible
for all the database logic for items.
Put it I'm in server services, itemService.
I start with first bring in the model
that we created before.
So add const itemModel equals require
and it's one level up, models, mongoose and there.
It's called item.
Next let's start with implementing a class to class,
should be named class ItemService.
And in the end, just add module.exports
equals itemService, so that we don't forget that.
So itemService will just contain static methods.
This means we will only call methods
on the class itself and never on the instance.
So I start with adding static async,
maybe you remember, we need a getAll method
that gives us all items that we have in the catalog.
So here I now return, and that's really easy now
with the model mongoose gives us ItemModel.find.
And here I put in curly brackets,
this means it's an empty query
which will return us everything.
And then add, .sort.
And in there, I want to sort by, createdAt
and minus one is descending, this means that the last item
that was created will always be on top.
And in the end add exec, this is for mongoose
to really return us a real promise.
Next, I will implement the method that we need to get
one item, for that add static async getOne.
And this takes the itemId as an argument.
And I return, itemModel.
In mongoose, gives us a method, findById
and here just have to pass in the itemId.
And then I call, exec on that again.
Next we will implement the class methods
that I talked about before.
So first we do the create.
So add static async and create,
and this takes an object with data.
So this is the data that will need to be stored
for the item.
And here I add const item
equals new itemModel.
So I'm now creating a new instance of the itemModel
and they're passing just the data.
And I return item.save,
here we don't need the exec in the end.
Next let's do the upgrade.
So at static async update,
and this method takes the itemId.
This is the item that should be updated.
And again, data and here I return.
Again the itemModel with the method,
findByIdAndUpdate.
So this is again, provided to us by mongoose.
It's a helper function, andI just have to add the itemId,
as first argument and then the data.
And in the end we again can call exec here.
What are we missing?
We're missing the delete method.
I called it remove,
so add static async remove
and there add just, passing the itemId of the item
that should be removed.
And the return ItemModel deleteOne.
Of course there's an API documentation for mongoose
where you can look up all those methods that it brings you.
And for this method, I have to provide the query,
so add curly brackets and _id.
This is how the Id is stored in MongoDB ,
should be the itemId.
And in the end, the call again, exec.
So this will delete this one item with this one itemId.
As you see I have the application still running
with nodemon.
So I now head to the browser.
And there I go into manage items
and let's try this out.
So the SKU is 42, the name should be Socks.
And price is 12.99 and then hit on submit.
And you see we created a new item.
Let's try to edit it.
Let's say it's Great Socks,
so I click on edit item.
Am changing the name and then submit again.
And you see that now
the item was also changed in the database.
Let's try one more thing.
So I'm now creating one more item
and I just take the same SKU again
and, say this is now Towel.
It costs 13.99 and let's hit submit
and we see that we are getting an error now.
If you remember when we created the item model,
we defined the SKU field to be unique.
And now when we look into visual studio code
we see that we indeed get an error here now,
to scroll a little bit,
right tells us that there is a duplicate key.
In this error, now comes directly from MongoDB ,
so we are violating a constraint on the database
and MongoDB prevents us from doing that.
If we look at the rest of the console output
you also see a deprecation warning.
I talked about this feature targets before
and I left this one thing, in a little bit on purpose
to show you what happens if one feature target
for a new feature is missing.
So in this case, it tells us that
a toggle useFindAndModify should be set false.
So am doing that now.
I copied that useFindAndModify, go into server
in start.js.
And here in the connection function, after line 13,
I will now add useFindAndModify
and I set this to false.
Let's save that.
Let's head into the shop again,
I click on edit item in there
now let's change this to Two Great Socks, I hit submit.
Then let's look into visual studio code,
if we still get the warning,
and you see that we don't get a warning anymore.
So this deprecation warning is gone as well.
And now of course, let's also look into MongoDB Compass
and let's look at the data that we just created.
So I hit on refresh, and there is now a database shopper
with the collection items.
And when I click on items,
we see the item that we just created.
So we have this auto-generated ID
along with the data that we put into the form
and then the createdAt and updatedAt timestamps.
And this underscore, _v field
which is just an internal field for mongoose.
Now let's head back to the browser
and let's also try to delete the item
and see now the item is deleted and in MongoDB Compass
it should be gone now as well.
So now the collection is empty again.
And with that, our item management is done.
Next, we will implement the user management
which is very similar to the item management
but has one very important difference.