-
Last but certainly not least is the ability to delete a difficulty here. So just like all of the others,
-
we want to grab our organization out of the HTTP context. And then just like creating and updating, we have an action for this that we can use,
-
which will inside of a transaction, replace any courses that have this difficulty assigned to it that we're deleting so that we don't end up getting a foreign key constraint
-
here and the course difficulty ID is not nullable. So something needs to be defined in it's stead. So we perform that swap there. Then we go ahead and find the difficulty
-
and delete it out from our database. Doing all of that inside of a transaction so that if any of it fails, all of it fails. So this too takes in a validator.
-
And if we take a look at the validators definition, all that it takes in is an replacement ID. So we are gonna need a body with our delete call. And then for this action, we need to provide in an ID
-
of what's being deleted as well. So in addition to our params and organization, let's also grab the request and we can do const data equals await request,
-
validate using, and this was our difficulty destroy validator. So we'll wanna import that. Now we are getting a red squiggly now on our validate using
-
because this is expecting two arguments, but got one, meaning that it's expecting some metadata to be provided. So if we jump into a peak of this validator yet again,
-
we can see that it has metadata for the organization metadata. We added a helper for this in the previous series, but what this would look like is we provide in options with metadata attached.
-
And I believe we called that our organization ID with which we were provided the organization ID just like so, which makes our validator happy. The helper that we added, I believe,
-
was with organization metadata. And then we just pass the organization ID in just like so, which essentially just builds out that meta object for us
-
and passes it into the option of the validator. That ID is ultimately just going to be used to make sure that the replacement ID of the difficulty
-
that we're after exists inside of the organization itself. Once we have that data, we can go ahead and await,
-
destroy difficulty, call the handle method, pass in our organization. I'm gonna go and break this down on the separate lines again as well as our validated data.
-
And then the ID that we're looking to delete, which we can grab from our route parameters. This returns back void. So it's not going to kick anything back to us. And we also don't need to kick anything back
-
from our difficulties controller here. If we leave this as is, so if we go ahead and just save this, jump back over to Hopscotch to see how it handles the response.
-
Let's go ahead and create ourselves a new request. We can call this destroy difficulty, switch our put to a delete. Let's delete difficulty ID of nine.
-
And then inside of our body, we'll specify a replacement ID of 10. I'm gonna get rid of the color there as well. So let's go ahead and try sending this off. And whoops, I left a comma on there.
-
I'm not sure if that's what caused that there. Let's go and try sending that off one more time. Okay, cool, it was. So now we get back a 200 response with no response data.
-
So whenever you're not sending response data back, 200 is okay, but it would be better to actually send back no content, which is a 204 status.
-
So let's switch back over to Visual Studio Code here and let's reach through to our response. With that response, we can do response.status
-
to set the response status ourselves and set that to a 204 status code. That's going to set it automatically on the response. We don't need to return here.
-
We could just leave this as is, but if you wanna make it evident that that is the exit for this method, you can absolutely return there as well. So let's give that a save, jump back over into Hopscotch.
-
Our ID of nine no longer exists. So we can go ahead and just test to make sure that we should get back an error of not found. And sure enough, we do, so that's good.
-
Let's jump back over to our store difficulties. I can send this off and just create a brand new one here with an ID of 11, jump back over into our delete difficulty,
-
switch this to delete an ID of 11 instead. We leave the replacement ID of 10, that's fine. Send this off and there we go. Now we get back a 204 no content response,
-
which is now perfectly applicable to what we're sending back, which is no content. Perfect, let's go ahead and save that. And we're now done with our difficulty CRUD operations.
-
We have the ability to get all of them, create them, get a specific one, update a specific one and delete a specific one, which is perfect.