Add a CategoryRepository5:41 with Chris Ramacciotti
In this video, you are tasked with creating a repository for your `Category` objects.
Nice work. 0:00 We now have a way to store individual category objects. 0:01 So why don't we create a data repository that has a sample collection of them? 0:04 That brings us to your next task. 0:09 Code a CategoryRepository class that is marked as a spring component. 0:12 And includes a private list of three sample categories. 0:17 Using whatever integers you want as IDs, and named whatever you'd like. 0:20 While you're in there, add a public method that returns a list of all categories. 0:26 Now that you have your task, give this one a whirl. 0:32 If you get stuck, try following the same pattern as your gif repository class. 0:35 Okay, here we go. 0:40 If you use your GifRepository class as an example to follow, 0:42 then you're likely in good shape here. 0:45 I created a CategoryRepository class and put it into my data package. 0:48 Notice how it's annotated, with component, so that Spring will be able to 0:53 inject it into our controller via autowiring in the next task. 0:58 Then I created a private static final list, 1:03 initializing it with three arbitrary categories using the IDs 1, 2, and 3. 1:08 I used the names Technology, People, and Destruction. 1:13 Finally, I added a public method named getAllCategories, 1:18 which is just like our getAllGifs method in the GifRepository class. 1:22 Again, not too many differences here. 1:26 And the differences you do see, are in the naming of fields and methods. 1:29 But the ideas are essentially the same. 1:32 There's some sort of data structure storing our data, in each case, a list. 1:35 And there are methods that allow us to retrieve, some or all, 1:39 of that list using public methods. 1:42 As long as we're in the CategoryRepository class, 1:45 I'm going to ask something a bit more complex of you. 1:48 I'd like you to add another method to this class 1:52 that will allow us to find a category that has a given Id. 1:55 What I'm asking is you add a method named FindById. 1:59 This method should accept a single int parameter, 2:03 interrate over the static list of category objects. 2:06 And return the category object with the supplied integer Id. 2:09 If no matching category is found the method should return null. 2:14 If you use the GifRepository class as an example, again you're in good shape. 2:20 This is just like the FindByName method of that class. 2:25 What I have here, 2:28 is a public category FindById method that has a single int parameter named id. 2:29 In the method body, 2:35 I use a for each loop iterate over the collection of all categories. 2:36 That's the static list we have created here. 2:41 If during any iteration of the loop, the category object has an id that equals 2:43 the id that was passed in as a parameter, I will return that category object. 2:49 Then, if I get through the entire loop without ever encountering 2:54 the category object that has the given id, I'll return null. 2:57 And that's it for the repository. 3:02 There are a couple changes we'll have to make in order to associate each gif 3:04 object with a category object, so let's switch to gif.javanow. 3:08 First, we'll need to add a field in our GIF class that associates it with some 3:14 category. 3:19 Like we would do in a database, 3:20 I'm going to simply include a reference to the corresponding category's ID here. 3:22 This is simply one approach that we could take. 3:26 So I'll start by adding an int field. 3:29 I'll enter it right after the name, private int categoryID. 3:32 Then I'll click down here. 3:39 And use command, and to generate the getter and setter for that field. 3:41 That's the only field in this class, that currently has no getter and setter, so 3:46 it's the only one listed. 3:50 I'll hit OK, and there we have the getter and the setter. 3:52 Excellent! 3:56 Finally, I'm going to add the CategoryId as a parameter to the constructor. 3:57 And set its value in the method body. 4:01 Since it's the second field I listed here, I'll stay consistent, and 4:03 list it as a second parameter. 4:07 So int, categoryId, separate that by a comma. 4:09 And then set the value in the method body, categoryId = categoryId. 4:14 Now this means that, whenever I construct a Gif object, I'll need to include 4:20 that int for the categoryId, after the name of the Gif object. 4:25 Since I've used a constructor in the GifRepository class, 4:31 my guess is that we now have some errors over there. 4:34 Let's switch to the GifRepository class now. 4:37 Yep, and we certainly do have some errors there. 4:41 Let's arbitrarily add a CategoryID to each constructor call. 4:44 Right now, I'm not going to worry about, whether or 4:49 not the actual assigned categories make sense. 4:51 So I will just assign them arbitrarily 1, 4:54 2, 3, add another 3, 5:00 another 1, and another 2. 5:04 Cool, now I've satisfied the order, and 5:09 types of parameters accepted by this Gif constructor, with a name, 5:12 followed by the category ID, followed by the date uploaded, then the user name 5:16 of the user who uploaded the GIF, followed by whether or not it's a favorite. 5:22 So we now have associated every gif object with some category ID. 5:27 We'll tie this all together in the controller, 5:32 before we wrap up our application with a couple new views. 5:34 Hey! Speaking of controllers let's do that next 5:38
You need to sign up for Treehouse in order to download course files.Sign up