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