Coding the GIF Service6:46 with Chris Ramacciotti
In this video we begin adding the functionality that will allow our application to save a Gif entity, including the data from the uploaded file.
Git Command to Sync Your Code to the Start of this Video
git checkout -f s5v2
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.
Our next order of business is to go the service for GIFs. 0:00 Let's start though on the controller, in particular, let's head to the controller 0:03 method that processes the new GIF submission and that is the add GIF method. 0:07 One quick note about a nice feature in IntelliJ and 0:11 other popular IDEs have this as well. 0:14 I have this Structure view, as opposed to the Project view in IntelliJ and 0:16 even if the files not open, with it highlighted, I can click structure. 0:21 And see a quick overview of that class and I mentioned the addGif method. 0:25 So if i double click on that, 0:31 that file opens with the cursor placed directly in the method I just clicked on. 0:33 Pretty cool. 0:38 Now, in order to capture the file data upon upload will need to add 0:40 a MultipartFile parameter to this method. 0:44 MultipartFile right there, I'll name it file. 0:47 And to tell Spring that this is a value that will be passed via the HTTP request 0:54 as a parameter in the payload we'll annotate it with the request param 0:59 annotation. 1:03 @RequestParam just like that and 1:05 this annotation definitely offers a name attribute. 1:09 You can specify a value for the name in there, 1:15 if the name coming from the HTML form differs from that of the parameter name. 1:18 But if I leave this off, this parameter name will be used by default. 1:24 So I'll leave it there because I know in my HTML form. 1:30 I used file as the name attribute on that input element. 1:33 So cool, we now have access to the file data. 1:39 The work we have left to do is to code a GIF service and 1:42 GIF TAO to handle the savings that we can call upon the service from the controller 1:46 to save the uploaded GIF. 1:50 Then, we'll return to this controller to wrap up its code for uploading. 1:52 Let's start by creating the GIF service. 1:57 So I'll go back to my project view. 1:59 And in the service package, I'll create a gif service 2:01 as an interface, GifService. 2:06 Let's add methods in here that we can use to do the same sorts of things that we did 2:13 in the category service. 2:17 So we'll add findAll, findByID, save and delete methods. 2:18 Here we go. 2:23 We'll make the findAll return a list of GIF objects, 2:24 Make sure you import all that stuff. 2:31 Let's import this with option enter, 2:37 there we go, also import the list interface java.util. 2:41 It's not always the first choice as you can see. 2:45 Cool, next, I will return a single GIF object with 2:48 the findByID method which accepts a long parameter value. 2:52 And then my save method will accept two parameter values, 2:58 a GIF and a multi-part file parameter. 3:04 And then, I will include a delete method as well. 3:08 Excellent, now I notice how I added this MultipartFile parameter to the save method 3:13 instead of using only a GIF parameter. 3:18 This is because currently our GIF entity does not include the MultipartFile 3:21 data our controller will have received. 3:25 So will let the service combine the two and I'll show you 3:27 the fairly brief code that's required to make this happen in just a bit. 3:30 Now, let's create the implementation class for this and annotate it with service. 3:35 So I will create a new Java Class, call it GifService. 3:40 This is the implementation i'll name it Impl and 3:45 let's annotate that with Service. 3:50 Next, I'll implement the GIF service interface. 3:53 Implements, GIF service. 3:57 And I'll add all of those unimplemented methods. 4:00 Let's use Option, Implement methods. 4:05 All of those that are not currently included, cool. 4:09 Now just as we did for the category service simple we'll auto wire a DAO 4:12 here but this time it's a gif DAO. 4:16 So we'll do @Autowired. 4:18 Private GifDao gifDao. 4:23 Now we're coding in the reverse order as we did with the categories so 4:27 this DAO is right here gifDao does not exist yet. 4:31 No worries though. 4:34 We'll add it in just a bit. 4:35 Let's finish our work here first. 4:37 So in the find all method, we'll simply call upon the find all method from the DAO 4:39 which we'll code in just a bit. 4:43 So gifdao.findAll. 4:45 And next, in the find by ID method, 4:49 we'll call upon an identical method in the DAO was well, 4:52 gifDao.findById and will pass along that ID parameter value. 4:56 Now let's jump to the delete method and 5:01 do the same before returning to the save method. 5:04 So here we'll just do gifDao.delete Gif. 5:07 Now, back to that save method. 5:14 I said we'd have the service put together this multi-part file data and 5:17 the GIF entity, before persisting the GIF entity. 5:21 To do this, we'll use the gif's setBytes method and 5:24 pass the bytes from the getBytes method of that multipart file parameter. 5:28 Here's what i mean. 5:34 We'll say, gif, that's the entity we'd receive, .setBytes, 5:35 there's the setter that came from our entity. 5:38 And the bytes that we wanna use come from the file, specifically file.getBytes. 5:41 You'll notice here that you're going to either have to make this method throw 5:49 the java.io.IOException here. 5:52 Or you're gonna have to surround it with a try catch. 5:56 Let's go ahead and surround it with a try catch here. 5:59 So I will surround with a try catch. 6:02 Cool and right here, 6:05 all says' instead of printing the stack trace all do system.arrow.print one. 6:07 And I’ll say, unable to get byte array from uploaded file. 6:13 And now that the GIF object will contain everything it needs to persist it's dated 6:21 to the database. 6:24 After that called a set bytes here, let's call the DAOs save method. 6:25 So gitDao.save(gif). 6:31 All right, that's it for the service. 6:36 Next, we'll tackle the DAO and that will get rid of all the compiler 6:38 errors we're seeing here since we're coding in the reverse order. 6:42
You need to sign up for Treehouse in order to download course files.Sign up