1 | package com.example.moviezone.web;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.moviezone.model.User;
|
---|
5 | import com.example.moviezone.model.exceptions.UserNotFoundException;
|
---|
6 | import com.example.moviezone.service.FilmService;
|
---|
7 | import com.example.moviezone.service.UserService;
|
---|
8 | import org.springframework.stereotype.Controller;
|
---|
9 | import org.springframework.ui.Model;
|
---|
10 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
11 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
12 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
13 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
14 |
|
---|
15 | import javax.servlet.http.HttpSession;
|
---|
16 |
|
---|
17 | @Controller
|
---|
18 | @RequestMapping("/")
|
---|
19 | public class HomeController {
|
---|
20 |
|
---|
21 | private final FilmService filmService;
|
---|
22 | private final UserService userService;
|
---|
23 |
|
---|
24 | public HomeController(FilmService filmService, UserService userService) {
|
---|
25 | this.filmService = filmService;
|
---|
26 | this.userService = userService;
|
---|
27 | }
|
---|
28 |
|
---|
29 | @GetMapping
|
---|
30 | public String getHomePage(Model model) {
|
---|
31 | model.addAttribute("bodyContent", "home");
|
---|
32 | return "master-template";
|
---|
33 | }
|
---|
34 |
|
---|
35 | @GetMapping("/login")
|
---|
36 | public String getLoginPage(Model model)
|
---|
37 | {
|
---|
38 | model.addAttribute("bodyContent", "login");
|
---|
39 | return "master-template";
|
---|
40 | }
|
---|
41 |
|
---|
42 | @GetMapping("/register")
|
---|
43 | public String getRegisterPage(Model model)
|
---|
44 | {
|
---|
45 | model.addAttribute("bodyContent", "register");
|
---|
46 | return "master-template";
|
---|
47 | }
|
---|
48 |
|
---|
49 | @PostMapping("/login")
|
---|
50 | public String login(@RequestParam String username,@RequestParam String password,Model model, HttpSession session)
|
---|
51 | {
|
---|
52 | User user = null;
|
---|
53 | try {
|
---|
54 | user=userService.login(username,password);
|
---|
55 | session.setAttribute("sessionUser",user);
|
---|
56 | model.addAttribute("user",user);
|
---|
57 | return "redirect:/home";
|
---|
58 |
|
---|
59 | }catch (UserNotFoundException e)
|
---|
60 | {
|
---|
61 | model.addAttribute("hasError", true);
|
---|
62 | model.addAttribute("error", e.getMessage());
|
---|
63 | return "login";
|
---|
64 | }
|
---|
65 |
|
---|
66 | }
|
---|
67 |
|
---|
68 | @PostMapping("register")
|
---|
69 | public String register(@RequestParam String username, @RequestParam String first_name, @RequestParam String last_name,
|
---|
70 | @RequestParam String password, @RequestParam String repeatedPassword,
|
---|
71 | @RequestParam String email, @RequestParam String number,
|
---|
72 | @RequestParam String role)
|
---|
73 | {
|
---|
74 | User user = null;
|
---|
75 | user=userService.register(first_name,last_name,username,email,number,password,repeatedPassword,role);
|
---|
76 | return "redirect:/login";
|
---|
77 | }
|
---|
78 |
|
---|
79 | }
|
---|