shniaga

userController

Jun 12th, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. package judgev2.web;
  2.  
  3. import judgev2.model.binding.UserAddBindingModel;
  4. import judgev2.model.binding.UserLoginBindingModel;
  5. import judgev2.model.service.UserServiceModel;
  6. import judgev2.service.UserService;
  7. import org.modelmapper.ModelMapper;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.validation.BindingResult;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.ModelAttribute;
  12. import org.springframework.web.bind.annotation.PostMapping;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.servlet.ModelAndView;
  15. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  16.  
  17. import javax.servlet.http.HttpSession;
  18. import javax.validation.Valid;
  19.  
  20. @Controller
  21. @RequestMapping("/users")
  22. public class UserController {
  23.  
  24. private final UserService userService;
  25. private final ModelMapper modelMapper;
  26.  
  27. public UserController(UserService userService, ModelMapper modelMapper) {
  28. this.userService = userService;
  29. this.modelMapper = modelMapper;
  30. }
  31.  
  32. @GetMapping("/login")
  33. public ModelAndView login(@Valid @ModelAttribute("userLoginBindingModel")UserLoginBindingModel userLoginBindingModel,
  34. BindingResult bindingResult, ModelAndView modelAndView) {
  35. modelAndView.addObject("userLoginBindingModel", userLoginBindingModel);
  36. modelAndView.setViewName("login");
  37. return modelAndView;
  38. }
  39.  
  40. @PostMapping("/login")
  41. public ModelAndView loginConfirm(@Valid @ModelAttribute("userLoginBindingModel")UserLoginBindingModel userLoginBindingModel,
  42. BindingResult bindingResult, ModelAndView modelAndView, HttpSession httpSession,
  43. RedirectAttributes redirectAttributes) {
  44. if (bindingResult.hasErrors()) {
  45. redirectAttributes.addAttribute("userLoginBindingModel", userLoginBindingModel);
  46. modelAndView.setViewName("redirect:/users/login");
  47. } else {
  48. UserServiceModel userServiceModel = this.userService.findByUsername(userLoginBindingModel.getUsername());
  49.  
  50. if(userServiceModel == null || !userServiceModel.getPassword().equals(userLoginBindingModel.getPassword())) {
  51. redirectAttributes.addFlashAttribute("notFound", true);
  52. redirectAttributes.addAttribute("userLoginBindingModel", userLoginBindingModel);
  53. modelAndView.setViewName("redirect:/users/login");
  54. } else {
  55. httpSession.setAttribute("user", userServiceModel);
  56. httpSession.setAttribute("id", userServiceModel.getId());
  57. httpSession.setAttribute("role", userServiceModel.getRole().getName());
  58. modelAndView.setViewName("redirect:/");
  59. }
  60.  
  61. }
  62.  
  63. return modelAndView;
  64. }
  65.  
  66. @GetMapping("/register")
  67. public String register() {
  68. return "register";
  69. }
  70.  
  71. @PostMapping("/register")
  72. public ModelAndView registerConfirm(@Valid @ModelAttribute("userAddBindingModel") UserAddBindingModel userAddBindingModel,
  73. BindingResult bindingResult, ModelAndView modelAndView) {
  74.  
  75. if (bindingResult.hasErrors()) {
  76. modelAndView.setViewName("redirect:/users/register");
  77. } else {
  78. UserServiceModel userServiceModel =
  79. this.userService.registerUser(this.modelMapper.map(userAddBindingModel, UserServiceModel.class));
  80.  
  81. modelAndView.setViewName("redirect:/users/login");
  82. }
  83.  
  84. return modelAndView;
  85.  
  86. }
  87.  
  88.  
  89. }
Add Comment
Please, Sign In to add comment