Changeset e0ef1b1 in Git for src/main/java/com/wediscussmovies/project/web/controller/MovieController.java
- Timestamp:
- 02/04/22 23:22:13 (3 years ago)
- Branches:
- main
- Children:
- 5b447b0
- Parents:
- 3ded84d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/wediscussmovies/project/web/controller/MovieController.java
r3ded84d re0ef1b1 1 1 package com.wediscussmovies.project.web.controller; 2 2 3 import com.wediscussmovies.project. exception.MovieIdNotFoundException;4 import com.wediscussmovies.project.model. *;5 3 import com.wediscussmovies.project.model.Movie; 4 import com.wediscussmovies.project.model.Person; 5 import com.wediscussmovies.project.model.User; 6 6 import com.wediscussmovies.project.service.GenreService; 7 7 import com.wediscussmovies.project.service.MovieService; 8 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; 9 15 import org.springframework.stereotype.Controller; 10 16 import org.springframework.ui.Model; … … 12 18 13 19 import java.sql.Date; 14 import java. util.LinkedList;20 import java.time.LocalDate; 15 21 import java.util.List; 16 import java.util.Optional;17 22 18 23 @Controller … … 30 35 31 36 @GetMapping 32 public String getMovies(@RequestParam(required = false) String titleQuery, Model model){ 37 public String getMovies(@RequestParam(required = false) String titleQuery, Model model, 38 @RequestParam(required = false) String error){ 33 39 List<Movie> movies; 34 40 if(titleQuery == null ) { … … 39 45 } 40 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 } 41 55 42 56 model.addAttribute("movies", movies); 43 57 model.addAttribute("contentTemplate", "moviesList"); 44 return "list"; 58 if (error != null && !error.equals(" ")) 59 model.addAttribute("error",error); 60 return "template"; 45 61 } 46 62 … … 48 64 @GetMapping("/add") 49 65 public String addMovie(Model model){ 50 model.addAttribute("directors", personService.findAllDirectors()); 51 model.addAttribute("actors", personService.findAllActors()); 52 model.addAttribute("genres", genreService.findAll()); 66 67 addModelProperties(model); 53 68 model.addAttribute("contentTemplate", "moviesAdd"); 54 69 return "template"; … … 56 71 57 72 58 @PostMapping("/{id}/delete") 59 public String addMovie(@PathVariable Integer id){ 60 Optional<Movie> movie = movieService.findById(id); 61 if(movie.isPresent()){ 62 movieService.deleteById(movie.get().getMovieId()); 63 } 64 return "redirect:/movies"; 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 } 65 84 } 66 85 67 @PostMapping("/add/confirm") 68 public String addMoviePost(@RequestParam String title, 86 @PostMapping("/save") 87 public String saveMovie( 88 89 90 91 @RequestParam String title, 69 92 @RequestParam String description, 70 @RequestParam String image _url,71 @RequestParam Date airing_date,72 @RequestParam Double rating,73 @RequestParam Integer director_id,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, 74 97 @RequestParam List<Integer> actors, 75 @RequestParam List<Integer> genres, 76 Model model){ 77 if(title == null || title.isEmpty() || 78 description == null || description.isEmpty() || 79 image_url == null || image_url.isEmpty() || 80 airing_date == null || 81 rating == null || 82 director_id == null || 83 actors == null || actors.size() == 0 || 84 genres == null || genres.size() == 0) 85 { 86 model.addAttribute("error", "Not enough attributes, make sure all values are inputted, all of them are required"); 87 model.addAttribute("hasError", true); 88 return "redirect:/add"; 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"; 89 105 } 90 List<Person> actorsList = new LinkedList<>(); 91 for(Integer id: actors){ 92 Optional<Person> person = personService.findActorById(id); 93 if(person.isEmpty()){ 94 model.addAttribute("error", "The actor with ID {" + id + "} was not found."); 95 model.addAttribute("hasError", true); 96 return "redirect:/add"; 97 } 98 actorsList.add(person.get()); 106 catch (RuntimeException exc){ 107 return "redirect:/movies?error="+exc.getMessage(); 99 108 } 109 } 110 @PostMapping("/save/{movieId}") 111 public String updateMovie( 100 112 101 List<Genre> genreList = new LinkedList<>(); 102 for(Integer id: genres){ 103 Optional<Genre> genre = genreService.findById(id); 104 if(genre.isEmpty()){ 105 model.addAttribute("error", "The genre with ID {" + id + "} was not found."); 106 model.addAttribute("hasError", true); 107 return "redirect:/add"; 108 } 109 genreList.add(genre.get()); 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"; 110 129 } 111 112 Optional<Person> directorOp = personService.findDirectorById(director_id); 113 if(directorOp.isEmpty()){ 114 model.addAttribute("error", "The director with ID {" + director_id + "} was not found."); 115 model.addAttribute("hasError", true); 116 return "redirect:/add"; 130 catch (RuntimeException exc){ 131 return "redirect:/movies?error="+exc.getMessage(); 117 132 } 118 119 Person director = directorOp.get();120 121 Movie movie = new Movie(title, description, image_url, airing_date,122 rating, director.getPersonId());123 124 movieService.save(movie);125 126 return "redirect:/movies";127 133 } 128 134 129 @PostMapping("/edit/confirm")130 public String editMoviePost(131 @RequestParam Integer movie_id,132 @RequestParam String title,133 @RequestParam String description,134 @RequestParam String image_url,135 @RequestParam Date airing_date,136 @RequestParam Double rating,137 @RequestParam Integer director_id,138 @RequestParam List<Integer> actors,139 @RequestParam List<Integer> genres,140 Model model){141 if(142 movie_id == null ||143 title == null || title.isEmpty() ||144 description == null || description.isEmpty() ||145 image_url == null || image_url.isEmpty() ||146 airing_date == null ||147 rating == null ||148 director_id == null ||149 actors == null || actors.size() == 0 ||150 genres == null || genres.size() == 0)151 {152 model.addAttribute("error", "Not enough attributes, make sure all values are inputted, all of them are required");153 model.addAttribute("hasError", true);154 return "redirect:/edit";155 }156 135 157 Optional<Movie> movieOptional = movieService.findById(movie_id);158 if(movieOptional.isEmpty()){159 model.addAttribute("error", "The movie with ID {" + movie_id + "} was not found.");160 model.addAttribute("hasError", true);161 return "redirect:/edit";162 }163 Movie movie = movieOptional.get();164 165 List<Person> actorsList = new LinkedList<>();166 for(Integer id: actors){167 Optional<Person> person = personService.findActorById(id);168 if(person.isEmpty()){169 model.addAttribute("error", "The actor with ID {" + id + "} was not found.");170 model.addAttribute("hasError", true);171 return "redirect:/edit";172 }173 actorsList.add(person.get());174 }175 176 List<Genre> genreList = new LinkedList<>();177 for(Integer id: genres){178 Optional<Genre> genre = genreService.findById(id);179 if(genre.isEmpty()){180 model.addAttribute("error", "The genre with ID {" + id + "} was not found.");181 model.addAttribute("hasError", true);182 return "redirect:/edit";183 }184 genreList.add(genre.get());185 }186 187 Optional<Person> directorOp = personService.findDirectorById(director_id);188 if(directorOp.isEmpty()){189 model.addAttribute("error", "The director with ID {" + director_id + "} was not found.");190 model.addAttribute("hasError", true);191 return "redirect:/edit";192 }193 194 Person director = directorOp.get();195 196 movieService.deleteById(movie_id);197 198 // // movie.setActors(actorsList);199 // movie.setDirector(director);200 // movie.setGenres(genreList);201 // movie.setTitle(title);202 // movie.setDescription(description);203 // movie.setAringDate(airing_date);204 // movie.setImageUrl(image_url);205 // movie.setImbdRating(rating);206 207 movieService.save(movie);208 209 return "redirect:/movies";210 }211 136 212 137 @GetMapping("/{id}/edit") 213 138 public String editMovie(@PathVariable Integer id, Model model){ 214 Movie movie = movieService.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id)); 215 model.addAttribute("directors", personService.findAllDirectors()); 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()); 216 158 model.addAttribute("actors", personService.findAllActors()); 217 159 model.addAttribute("genres", genreService.findAll()); 218 model.addAttribute("movie", movie);219 model.addAttribute("contentTemplate", "moviesEdit");220 return "template";221 160 } 222 161 }
Note:
See TracChangeset
for help on using the changeset viewer.