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.EventService;
|
---|
7 | import com.example.moviezone.service.FilmService;
|
---|
8 | import com.example.moviezone.service.ProjectionService;
|
---|
9 | import com.example.moviezone.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 javax.servlet.http.HttpSession;
|
---|
18 | import java.time.LocalDate;
|
---|
19 |
|
---|
20 | @Controller
|
---|
21 | @RequestMapping("/")
|
---|
22 | public class HomeController {
|
---|
23 |
|
---|
24 | private final FilmService filmService;
|
---|
25 | private final UserService userService;
|
---|
26 | private final ProjectionService projectionService;
|
---|
27 | private final EventService eventService;
|
---|
28 |
|
---|
29 | public HomeController(FilmService filmService, UserService userService, ProjectionService projectionService, EventService eventService) {
|
---|
30 | this.filmService = filmService;
|
---|
31 | this.userService = userService;
|
---|
32 | this.projectionService = projectionService;
|
---|
33 | this.eventService = eventService;
|
---|
34 | }
|
---|
35 |
|
---|
36 | @GetMapping
|
---|
37 | public String getHomePage(Model model) {
|
---|
38 | model.addAttribute("bodyContent", "home");
|
---|
39 | return "master-template";
|
---|
40 | }
|
---|
41 |
|
---|
42 | @GetMapping("/login")
|
---|
43 | public String getLoginPage(Model model)
|
---|
44 | {
|
---|
45 | model.addAttribute("bodyContent", "login");
|
---|
46 | return "master-template";
|
---|
47 | }
|
---|
48 |
|
---|
49 | @GetMapping("/register")
|
---|
50 | public String getRegisterPage(Model model)
|
---|
51 | {
|
---|
52 | model.addAttribute("bodyContent", "register");
|
---|
53 | return "master-template";
|
---|
54 | }
|
---|
55 |
|
---|
56 | @PostMapping("/login")
|
---|
57 | public String login(@RequestParam String username,@RequestParam String password,Model model, HttpSession session)
|
---|
58 | {
|
---|
59 | User user = null;
|
---|
60 | try {
|
---|
61 | user=userService.login(username,password);
|
---|
62 | session.setAttribute("sessionUser",user);
|
---|
63 | model.addAttribute("user",user);
|
---|
64 | return "redirect:/home";
|
---|
65 |
|
---|
66 | }catch (UserNotFoundException e)
|
---|
67 | {
|
---|
68 | model.addAttribute("hasError", true);
|
---|
69 | model.addAttribute("error", e.getMessage());
|
---|
70 | return "login";
|
---|
71 | }
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | @PostMapping("register")
|
---|
76 | public String register(@RequestParam String username, @RequestParam String first_name, @RequestParam String last_name,
|
---|
77 | @RequestParam String password, @RequestParam String repeatedPassword,
|
---|
78 | @RequestParam String email, @RequestParam String number,
|
---|
79 | @RequestParam String role)
|
---|
80 | {
|
---|
81 | User user = null;
|
---|
82 | user=userService.register(first_name,last_name,username,email,number,password,repeatedPassword,role);
|
---|
83 | return "redirect:/login";
|
---|
84 | }
|
---|
85 |
|
---|
86 | @GetMapping("/films")
|
---|
87 | public String getFilmsPage(Model model){
|
---|
88 | model.addAttribute("films",filmService.findAllFilms());
|
---|
89 | model.addAttribute("bodyContent","films");
|
---|
90 | return "master-template";
|
---|
91 | }
|
---|
92 |
|
---|
93 | @GetMapping("/projections")
|
---|
94 | public String getProjectionsPage(Model model)
|
---|
95 | {
|
---|
96 | model.addAttribute("projections",projectionService.findAllAvailableProjections(LocalDate.now()));
|
---|
97 | model.addAttribute("bodyContent","projections");
|
---|
98 | return "master-template";
|
---|
99 | }
|
---|
100 | @GetMapping("/events")
|
---|
101 | public String getEventsPage(Model model)
|
---|
102 | {
|
---|
103 | model.addAttribute("events",eventService.findAllEvents());
|
---|
104 | model.addAttribute("bodyContent","events");
|
---|
105 | return "master-template";
|
---|
106 | }
|
---|
107 |
|
---|
108 | }
|
---|