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

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

home controller added getMappings

  • Property mode set to 100644
File size: 3.6 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.EventService;
7import com.example.moviezone.service.FilmService;
8import com.example.moviezone.service.ProjectionService;
9import com.example.moviezone.service.UserService;
10import org.springframework.stereotype.Controller;
11import org.springframework.ui.Model;
12import org.springframework.web.bind.annotation.GetMapping;
13import org.springframework.web.bind.annotation.PostMapping;
14import org.springframework.web.bind.annotation.RequestMapping;
15import org.springframework.web.bind.annotation.RequestParam;
16
17import javax.servlet.http.HttpSession;
18import java.time.LocalDate;
19
20@Controller
21@RequestMapping("/")
22public class HomeController {
23
24private final FilmService filmService;
25private final UserService userService;
26private final ProjectionService projectionService;
27private 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}
Note: See TracBrowser for help on using the repository browser.