Changeset 2a5d6a3 in Git for src/main/java/com/wediscussmovies/project/web
- Timestamp:
- 01/16/22 15:29:49 (3 years ago)
- Branches:
- main
- Children:
- 7bc8942
- Parents:
- 7a0bf79
- Location:
- src/main/java/com/wediscussmovies/project/web/controller
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/wediscussmovies/project/web/controller/DiscussionsController.java
r7a0bf79 r2a5d6a3 1 1 package com.wediscussmovies.project.web.controller; 2 2 3 import com.wediscussmovies.project.model.*; 4 import com.wediscussmovies.project.service.DiscussionService; 5 import com.wediscussmovies.project.service.MovieService; 6 import com.wediscussmovies.project.service.PersonService; 7 import com.wediscussmovies.project.service.ReplyService; 3 8 import org.springframework.stereotype.Controller; 9 import org.springframework.ui.Model; 10 import org.springframework.web.bind.annotation.*; 11 12 import javax.servlet.http.HttpServletRequest; 13 import java.sql.Date; 14 import java.time.LocalDate; 15 import java.util.List; 16 import java.util.Optional; 4 17 5 18 @Controller 19 @RequestMapping("/discussions") 6 20 public class DiscussionsController { 21 private final DiscussionService discussionService; 22 private final ReplyService replyService; 23 private final MovieService movieService; 24 private final PersonService personService; 25 public DiscussionsController(DiscussionService discussionService, ReplyService replyService, MovieService movieService, PersonService personService) { 26 this.discussionService = discussionService; 27 this.replyService = replyService; 28 this.movieService = movieService; 29 this.personService = personService; 30 } 31 32 @GetMapping("/") 33 public String getDiscussions(@RequestParam(required = false) String titleSearch, 34 Model model){ 35 List<Discussion> discussions = discussionService.listAll(); 36 if(titleSearch != null && !titleSearch.isEmpty()){ 37 discussions = discussionService.listAllByTitle(titleSearch); 38 } 39 model.addAttribute("discussions", discussions); 40 model.addAttribute("contentTemplate", "discussionsList"); 41 return "template"; 42 } 43 44 @GetMapping("/{id}") 45 public String getDiscussion( 46 @PathVariable Integer id, 47 Model model){ 48 Optional<Discussion> discussion = discussionService.findById(id); 49 if(discussion.isEmpty()) 50 return "redirect:/discussions"; 51 model.addAttribute("discussion", discussion); 52 model.addAttribute("contentTemplate", "discussionsDiscussion"); 53 return "template"; 54 } 55 56 @GetMapping("/{id}/reply") 57 public String getReplyToDiscussion( 58 @PathVariable Integer id, 59 Model model){ 60 Optional<Discussion> discussion = discussionService.findById(id); 61 if(discussion.isEmpty()) 62 return "redirect:/discussions"; 63 model.addAttribute("discussion", discussion); 64 model.addAttribute("contentTemplate", "discussionsReply"); 65 return "template"; 66 } 67 @PostMapping("/{id}/reply/confirm") 68 public String getReplyToDiscussionConfirm( 69 @PathVariable Integer id, 70 @RequestParam String text, 71 HttpServletRequest request){ 72 Optional<Discussion> discussion = discussionService.findById(id); 73 User user = (User) request.getSession().getAttribute("user"); 74 if(user == null){ 75 return "redirect:/login"; 76 } 77 Date date = Date.valueOf(LocalDate.now()); 78 if(discussion.isEmpty()) 79 return "redirect:/discussions"; 80 Reply reply = new Reply(discussion.get(), user, date, text); 81 replyService.save(reply); 82 return "redirect:/discussions/"+id; 83 } 84 85 @GetMapping("/movies/add/{id}") 86 public String getAddDiscussionForMovie(Model model, 87 @PathVariable Integer id, 88 HttpServletRequest request){ 89 model.addAttribute("contentTemplate", "discussionsAdd"); 90 request.setAttribute("movieId", id); 91 return "template"; 92 } 93 94 @PostMapping("/movies/add/confirm") 95 public String getAddDiscussionMovieConfirm(Model model, 96 HttpServletRequest request, 97 @RequestParam String title, 98 @RequestParam String text){ 99 User user = (User) request.getSession().getAttribute("user"); 100 if(user == null){ 101 return "redirect:/login"; 102 } 103 Integer movie_id = (Integer) request.getSession().getAttribute("movieId"); 104 request.getSession().setAttribute("movieId", null); 105 Optional<Movie> movieOp = movieService.findById(movie_id); 106 if(movieOp.isEmpty()) 107 return "redirect:/movies"; 108 Discussion discussion = new Discussion(title, text, user, movieOp.get(), Date.valueOf(LocalDate.now())); 109 discussionService.save(discussion); 110 return "redirect:/discussions"; 111 } 112 113 114 @GetMapping("/persons/add/{id}") 115 public String getAddDiscussionForPerson(Model model, 116 @PathVariable Integer id, 117 HttpServletRequest request){ 118 model.addAttribute("contentTemplate", "discussionsAdd"); 119 request.setAttribute("personId", id); 120 return "template"; 121 } 122 123 @PostMapping("/persons/add/confirm") 124 public String getAddDiscussionForPersonConfirm(Model model, 125 HttpServletRequest request, 126 @RequestParam String title, 127 @RequestParam String text){ 128 User user = (User) request.getSession().getAttribute("user"); 129 if(user == null){ 130 return "redirect:/login"; 131 } 132 Integer person_id = (Integer) request.getSession().getAttribute("personId"); 133 Optional<Person> personOp = personService.findById(person_id); 134 request.getSession().setAttribute("personId", null); 135 if(personOp.isEmpty()) 136 return "redirect:/discussions"; 137 Discussion discussion = new Discussion(title, text, user, personOp.get(), Date.valueOf(LocalDate.now())); 138 discussionService.save(discussion); 139 return "redirect:/discussions"; 140 } 141 142 7 143 } -
src/main/java/com/wediscussmovies/project/web/controller/UserController.java
r7a0bf79 r2a5d6a3 1 1 package com.wediscussmovies.project.web.controller; 2 2 3 import com.wediscussmovies.project.model.User; 4 import com.wediscussmovies.project.model.exception.InvalidUserCredentialsException; 5 import com.wediscussmovies.project.service.UserService; 3 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.GetMapping; 9 import org.springframework.web.bind.annotation.PostMapping; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestParam; 12 13 import javax.servlet.http.HttpServletRequest; 14 import java.util.Optional; 4 15 5 16 @Controller 17 @RequestMapping("/") 6 18 public class UserController { 19 private final UserService userService; 20 21 public UserController(UserService userService) { 22 this.userService = userService; 23 } 24 25 @GetMapping("/login") 26 public String getLoginPage(){ 27 return "login"; 28 } 29 30 @PostMapping("/login/confirm") 31 public String confirmLogin(HttpServletRequest request, Model model){ 32 Optional<User> user; 33 try{ 34 user = this.userService.login(request.getParameter("username"), request.getParameter("password")); 35 request.getSession().setAttribute("user", user); 36 request.getSession().setAttribute("loggedIn",true); 37 return "redirect:/movies"; 38 } 39 catch (InvalidUserCredentialsException ex){ 40 model.addAttribute("hasError", true); 41 model.addAttribute("error", ex.getMessage()); 42 return "login"; 43 } 44 } 45 46 @GetMapping("/register") 47 public String getRegisterPage(){ 48 return "register"; 49 } 50 51 @PostMapping("/login/confirm") 52 public String confirmRegister(HttpServletRequest request, 53 @RequestParam String username, 54 @RequestParam String email, 55 @RequestParam String password, 56 @RequestParam String confirmPassword, 57 @RequestParam String name, 58 @RequestParam String surname){ 59 Optional<User> user; 60 user = this.userService.register(request, email, password, confirmPassword, username, name, surname); 61 if(user.isEmpty()){ 62 request.setAttribute("hasError", "true"); 63 }else{ 64 request.setAttribute("hasError", "false"); 65 } 66 request.getSession().setAttribute("user", user.get()); 67 request.getSession().setAttribute("loggedIn",true); 68 return "redirect:/movies"; 69 } 70 71 @GetMapping 72 public String logout(HttpServletRequest request){ 73 request.getSession().invalidate(); 74 return "redirect:/login"; 75 } 7 76 }
Note:
See TracChangeset
for help on using the changeset viewer.