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