[e0ef1b1] | 1 | package com.wediscussmovies.project.web.controller;
|
---|
| 2 |
|
---|
| 3 |
|
---|
| 4 | import com.wediscussmovies.project.LoggedUser;
|
---|
[2efe93e] | 5 | import com.wediscussmovies.project.model.Movie;
|
---|
[e0ef1b1] | 6 | import com.wediscussmovies.project.model.exception.InvalidArgumentsException;
|
---|
| 7 | import com.wediscussmovies.project.model.exception.PasswordsDoNotMatchException;
|
---|
| 8 | import com.wediscussmovies.project.service.MovieService;
|
---|
| 9 | import com.wediscussmovies.project.service.UserService;
|
---|
[7f36551] | 10 | import com.wediscussmovies.project.web.DesignFrontMovies;
|
---|
[e0ef1b1] | 11 | import org.springframework.stereotype.Controller;
|
---|
| 12 | import org.springframework.ui.Model;
|
---|
| 13 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
| 14 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
| 15 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
| 16 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
| 17 |
|
---|
[2efe93e] | 18 | import java.util.ArrayList;
|
---|
| 19 | import java.util.List;
|
---|
| 20 |
|
---|
[e0ef1b1] | 21 | @Controller
|
---|
| 22 | @RequestMapping
|
---|
| 23 | public class UsersController{
|
---|
| 24 | private final UserService userService;
|
---|
| 25 | private final MovieService movieService;
|
---|
| 26 |
|
---|
| 27 | public UsersController(UserService userService, MovieService movieService) {
|
---|
| 28 | this.userService = userService;
|
---|
| 29 | this.movieService = movieService;
|
---|
| 30 | }
|
---|
| 31 | @PostMapping("/register")
|
---|
| 32 | public String register(@RequestParam String username,
|
---|
| 33 | @RequestParam String password,
|
---|
| 34 | @RequestParam String repeatedPassword,
|
---|
| 35 | @RequestParam String email,
|
---|
| 36 | @RequestParam String name,
|
---|
| 37 | @RequestParam String surname) {
|
---|
| 38 | try{
|
---|
| 39 | this.userService.register(email,username,password,repeatedPassword,name,surname);
|
---|
| 40 | return "redirect:/login";
|
---|
| 41 | } catch (InvalidArgumentsException | PasswordsDoNotMatchException exception) {
|
---|
| 42 | return "redirect:/register?error=" + exception.getMessage();
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | @GetMapping("/register")
|
---|
| 46 | public String getRegisterPage(@RequestParam(required = false) String error, Model model) {
|
---|
| 47 | addModelError(model,error);
|
---|
| 48 | model.addAttribute("contentTemplate","register");
|
---|
| 49 | return "template";
|
---|
| 50 | }
|
---|
| 51 | @GetMapping("/login")
|
---|
| 52 | public String getLoginPage(@RequestParam(required = false) String error,Model model){
|
---|
| 53 | addModelError(model,error);
|
---|
| 54 | model.addAttribute("contentTemplate","login");
|
---|
| 55 | return "template";
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | private void addModelError(Model model,String error){
|
---|
| 59 | if(error != null && !error.isEmpty()) {
|
---|
| 60 | model.addAttribute("hasError", true);
|
---|
| 61 | model.addAttribute("error", error);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | @GetMapping("/favoriteList")
|
---|
| 65 | public String getFavoriteList(Model model){
|
---|
[2efe93e] | 66 | List<Movie> movieList = this.movieService.findLikedMoviesByUser(LoggedUser.getLoggedUser());
|
---|
| 67 | List<List<Movie>> movie_rows = new ArrayList<>();
|
---|
[7f36551] | 68 | DesignFrontMovies.designMovieList(movieList,movie_rows);
|
---|
[2efe93e] | 69 | model.addAttribute("movie_rows", movie_rows);
|
---|
[e0ef1b1] | 70 | model.addAttribute("contentTemplate","favoriteList");
|
---|
| 71 | return "template";
|
---|
| 72 |
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | }
|
---|