[e0ef1b1] | 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 | }
|
---|