source: src/main/java/com/example/moviezone/web/HomeController.java@ e097c1c

Last change on this file since e097c1c was e097c1c, checked in by milamihajlovska <mila.mihajlovska01@…>, 22 months ago

login and register in HomeController

  • Property mode set to 100644
File size: 2.5 KB
Line 
1package com.example.moviezone.web;
2
3
4import com.example.moviezone.model.User;
5import com.example.moviezone.model.exceptions.UserNotFoundException;
6import com.example.moviezone.service.FilmService;
7import com.example.moviezone.service.UserService;
8import org.springframework.stereotype.Controller;
9import org.springframework.ui.Model;
10import org.springframework.web.bind.annotation.GetMapping;
11import org.springframework.web.bind.annotation.PostMapping;
12import org.springframework.web.bind.annotation.RequestMapping;
13import org.springframework.web.bind.annotation.RequestParam;
14
15import javax.servlet.http.HttpSession;
16
17@Controller
18@RequestMapping("/")
19public class HomeController {
20
21private final FilmService filmService;
22private 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}
Note: See TracBrowser for help on using the repository browser.