|
10 | 10 | import static com.redis.cache.demo.SearchService.FIELD_VOTE_COUNT;
|
11 | 11 |
|
12 | 12 | import java.util.Arrays;
|
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
13 | 15 |
|
14 | 16 | import org.springframework.beans.factory.annotation.Autowired;
|
15 | 17 | import org.springframework.stereotype.Controller;
|
|
18 | 20 | import org.springframework.web.bind.annotation.PathVariable;
|
19 | 21 | import org.springframework.web.bind.annotation.RequestParam;
|
20 | 22 |
|
| 23 | +import com.redis.cache.RedisCache; |
| 24 | +import com.redis.cache.RedisCacheManager; |
| 25 | + |
21 | 26 | @Controller
|
22 | 27 | public class MovieController {
|
23 | 28 |
|
24 |
| - @Autowired |
25 |
| - MovieService movieService; |
26 |
| - |
27 |
| - @Autowired |
28 |
| - SearchService searchService; |
29 |
| - |
30 |
| - @GetMapping("/movie/{id}") |
31 |
| - public String movie(Model model, @PathVariable("id") Integer id) { |
32 |
| - model.addAttribute("movie", movieService.fetchMovie(id)); |
33 |
| - return "movie"; |
34 |
| - } |
35 |
| - |
36 |
| - @GetMapping({ "/movies/popular", "/" }) |
37 |
| - public String getPopularMovies(Model model, @RequestParam(defaultValue = "1", name = "page") int page) |
38 |
| - throws Exception { |
39 |
| - // Add the movies to the model |
40 |
| - model.addAttribute("movies", movieService.fetchPopularMovies(page)); |
41 |
| - model.addAttribute("page", page); |
42 |
| - // Return the view name |
43 |
| - return "popular"; |
44 |
| - } |
45 |
| - |
46 |
| - @GetMapping("/movies/top-rated") |
47 |
| - public String getTopRatedMovies(Model model, @RequestParam(defaultValue = "1", name = "page") int page) |
48 |
| - throws Exception { |
49 |
| - |
50 |
| - // Add the movies to the model |
51 |
| - model.addAttribute("movies", movieService.fetchTopRatedMovies(page)); |
52 |
| - model.addAttribute("page", page); |
53 |
| - |
54 |
| - // Return the view name |
55 |
| - return "top-rated"; |
56 |
| - } |
57 |
| - |
58 |
| - @GetMapping("/movies/upcoming") |
59 |
| - public String getUpcomingMovies(Model model, @RequestParam(defaultValue = "1", name = "page") int page) |
60 |
| - throws Exception { |
61 |
| - // Add the movies to the model |
62 |
| - model.addAttribute("movies", movieService.fetchUpcomingMovies(page)); |
63 |
| - model.addAttribute("page", page); |
64 |
| - |
65 |
| - // Return the view name |
66 |
| - return "upcoming"; |
67 |
| - } |
68 |
| - |
69 |
| - @GetMapping("/movies/search") |
70 |
| - public String search(Model model, @RequestParam(name = "query", defaultValue = "") String query) { |
71 |
| - // Add the movies to the model |
72 |
| - model.addAttribute("movies", searchService.searchMovies(query)); |
73 |
| - model.addAttribute("query", query); |
74 |
| - model.addAttribute("fields", Arrays.asList(FIELD_ID, FIELD_POPULARITY, FIELD_RELEASE_DATE, FIELD_RUNTIME, |
75 |
| - FIELD_STATUS, FIELD_TAGLINE, FIELD_VOTE_AVERAGE, FIELD_VOTE_COUNT)); |
76 |
| - |
77 |
| - // Return the view name |
78 |
| - return "search"; |
79 |
| - } |
| 29 | + @Autowired |
| 30 | + MovieService movieService; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + SearchService searchService; |
| 34 | + |
| 35 | + @Autowired |
| 36 | + RedisCacheManager cacheManager; |
| 37 | + |
| 38 | + @GetMapping("/movie/{id}") |
| 39 | + public String movie(Model model, @PathVariable("id") Integer id) { |
| 40 | + model.addAttribute("movie", movieService.fetchMovie(id)); |
| 41 | + return "movie"; |
| 42 | + } |
| 43 | + |
| 44 | + @GetMapping({ "/movies/popular", "/" }) |
| 45 | + public String getPopularMovies(Model model, @RequestParam(defaultValue = "1", name = "page") int page) throws Exception { |
| 46 | + // Add the movies to the model |
| 47 | + model.addAttribute("movies", movieService.fetchPopularMovies(page)); |
| 48 | + model.addAttribute("page", page); |
| 49 | + // Return the view name |
| 50 | + return "popular"; |
| 51 | + } |
| 52 | + |
| 53 | + @GetMapping("/movies/top-rated") |
| 54 | + public String getTopRatedMovies(Model model, @RequestParam(defaultValue = "1", name = "page") int page) throws Exception { |
| 55 | + |
| 56 | + // Add the movies to the model |
| 57 | + model.addAttribute("movies", movieService.fetchTopRatedMovies(page)); |
| 58 | + model.addAttribute("page", page); |
| 59 | + |
| 60 | + // Return the view name |
| 61 | + return "top-rated"; |
| 62 | + } |
| 63 | + |
| 64 | + @GetMapping("/movies/upcoming") |
| 65 | + public String getUpcomingMovies(Model model, @RequestParam(defaultValue = "1", name = "page") int page) throws Exception { |
| 66 | + // Add the movies to the model |
| 67 | + model.addAttribute("movies", movieService.fetchUpcomingMovies(page)); |
| 68 | + model.addAttribute("page", page); |
| 69 | + |
| 70 | + // Return the view name |
| 71 | + return "upcoming"; |
| 72 | + } |
| 73 | + |
| 74 | + @GetMapping("/movies/search") |
| 75 | + public String search(Model model, @RequestParam(name = "query", defaultValue = "") String query) { |
| 76 | + // Add the movies to the model |
| 77 | + model.addAttribute("movies", searchService.searchMovies(query)); |
| 78 | + model.addAttribute("query", query); |
| 79 | + model.addAttribute("fields", Arrays.asList(FIELD_ID, FIELD_POPULARITY, FIELD_RELEASE_DATE, FIELD_RUNTIME, FIELD_STATUS, |
| 80 | + FIELD_TAGLINE, FIELD_VOTE_AVERAGE, FIELD_VOTE_COUNT)); |
| 81 | + |
| 82 | + // Return the view name |
| 83 | + return "search"; |
| 84 | + } |
| 85 | + |
| 86 | + @GetMapping("/cache") |
| 87 | + public String cacheInfo(Model model) { |
| 88 | + Map<String, Long> cacheInfo = new HashMap<>(); |
| 89 | + RedisCache cache = (RedisCache) cacheManager.getCache("movie"); |
| 90 | + cacheInfo.put("movie", cache.getCount()); |
| 91 | + model.addAttribute("cacheInfo", cacheInfo); |
| 92 | + return "cache"; |
| 93 | + } |
80 | 94 |
|
81 | 95 | }
|
0 commit comments