Changeset e0ef1b1 in Git for src/main/java/com/wediscussmovies/project/web/controller/UsersController.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/UsersController.java
r3ded84d re0ef1b1 1 //package com.wediscussmovies.project.web.controller; 2 // 3 //import com.wediscussmovies.project.model.PasswordEncoder; 4 //import com.wediscussmovies.project.model.*; 5 //import com.wediscussmovies.project.model.exception.InvalidUserCredentialsException; 6 //import com.wediscussmovies.project.service.UserService; 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.RequestParam; 12 // 13 //import javax.servlet.http.HttpServletRequest; 14 //import java.security.NoSuchAlgorithmException; 15 //import java.util.Optional; 16 // 17 //@Controller 18 //public class UsersController{ 19 // private final UserService userService; 20 // 21 // public UsersController(UserService userService) { 22 // this.userService = userService; 23 // } 24 // 25 // @GetMapping("/login") 26 // public String getLoginPage(Model model){ 27 // return "login"; 28 // } 29 // 30 // @PostMapping("/login/confirm") 31 // public String confirmLogin(HttpServletRequest request, Model model, 32 // @RequestParam String username, 33 // @RequestParam String password){ 34 // Optional<User> user; 35 // try{ 36 // password = PasswordEncoder.getEncodedPasswordString(password); 37 // } 38 // catch (NoSuchAlgorithmException ex){ 39 // model.addAttribute("hasError", true); 40 // model.addAttribute("error", ex.getMessage()); 41 // return "login"; 42 // } 43 // 44 // try{ 45 // user = this.userService.login(username, password); 46 // request.getSession().setAttribute("user", user); 47 // request.getSession().setAttribute("loggedIn",true); 48 // return "redirect:/movies"; 49 // } 50 // catch (InvalidUserCredentialsException ex){ 51 // model.addAttribute("hasError", true); 52 // model.addAttribute("error", ex.getMessage()); 53 // return "login"; 54 // } 55 // } 56 // 57 // @GetMapping("/register") 58 // public String getRegisterPage(){ 59 // return "register"; 60 // } 61 // 62 // @PostMapping("/login/confirm") 63 // public String confirmRegister(HttpServletRequest request, 64 // @RequestParam String username, 65 // @RequestParam String email, 66 // @RequestParam String password, 67 // @RequestParam String confirmPassword, 68 // @RequestParam String name, 69 // @RequestParam String surname){ 70 // Optional<User> user; 71 // 72 // try{ 73 // password = PasswordEncoder.getEncodedPasswordString(password); 74 // confirmPassword = PasswordEncoder.getEncodedPasswordString(confirmPassword); 75 // } 76 // catch (NoSuchAlgorithmException ex){ 77 // request.getSession().setAttribute("error", "Contact the administrators!"); 78 // request.getSession().setAttribute("hasError", "true"); 79 // return "redirect:/movies"; 80 // } 81 // 82 // user = this.userService.register(request, email, password, confirmPassword, username, name, surname); 83 // if(user.isEmpty()){ 84 // request.setAttribute("hasError", "true"); 85 // }else{ 86 // request.getSession().setAttribute("hasError", "false"); 87 // request.getSession().setAttribute("user", user.get()); 88 // request.getSession().setAttribute("loggedIn",true); 89 // } 90 // return "redirect:/movies"; 91 // } 92 // 93 // @GetMapping 94 // public String logout(HttpServletRequest request){ 95 // request.getSession().invalidate(); 96 // return "redirect:/login"; 97 // } 98 //} 1 package com.wediscussmovies.project.web.controller; 2 3 4 import com.wediscussmovies.project.LoggedUser; 5 import com.wediscussmovies.project.model.exception.InvalidArgumentsException; 6 import com.wediscussmovies.project.model.exception.PasswordsDoNotMatchException; 7 import com.wediscussmovies.project.service.MovieService; 8 import com.wediscussmovies.project.service.UserService; 9 import org.springframework.stereotype.Controller; 10 import org.springframework.ui.Model; 11 import org.springframework.web.bind.annotation.GetMapping; 12 import org.springframework.web.bind.annotation.PostMapping; 13 import org.springframework.web.bind.annotation.RequestMapping; 14 import org.springframework.web.bind.annotation.RequestParam; 15 16 @Controller 17 @RequestMapping 18 public class UsersController{ 19 private final UserService userService; 20 private final MovieService movieService; 21 22 public UsersController(UserService userService, MovieService movieService) { 23 this.userService = userService; 24 this.movieService = movieService; 25 } 26 @PostMapping("/register") 27 public String register(@RequestParam String username, 28 @RequestParam String password, 29 @RequestParam String repeatedPassword, 30 @RequestParam String email, 31 @RequestParam String name, 32 @RequestParam String surname) { 33 try{ 34 this.userService.register(email,username,password,repeatedPassword,name,surname); 35 return "redirect:/login"; 36 } catch (InvalidArgumentsException | PasswordsDoNotMatchException exception) { 37 return "redirect:/register?error=" + exception.getMessage(); 38 } 39 } 40 @GetMapping("/register") 41 public String getRegisterPage(@RequestParam(required = false) String error, Model model) { 42 addModelError(model,error); 43 model.addAttribute("contentTemplate","register"); 44 return "template"; 45 } 46 @GetMapping("/login") 47 public String getLoginPage(@RequestParam(required = false) String error,Model model){ 48 addModelError(model,error); 49 model.addAttribute("contentTemplate","login"); 50 return "template"; 51 } 52 53 private void addModelError(Model model,String error){ 54 if(error != null && !error.isEmpty()) { 55 model.addAttribute("hasError", true); 56 model.addAttribute("error", error); 57 } 58 } 59 @GetMapping("/favoriteList") 60 public String getFavoriteList(Model model){ 61 model.addAttribute("movies",this.movieService.findLikedMoviesByUser(LoggedUser.getLoggedUser())); 62 model.addAttribute("contentTemplate","favoriteList"); 63 return "template"; 64 65 } 66 67 68 }
Note:
See TracChangeset
for help on using the changeset viewer.