Changeset 7fafead in Git for src/main/java/com/wediscussmovies/project/web/controller
- Timestamp:
- 01/16/22 20:22:55 (3 years ago)
- Branches:
- main
- Children:
- 3ded84d
- Parents:
- 2d57cad (diff), 7bc8942 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- src/main/java/com/wediscussmovies/project/web/controller
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/wediscussmovies/project/web/controller/DiscussionsController.java
r2d57cad r7fafead 1 1 package com.wediscussmovies.project.web.controller; 2 2 3 import com.wediscussmovies.project.model.*; 4 import com.wediscussmovies.project.model.enumerations.DiscussionType; 5 import com.wediscussmovies.project.service.DiscussionService; 6 import com.wediscussmovies.project.service.MovieService; 7 import com.wediscussmovies.project.service.PersonService; 8 import com.wediscussmovies.project.service.ReplyService; 3 9 import org.springframework.stereotype.Controller; 10 import org.springframework.ui.Model; 11 import org.springframework.web.bind.annotation.*; 12 13 import javax.servlet.http.HttpServletRequest; 14 import java.sql.Date; 15 import java.time.LocalDate; 16 import java.util.ArrayList; 17 import java.util.List; 18 import java.util.Optional; 4 19 5 20 @Controller 21 @RequestMapping("/discussions") 6 22 public class DiscussionsController { 23 private final DiscussionService discussionService; 24 private final ReplyService replyService; 25 private final MovieService movieService; 26 private final PersonService personService; 27 public DiscussionsController(DiscussionService discussionService, ReplyService replyService, MovieService movieService, PersonService personService) { 28 this.discussionService = discussionService; 29 this.replyService = replyService; 30 this.movieService = movieService; 31 this.personService = personService; 32 } 33 34 @GetMapping("/") 35 public String getDiscussions(@RequestParam(required = false) String titleSearch, 36 Model model){ 37 List<Discussion> discussions = discussionService.listAll(); 38 if(titleSearch != null && !titleSearch.isEmpty()){ 39 discussions = discussionService.listAllByTitle(titleSearch); 40 } 41 model.addAttribute("discussions", discussions); 42 model.addAttribute("contentTemplate", "discussionsList"); 43 return "template"; 44 } 45 46 @GetMapping("/{id}") 47 public String getDiscussion( 48 @PathVariable Integer id, 49 Model model){ 50 Optional<Discussion> discussion = discussionService.findById(id); 51 if(discussion.isEmpty()) 52 return "redirect:/discussions"; 53 model.addAttribute("discussion", discussion); 54 model.addAttribute("contentTemplate", "discussionsDiscussion"); 55 return "template"; 56 } 57 58 @GetMapping("/{id}/reply") 59 public String getReplyToDiscussion( 60 @PathVariable Integer id, 61 Model model){ 62 Optional<Discussion> discussion = discussionService.findById(id); 63 if(discussion.isEmpty()) 64 return "redirect:/discussions"; 65 model.addAttribute("discussion", discussion); 66 model.addAttribute("contentTemplate", "discussionsReply"); 67 return "template"; 68 } 69 @PostMapping("/{id}/reply/confirm") 70 public String getReplyToDiscussionConfirm( 71 @PathVariable Integer id, 72 @RequestParam String text, 73 HttpServletRequest request){ 74 Optional<Discussion> discussion = discussionService.findById(id); 75 User user = (User) request.getSession().getAttribute("user"); 76 if(user == null){ 77 return "redirect:/login"; 78 } 79 Date date = Date.valueOf(LocalDate.now()); 80 if(discussion.isEmpty()) 81 return "redirect:/discussions"; 82 Reply reply = new Reply(discussion.get(), user, date, text); 83 replyService.save(reply); 84 return "redirect:/discussions/"+id; 85 } 86 87 @GetMapping("/movies/add/{id}") 88 public String getAddDiscussionForMovie(Model model, 89 @PathVariable Integer id, 90 HttpServletRequest request){ 91 model.addAttribute("contentTemplate", "discussionsAdd"); 92 request.setAttribute("movieId", id); 93 return "template"; 94 } 95 96 @PostMapping("/movies/add/confirm") 97 public String getAddDiscussionMovieConfirm(Model model, 98 HttpServletRequest request, 99 @RequestParam String title, 100 @RequestParam String text){ 101 User user = (User) request.getSession().getAttribute("user"); 102 if(user == null){ 103 return "redirect:/login"; 104 } 105 Long movie_id = (Long) request.getSession().getAttribute("movieId"); 106 request.getSession().setAttribute("movieId", null); 107 Optional<Movie> movieOp = movieService.findById(movie_id); 108 if(movieOp.isEmpty()) 109 return "redirect:/movies"; 110 Discussion discussion = new Discussion(DiscussionType.M,text, title, Date.valueOf(LocalDate.now()),user,movieOp.get(),null,new ArrayList<>()); 111 discussionService.save(discussion); 112 return "redirect:/discussions"; 113 } 114 115 116 @GetMapping("/persons/add/{id}") 117 public String getAddDiscussionForPerson(Model model, 118 @PathVariable Integer id, 119 HttpServletRequest request){ 120 model.addAttribute("contentTemplate", "discussionsAdd"); 121 request.setAttribute("personId", id); 122 return "template"; 123 } 124 125 @PostMapping("/persons/add/confirm") 126 public String getAddDiscussionForPersonConfirm(Model model, 127 HttpServletRequest request, 128 @RequestParam String title, 129 @RequestParam String text){ 130 User user = (User) request.getSession().getAttribute("user"); 131 if(user == null){ 132 return "redirect:/login"; 133 } 134 Integer person_id = (Integer) request.getSession().getAttribute("personId"); 135 Optional<Person> personOp = personService.findById(person_id); 136 request.getSession().setAttribute("personId", null); 137 if(personOp.isEmpty()) 138 return "redirect:/discussions"; 139 Discussion discussion = new Discussion(DiscussionType.P,title, text,Date.valueOf(LocalDate.now()),user,null,personOp.get(),new ArrayList<>()); 140 discussionService.save(discussion); 141 return "redirect:/discussions"; 142 } 143 144 7 145 } -
src/main/java/com/wediscussmovies/project/web/controller/MovieController.java
r2d57cad r7fafead 1 1 package com.wediscussmovies.project.web.controller; 2 2 3 import com.wediscussmovies.project.exception.MovieIdNotFoundException; 4 import com.wediscussmovies.project.model.Genre; 3 5 import com.wediscussmovies.project.model.Movie; 6 import com.wediscussmovies.project.model.Person; 7 import com.wediscussmovies.project.service.GenreService; 4 8 import com.wediscussmovies.project.service.MovieService; 9 import com.wediscussmovies.project.service.PersonService; 5 10 import org.springframework.stereotype.Controller; 6 11 import org.springframework.ui.Model; 7 import org.springframework.web.bind.annotation. GetMapping;8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestParam;10 12 import org.springframework.web.bind.annotation.*; 13 14 import java.sql.Date; 15 import java.util.LinkedList; 11 16 import java.util.List; 17 import java.util.Optional; 12 18 13 19 @Controller … … 15 21 public class MovieController { 16 22 private final MovieService movieService; 17 18 public MovieController(MovieService movieService) { 23 private final GenreService genreService; 24 private final PersonService personService; 25 26 public MovieController(MovieService movieService, GenreService genreService, PersonService personService) { 19 27 this.movieService = movieService; 28 this.genreService = genreService; 29 this.personService = personService; 20 30 } 21 31 … … 29 39 movies = movieService.searchByTitle(titleQuery); 30 40 } 41 42 movies.sort(Movie.comparatorTitle); 43 31 44 model.addAttribute("movies", movies); 32 45 model.addAttribute("contentTemplate", "moviesList"); … … 34 47 } 35 48 49 50 @GetMapping("/add") 51 public String addMovie(Model model){ 52 model.addAttribute("directors", personService.findAllDirectors()); 53 model.addAttribute("actors", personService.findAllActors()); 54 model.addAttribute("genres", genreService.findAll()); 55 model.addAttribute("contentTemplate", "moviesAdd"); 56 return "template"; 57 } 58 59 60 @PostMapping("/{id}/delete") 61 public String addMovie(@PathVariable Long id){ 62 Optional<Movie> movie = movieService.findById(id); 63 if(movie.isPresent()){ 64 movieService.deleteById(movie.get().getId()); 65 } 66 return "redirect:/movies"; 67 } 68 69 @PostMapping("/add/confirm") 70 public String addMoviePost(@RequestParam String title, 71 @RequestParam String description, 72 @RequestParam String image_url, 73 @RequestParam Date airing_date, 74 @RequestParam Double rating, 75 @RequestParam Integer director_id, 76 @RequestParam List<Integer> actors, 77 @RequestParam List<Integer> genres, 78 Model model){ 79 if(title == null || title.isEmpty() || 80 description == null || description.isEmpty() || 81 image_url == null || image_url.isEmpty() || 82 airing_date == null || 83 rating == null || 84 director_id == null || 85 actors == null || actors.size() == 0 || 86 genres == null || genres.size() == 0) 87 { 88 model.addAttribute("error", "Not enough attributes, make sure all values are inputted, all of them are required"); 89 model.addAttribute("hasError", true); 90 return "redirect:/add"; 91 } 92 List<Person> actorsList = new LinkedList<>(); 93 for(Integer id: actors){ 94 Optional<Person> person = personService.findActorById(id); 95 if(person.isEmpty()){ 96 model.addAttribute("error", "The actor with ID {" + id + "} was not found."); 97 model.addAttribute("hasError", true); 98 return "redirect:/add"; 99 } 100 actorsList.add(person.get()); 101 } 102 103 List<Genre> genreList = new LinkedList<>(); 104 for(Integer id: genres){ 105 Optional<Genre> genre = genreService.findById(id); 106 if(genre.isEmpty()){ 107 model.addAttribute("error", "The genre with ID {" + id + "} was not found."); 108 model.addAttribute("hasError", true); 109 return "redirect:/add"; 110 } 111 genreList.add(genre.get()); 112 } 113 114 Optional<Person> directorOp = personService.findDirectorById(director_id); 115 if(directorOp.isEmpty()){ 116 model.addAttribute("error", "The director with ID {" + director_id + "} was not found."); 117 model.addAttribute("hasError", true); 118 return "redirect:/add"; 119 } 120 121 Person director = directorOp.get(); 122 123 Movie movie = new Movie(title, description, image_url, airing_date, 124 rating, director, actorsList, genreList); 125 126 movieService.save(movie); 127 128 return "redirect:/movies"; 129 } 130 131 @PostMapping("/edit/confirm") 132 public String editMoviePost( 133 @RequestParam Long movie_id, 134 @RequestParam String title, 135 @RequestParam String description, 136 @RequestParam String image_url, 137 @RequestParam Date airing_date, 138 @RequestParam Double rating, 139 @RequestParam Integer director_id, 140 @RequestParam List<Integer> actors, 141 @RequestParam List<Integer> genres, 142 Model model){ 143 if( 144 movie_id == null || 145 title == null || title.isEmpty() || 146 description == null || description.isEmpty() || 147 image_url == null || image_url.isEmpty() || 148 airing_date == null || 149 rating == null || 150 director_id == null || 151 actors == null || actors.size() == 0 || 152 genres == null || genres.size() == 0) 153 { 154 model.addAttribute("error", "Not enough attributes, make sure all values are inputted, all of them are required"); 155 model.addAttribute("hasError", true); 156 return "redirect:/edit"; 157 } 158 159 Optional<Movie> movieOptional = movieService.findById(movie_id); 160 if(movieOptional.isEmpty()){ 161 model.addAttribute("error", "The movie with ID {" + movie_id + "} was not found."); 162 model.addAttribute("hasError", true); 163 return "redirect:/edit"; 164 } 165 Movie movie = movieOptional.get(); 166 167 List<Person> actorsList = new LinkedList<>(); 168 for(Integer id: actors){ 169 Optional<Person> person = personService.findActorById(id); 170 if(person.isEmpty()){ 171 model.addAttribute("error", "The actor with ID {" + id + "} was not found."); 172 model.addAttribute("hasError", true); 173 return "redirect:/edit"; 174 } 175 actorsList.add(person.get()); 176 } 177 178 List<Genre> genreList = new LinkedList<>(); 179 for(Integer id: genres){ 180 Optional<Genre> genre = genreService.findById(id); 181 if(genre.isEmpty()){ 182 model.addAttribute("error", "The genre with ID {" + id + "} was not found."); 183 model.addAttribute("hasError", true); 184 return "redirect:/edit"; 185 } 186 genreList.add(genre.get()); 187 } 188 189 Optional<Person> directorOp = personService.findDirectorById(director_id); 190 if(directorOp.isEmpty()){ 191 model.addAttribute("error", "The director with ID {" + director_id + "} was not found."); 192 model.addAttribute("hasError", true); 193 return "redirect:/edit"; 194 } 195 196 Person director = directorOp.get(); 197 198 movieService.deleteById(movie_id); 199 200 movie.setActors(actorsList); 201 movie.setDirector(director); 202 movie.setGenres(genreList); 203 movie.setTitle(title); 204 movie.setDescription(description); 205 movie.setAringDate(airing_date); 206 movie.setImageUrl(image_url); 207 movie.setImbdRating(rating); 208 209 movieService.save(movie); 210 211 return "redirect:/movies"; 212 } 213 214 @GetMapping("/{id}/edit") 215 public String editMovie(@PathVariable Long id, Model model){ 216 Movie movie = movieService.findById(id).orElseThrow(() -> new MovieIdNotFoundException(id)); 217 model.addAttribute("directors", personService.findAllDirectors()); 218 model.addAttribute("actors", personService.findAllActors()); 219 model.addAttribute("genres", genreService.findAll()); 220 model.addAttribute("movie", movie); 221 model.addAttribute("contentTemplate", "moviesEdit"); 222 return "template"; 223 } 36 224 } -
src/main/java/com/wediscussmovies/project/web/controller/UserController.java
r2d57cad r7fafead 1 1 package com.wediscussmovies.project.web.controller; 2 2 3 import com.wediscussmovies.project.model.PasswordEncoder; 4 import com.wediscussmovies.project.model.User; 5 import com.wediscussmovies.project.model.exception.InvalidUserCredentialsException; 6 import com.wediscussmovies.project.service.UserService; 3 7 import org.springframework.stereotype.Controller; 8 import org.springframework.ui.Model; 9 import org.springframework.web.bind.annotation.GetMapping; 10 import org.springframework.web.bind.annotation.PostMapping; 11 import org.springframework.web.bind.annotation.RequestMapping; 12 import org.springframework.web.bind.annotation.RequestParam; 13 14 import javax.servlet.http.HttpServletRequest; 15 import java.security.NoSuchAlgorithmException; 16 import java.util.Optional; 4 17 5 18 @Controller 19 @RequestMapping("/") 6 20 public class UserController { 21 private final UserService userService; 22 23 public UserController(UserService userService) { 24 this.userService = userService; 25 } 26 27 @GetMapping("/login") 28 public String getLoginPage(Model model){ 29 return "login"; 30 } 31 32 @PostMapping("/login/confirm") 33 public String confirmLogin(HttpServletRequest request, Model model, 34 @RequestParam String username, 35 @RequestParam String password){ 36 Optional<User> user; 37 try{ 38 password = PasswordEncoder.getEncodedPasswordString(password); 39 } 40 catch (NoSuchAlgorithmException ex){ 41 model.addAttribute("hasError", true); 42 model.addAttribute("error", ex.getMessage()); 43 return "login"; 44 } 45 46 try{ 47 user = this.userService.login(username, password); 48 request.getSession().setAttribute("user", user); 49 request.getSession().setAttribute("loggedIn",true); 50 return "redirect:/movies"; 51 } 52 catch (InvalidUserCredentialsException ex){ 53 model.addAttribute("hasError", true); 54 model.addAttribute("error", ex.getMessage()); 55 return "login"; 56 } 57 } 58 59 @GetMapping("/register") 60 public String getRegisterPage(){ 61 return "register"; 62 } 63 64 @PostMapping("/login/confirm") 65 public String confirmRegister(HttpServletRequest request, 66 @RequestParam String username, 67 @RequestParam String email, 68 @RequestParam String password, 69 @RequestParam String confirmPassword, 70 @RequestParam String name, 71 @RequestParam String surname){ 72 Optional<User> user; 73 74 try{ 75 password = PasswordEncoder.getEncodedPasswordString(password); 76 confirmPassword = PasswordEncoder.getEncodedPasswordString(confirmPassword); 77 } 78 catch (NoSuchAlgorithmException ex){ 79 request.getSession().setAttribute("error", "Contact the administrators!"); 80 request.getSession().setAttribute("hasError", "true"); 81 return "redirect:/movies"; 82 } 83 84 user = this.userService.register(request, email, password, confirmPassword, username, name, surname); 85 if(user.isEmpty()){ 86 request.setAttribute("hasError", "true"); 87 }else{ 88 request.getSession().setAttribute("hasError", "false"); 89 request.getSession().setAttribute("user", user.get()); 90 request.getSession().setAttribute("loggedIn",true); 91 } 92 return "redirect:/movies"; 93 } 94 95 @GetMapping 96 public String logout(HttpServletRequest request){ 97 request.getSession().invalidate(); 98 return "redirect:/login"; 99 } 7 100 }
Note:
See TracChangeset
for help on using the changeset viewer.