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 firstname.lastname@example.org: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 0:00 Hibernate to delete entities, try implementing the delete feature for GIFs. 0:02 This one is actually simpler than categories because there isn't a check to 0:07 see if any GIFs are in a category. 0:11 In this case, if a user chooses to delete a GIF, we delete it. 0:13 Here's what I'm asking of you. 0:17 First add the proper markup for 0:19 a delete button to the individual GIF page just to the left of the Edit link. 0:21 Remember this will have to be a mini form with a hidden 0:25 input containing the GIF's ID. 0:28 Then add a controller method to handle the post request upon 0:31 clicking the delete button. 0:35 Finally, you need to update the service and 0:36 the DAO layers to provide delete functionality. 0:39 Okay, when I say code, pause and get to work. 0:43 Then unpause and I'll show you how I did it. 0:46 Ready, set, code. 0:48 All right, let me show you what I did. 0:52 Now first, let's look at the GIF's details.html template. 0:54 You may have been pleasantly surprised to see that all the markup was in place for 0:58 deleting the GIF, 1:04 that is the th:action attribute was in place as well as the method. 1:05 Now let's switch to the GifController to check out the code that you did need 1:11 to add in order to implement the deletion. 1:15 In this delete method here, you'll notice that I fetched 1:18 the GIF entity using the gifService's findById method and 1:23 the ID in the request URI, that is this gifId which was captured 1:27 with the controller parameter path variable called gifId. 1:32 Then I use the service's delete method right here, which we already implemented. 1:38 Next I added a flash message saying that we successfully deleted the GIF and 1:43 then redirected back to the app root. 1:49 If yours works now, congratulations, you can move on. 1:52 Otherwise, you can use some of the code I've shown you here to assist you in 1:56 getting to a working solution. 2:00
You need to sign up for Treehouse in order to download course files.Sign up