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