Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Deleting GIFs2:02 with Chris Ramacciotti
This video presents a code challenge of using your experience with deleting categories to implement the delete operation for GIFs.
Git Command to Sync Your Code to the Start of this Video
git checkout -f s5v9
Using Github With This Course
You can complete this course entirely using code created by you on your local machine. However, if you choose to use the code I've made available to you, you have two options:
- Use the project files linked at the bottom of this page, or
- Use the Github repository I've made available (recommended)
If you choose the recommended option of using the Github repository, it can be found at
To utilize Github with this course, you can download the Github desktop client for your system or use the command line interface provided with Git.
Clone this repository to your machine using the Github desktop client, or using the following command:
git clone email@example.com:treehouse/giflib-hibernate.git
To update your local repository to match a video's starting point, you can use the
git checkout command in combination with the stage and video number. For example, to update your local repository to match the starting point of Stage 5, Video 4, you'd use the following:
git checkout -f s5v4
Notice the use of the -f option. This forces Git to override all local changes, so be aware: this will cause any changes you made to be lost.
Now that you've gained some experience in using Spring and
Hibernate to delete entities, try implementing the delete feature for GIFs.
This one is actually simpler than categories because there isn't a check to
see if any GIFs are in a category.
In this case, if a user chooses to delete a GIF, we delete it.
Here's what I'm asking of you.
First add the proper markup for
a delete button to the individual GIF page just to the left of the Edit link.
Remember this will have to be a mini form with a hidden
input containing the GIF's ID.
Then add a controller method to handle the post request upon
clicking the delete button.
Finally, you need to update the service and
the DAO layers to provide delete functionality.
Okay, when I say code, pause and get to work.
Then unpause and I'll show you how I did it.
Ready, set, code.
All right, let me show you what I did.
Now first, let's look at the GIF's details.html template.
You may have been pleasantly surprised to see that all the markup was in place for
deleting the GIF,
that is the th:action attribute was in place as well as the method.
Now let's switch to the GifController to check out the code that you did need
to add in order to implement the deletion.
In this delete method here, you'll notice that I fetched
the GIF entity using the gifService's findById method and
the ID in the request URI, that is this gifId which was captured
with the controller parameter path variable called gifId.
Then I use the service's delete method right here, which we already implemented.
Next I added a flash message saying that we successfully deleted the GIF and
then redirected back to the app root.
If yours works now, congratulations, you can move on.
Otherwise, you can use some of the code I've shown you here to assist you in
getting to a working solution.
You need to sign up for Treehouse in order to download course files.Sign up