1 | package com.wediscussmovies.project.web.controller;
|
---|
2 |
|
---|
3 | import com.wediscussmovies.project.model.Movie;
|
---|
4 | import com.wediscussmovies.project.model.Person;
|
---|
5 | import com.wediscussmovies.project.model.User;
|
---|
6 | import com.wediscussmovies.project.service.GenreService;
|
---|
7 | import com.wediscussmovies.project.service.MovieService;
|
---|
8 | import com.wediscussmovies.project.service.PersonService;
|
---|
9 | import lombok.Data;
|
---|
10 | import org.springframework.format.annotation.DateTimeFormat;
|
---|
11 | import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
---|
12 | import org.springframework.security.core.Authentication;
|
---|
13 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
14 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
15 | import org.springframework.stereotype.Controller;
|
---|
16 | import org.springframework.ui.Model;
|
---|
17 | import org.springframework.web.bind.annotation.*;
|
---|
18 |
|
---|
19 | import java.sql.Date;
|
---|
20 | import java.time.LocalDate;
|
---|
21 | import java.util.List;
|
---|
22 |
|
---|
23 | @Controller
|
---|
24 | @RequestMapping("/movies")
|
---|
25 | public class MovieController {
|
---|
26 | private final MovieService movieService;
|
---|
27 | private final GenreService genreService;
|
---|
28 | private final PersonService personService;
|
---|
29 |
|
---|
30 | public MovieController(MovieService movieService, GenreService genreService, PersonService personService) {
|
---|
31 | this.movieService = movieService;
|
---|
32 | this.genreService = genreService;
|
---|
33 | this.personService = personService;
|
---|
34 | }
|
---|
35 |
|
---|
36 | @GetMapping
|
---|
37 | public String getMovies(@RequestParam(required = false) String titleQuery, Model model,
|
---|
38 | @RequestParam(required = false) String error){
|
---|
39 | List<Movie> movies;
|
---|
40 | if(titleQuery == null ) {
|
---|
41 | movies = movieService.listAll();
|
---|
42 | }
|
---|
43 | else{
|
---|
44 | movies = movieService.searchByTitle(titleQuery);
|
---|
45 | }
|
---|
46 |
|
---|
47 | Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
---|
48 |
|
---|
49 | if (!(auth instanceof AnonymousAuthenticationToken)){
|
---|
50 | UserDetails userDetails = (UserDetails) auth.getPrincipal();
|
---|
51 | User user = (User) userDetails;
|
---|
52 | model.addAttribute("likedMovies",this.movieService.findLikedMoviesByUser(user));
|
---|
53 | model.addAttribute("user",user);
|
---|
54 | }
|
---|
55 |
|
---|
56 | model.addAttribute("movies", movies);
|
---|
57 | model.addAttribute("contentTemplate", "moviesList");
|
---|
58 | if (error != null && !error.equals(" "))
|
---|
59 | model.addAttribute("error",error);
|
---|
60 | return "template";
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | @GetMapping("/add")
|
---|
65 | public String addMovie(Model model){
|
---|
66 |
|
---|
67 | addModelProperties(model);
|
---|
68 | model.addAttribute("contentTemplate", "moviesAdd");
|
---|
69 | return "template";
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | @GetMapping("/{id}/delete")
|
---|
74 | public String deleteMovie(@PathVariable Integer id){
|
---|
75 |
|
---|
76 | try {
|
---|
77 | this.movieService.deleteById(id);
|
---|
78 | return "redirect:/movies";
|
---|
79 |
|
---|
80 | }
|
---|
81 | catch (RuntimeException exc){
|
---|
82 | return "redirect:/movies?error="+exc.getMessage();
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | @PostMapping("/save")
|
---|
87 | public String saveMovie(
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 | @RequestParam String title,
|
---|
92 | @RequestParam String description,
|
---|
93 | @RequestParam String imageUrl,
|
---|
94 | @RequestParam(required = false) Double rating,
|
---|
95 | @RequestParam(required = false) Integer directorId,
|
---|
96 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate airingDate,
|
---|
97 | @RequestParam List<Integer> actors,
|
---|
98 | @RequestParam List<Integer> genres
|
---|
99 | ){
|
---|
100 |
|
---|
101 | try {
|
---|
102 | Movie movie = this.movieService.save(title,description,imageUrl,Date.valueOf(airingDate),rating,directorId,actors,genres);
|
---|
103 |
|
---|
104 | return "redirect:/movies";
|
---|
105 | }
|
---|
106 | catch (RuntimeException exc){
|
---|
107 | return "redirect:/movies?error="+exc.getMessage();
|
---|
108 | }
|
---|
109 | }
|
---|
110 | @PostMapping("/save/{movieId}")
|
---|
111 | public String updateMovie(
|
---|
112 |
|
---|
113 |
|
---|
114 | @PathVariable Integer movieId,
|
---|
115 | @RequestParam String title,
|
---|
116 | @RequestParam String description,
|
---|
117 | @RequestParam String imageUrl,
|
---|
118 | @RequestParam(required = false) Double rating,
|
---|
119 | @RequestParam(required = false) Integer directorId,
|
---|
120 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate airingDate,
|
---|
121 | @RequestParam List<Integer> actors,
|
---|
122 | @RequestParam List<Integer> genres
|
---|
123 | ){
|
---|
124 |
|
---|
125 | try {
|
---|
126 | Movie movie = this.movieService.edit(movieId,title,description,imageUrl,Date.valueOf(airingDate),rating,directorId,actors,genres);
|
---|
127 |
|
---|
128 | return "redirect:/movies";
|
---|
129 | }
|
---|
130 | catch (RuntimeException exc){
|
---|
131 | return "redirect:/movies?error="+exc.getMessage();
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 |
|
---|
137 | @GetMapping("/{id}/edit")
|
---|
138 | public String editMovie(@PathVariable Integer id, Model model){
|
---|
139 | try {
|
---|
140 | Movie movie = movieService.findById(id);
|
---|
141 | addModelProperties(model);
|
---|
142 | model.addAttribute("movie", movie);
|
---|
143 | model.addAttribute("director",movie.getDirector());
|
---|
144 | model.addAttribute("movieActors",movieService.findAllActorsForMovie(movie));
|
---|
145 | model.addAttribute("movieGenres",movieService.findAllGenresForMovie(movie));
|
---|
146 | model.addAttribute("contentTemplate", "moviesAdd");
|
---|
147 | return "template";
|
---|
148 |
|
---|
149 | }
|
---|
150 | catch (RuntimeException exc){
|
---|
151 | return "redirect:/movies?error="+exc.getMessage();
|
---|
152 | }
|
---|
153 |
|
---|
154 | }
|
---|
155 | private void addModelProperties(Model model){
|
---|
156 |
|
---|
157 | model.addAttribute("directors", personService.findAllDirectors());
|
---|
158 | model.addAttribute("actors", personService.findAllActors());
|
---|
159 | model.addAttribute("genres", genreService.findAll());
|
---|
160 | }
|
---|
161 | }
|
---|