Add a CategoryController9:40 with Chris Ramacciotti
In this video, you are tasked with creating a controller to handle requests related to categories.
With our model and data repository in place, let's move on to the controller. 0:00 We'll need a controller for categories that can capture requests to a URI for 0:05 all the categories and a URI for a single category. 0:09 So, here's your next task. 0:13 Create a Category Controller with one method [SOUND] that captures requests to 0:16 /categories and returns a String. 0:20 This method should fetch the entire list of category objects from 0:23 an auto-wired category repository and add it to a ModelMap. 0:26 [SOUND] Then, the method should return the string categories to associate it 0:30 with the provided Thymeleaf Template. 0:35 Are you ready for this? 0:37 Okay. 0:39 Go ahead, have at it. 0:40 But wait, remember to use the GIF controller as an example to follow. 0:41 Ready, set, code. 0:46 >> All right, what you're looking at here is my category controller class, 0:48 which I put in the controller package with the other controller. 0:52 You can see that I've annotated the class with Controller and included an Autowired 0:55 category repository that I can use in the controller method down here. 1:00 As for this method, I've annotated it with request mapping to capture 1:05 any requests for the category list page, which is /categories. 1:09 Here is my modelMap parameter, and inside is the the call to the repository's 1:14 getALLCategories method that we wrote together earlier. 1:19 I added the list that was returned by this method to the modelMap using 1:22 modelmap.put here. 1:27 And here I've returned the name of the time leave template, or categories. 1:28 This is will ensure that it's the categories.html template that will be 1:34 rendered when this page is requested. 1:37 The last piece of this controller, we'll do together. 1:41 We'll need another method that will capture requests to an individual 1:44 category. 1:46 To start that, I'll request a mapping to a single category page, 1:47 category/, and here we're going to use an integer id. 1:52 The name won't work well as a URI placeholder 1:57 here because names could have spaces in them. 2:00 So instead, I'll use the integer id as a placeholder. 2:03 Then we'll start typing the method public String category. 2:08 And the first parameter I'm going to list is our @PathVariable. 2:14 That will capture the value that's passed into 2:18 the id placeholder that's in our RequestMapping annotation. 2:21 So I will call it id. 2:26 And of course, we'll include an instance of a modelMap that'll 2:27 be passed in by the Spring framework. 2:31 >> Now, I know I'm going to return the single categories template, 2:35 which is given in category.html, so I'll type in that return value right now. 2:38 That's going to be return "category". 2:44 Now I know the category page in the browser should probably 2:49 contain the category name, as well as 2:52 all GIFs related to that category, >> So by the time we reach the return 2:54 statement we better have added to the model map, both a category object 2:59 associated with this ID as well as a list of GIFs associated with this category. 3:04 Let's tackle each of those now. 3:11 First, the category object. 3:13 You just wrote a findById method, so 3:15 we'll call that now and add the result to the model map. 3:17 I'll create the category object, 3:19 And use the categoryRepository field and call its findById method 3:27 passing it the integer id that came into our path variable. 3:32 Then I'll add the category object that was returned by the findById method to 3:37 the model, using modelMap.put. 3:41 The name I'll give it will be simply category, and I'll pass category. 3:46 Whatever value was stored in there as a result of this return value, 3:50 I will add to the ModelMap. 3:54 Now onto the next item. 3:57 For a list of the GIFs associated with this category, we'll need to use a GIF 3:58 repository to fetch all those GIFs within a certain category ID, like this. 4:03 I'll give myself a couple lines to separate this code. 4:10 And the first thing I'll do is define a list that'll 4:13 hold that list of GIFs that comes back from the GIF repository. 4:15 Make sure to import the GIF class there, since we haven't done that yet. 4:19 And I'll call the gifRepositories, 4:24 findByCategoryId method, passing it the id. 4:28 And finally, adding that list to the model map with modelMap.put. 4:33 I'll call that "gifs" in the map key, and pass it the object gifs. 4:38 But wait, there are two problems with this. 4:47 First, there is no GIF repository variable defined. 4:50 And second, there is no findByCategoryId method defined for 4:53 a GIF repository object. 4:57 The first one we can fix right here. 4:59 We'll simply add an Autowired gift repository to the object. 5:00 So I'll add that with the @Autowired annotation, add a GifRepository. 5:06 That fixes the first error, but 5:14 we still have this error with the missing findByCategoryId method. 5:16 This one will take a bit more work. 5:21 But it's still pretty quick. 5:23 We'll hop over to the GifRepository class, and add a findByCategoryId method. 5:24 So let's scroll down here and I'll just add it to the bottom. 5:32 That'll return a list. 5:35 FindByCategoryId, and let's have it accept a single end parameter. 5:41 So this method, given some int id, will essentially iterate over our 5:48 list up here looking for objects that have a given category id. 5:53 If they haven't given a category id, 5:58 I will include those in the list that I return. 6:00 So to start, we better initialize a brand new empty list. 6:03 I'll make it a list of GIFs and initialize it as a new ArrayList. 6:08 Make sure to import those classes, excellent. 6:18 Now, let's use a for each loop to iterate over the list of ALL_GIFS. 6:22 So for every Gif, I keep typing Fig, how about Gif? 6:28 For every Gif in ALL_GIFS Let's 6:33 check to see if the category id of the Gif object in this iteration of the loop is 6:38 equal to the id value that was passed in as a parameter. 6:43 So, if gif.getCategoryId ==, 6:46 I can use double equals here. 6:51 It's a primitive value. 6:54 I don't need to use dot equals. 6:55 Then what I want to do is add that to the list. 6:59 So, by the time we get through this loop, any Gif in the list that has the category 7:05 ID that was passed in as a parameter value, 7:10 will be added to the list name Gifs. 7:12 And once I get through my loop, what I'd like to do is return 7:14 whatever list has resulted by iterating over all gifs. 7:19 Now that we've coded this method, after we jumped back to the category controller 7:24 class, we should see that that error right there has been resolved, 7:28 since we created that method. 7:33 At this point, we should at least be able to see the category list page, 7:36 even if it hasn't been infused with live data. 7:40 Let's redeploy and check it out. 7:44 So if your boot run task is still running, go ahead and stop it. 7:46 And after a few moments, 7:50 you can rerun that bootRun task to reboot your server and redeploy your web app. 7:52 And it looks like I have a compiler error. 7:58 I see what happened here. 8:06 So this error right here says that I can't 8:08 leave this generic between these angle brackets here empty. 8:12 And why does it say that? 8:16 Well, it says that because in my build file, if I go to my build file, 8:18 build.gradle, I said I wanted my Java source code to 8:22 be compatible with Java five, that is version 1.5, and 8:28 I have to use source 7 or higher to enable the diamond operator. 8:33 That's the operator that allows us to leave that generic empty. 8:39 So, what I'm going to do is, well, I could do one of two things, I could remove 8:43 the source compatibility here, I could increase the source compatibility here, or 8:47 I could go back to this line right here, and actually add gif right there. 8:52 I'm simply actually going to delete the source compatibility. 8:59 Delete that. 9:02 We'll save it and now I'm going to rerun the bootRun task. 9:03 And there it looks like the compilation has succeeded. 9:09 And the web server's up and running. 9:13 My application is deployed. 9:14 Let's check it out. 9:16 So I'm going to go back to my main page. 9:19 There's the home page, we didn't touch it in this last task. 9:22 What we did touch, though, is the Categories page. 9:24 And there it is! 9:28 It's working! 9:29 We see that the template is actually displayed, though it has static HTML. 9:31 And from this, you can probably guess what your final task is going to be. 9:35
You need to sign up for Treehouse in order to download course files.Sign up