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

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

home controller added mappings for add new film, event and projection

  • Property mode set to 100644
File size: 6.4 KB
Line 
1package com.example.moviezone.web;
2
3
4import com.example.moviezone.model.Customer;
5import com.example.moviezone.model.Film;
6import com.example.moviezone.model.User;
7import com.example.moviezone.model.exceptions.UserNotFoundException;
8import com.example.moviezone.service.*;
9import org.springframework.format.annotation.DateTimeFormat;
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;
28private final TicketService ticketService;
29
30 public HomeController(FilmService filmService, UserService userService, ProjectionService projectionService, EventService eventService, TicketService ticketService) {
31 this.filmService = filmService;
32 this.userService = userService;
33 this.projectionService = projectionService;
34 this.eventService = eventService;
35 this.ticketService = ticketService;
36 }
37
38 @GetMapping({"/","/home"})
39 public String getHomePage(Model model) {
40 model.addAttribute("bodyContent", "home");
41 return "master-template";
42 }
43
44 @GetMapping("/login")
45 public String getLoginPage(Model model)
46 {
47 model.addAttribute("bodyContent", "login");
48 return "master-template";
49 }
50
51 @GetMapping("/register")
52 public String getRegisterPage(Model model)
53 {
54 model.addAttribute("bodyContent", "register");
55 return "master-template";
56 }
57
58 @PostMapping("/login")
59 public String login(@RequestParam String username,@RequestParam String password,Model model, HttpSession session)
60 {
61 User user = null;
62 try {
63 user=userService.login(username,password);
64 session.setAttribute("sessionUser",user);
65 model.addAttribute("user",user);
66 return "redirect:/home";
67
68 }catch (UserNotFoundException e)
69 {
70 model.addAttribute("hasError", true);
71 model.addAttribute("error", e.getMessage());
72 return "login";
73 }
74
75 }
76
77 @PostMapping("register")
78 public String register(@RequestParam String username, @RequestParam String first_name, @RequestParam String last_name,
79 @RequestParam String password, @RequestParam String repeatedPassword,
80 @RequestParam String email, @RequestParam String number,
81 @RequestParam String role)
82 {
83 User user = null;
84 user=userService.register(first_name,last_name,username,email,number,password,repeatedPassword,role);
85 return "redirect:/login";
86 }
87
88 @GetMapping("/films")
89 public String getFilmsPage(Model model){
90 model.addAttribute("films",filmService.findAllFilms());
91 model.addAttribute("bodyContent","films");
92 return "master-template";
93 }
94
95 @GetMapping("/projections")
96 public String getProjectionsPage(Model model)
97 {
98 model.addAttribute("projections",projectionService.findAllAvailableProjections(LocalDate.now()));
99 model.addAttribute("bodyContent","projections");
100 return "master-template";
101 }
102 @GetMapping("/events")
103 public String getEventsPage(Model model)
104 {
105 model.addAttribute("events",eventService.findAllEvents());
106 model.addAttribute("bodyContent","events");
107 return "master-template";
108 }
109 @GetMapping("/myTickets")
110 public String getMyTicketsPage(Model model,HttpSession session)
111 {
112 model.addAttribute("tickets",ticketService.findAllByCustomer((Customer) session.getAttribute("user")));
113 model.addAttribute("bodyContent","myTickets");
114 return "master-template";
115 }
116 @GetMapping("/addProjection")
117 public String getAddProjectionPage(Model model)
118 {
119 model.addAttribute("films",filmService.findAllFilms());
120 model.addAttribute("bodyContent","addProjection");
121 return "master-template";
122 }
123 @GetMapping("/addEvent")
124 public String getAddEventPage(Model model)
125 {
126 model.addAttribute("bodyContent","addEvent");
127 return "master-template";
128 }
129 @GetMapping("/addFilm")
130 public String getAddFilmPage(Model model)
131 {
132 model.addAttribute("bodyContent","addFilm");
133 return "master-template";
134 }
135
136 @PostMapping("/addP")
137 public String saveProjection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_start,
138 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_end,
139 @RequestParam String type_of_technology,
140 @RequestParam Integer id_film)
141 {
142 projectionService.save(date_time_start,date_time_end,type_of_technology,id_film);
143 return "redirect:/home";
144 }
145 @PostMapping("/addE")
146 public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
147 @RequestParam String theme,
148 @RequestParam String duration,
149 @RequestParam String repeating)
150 {
151 eventService.save(start_date,theme,duration,repeating);
152 return "redirect:/home";
153 }
154 @PostMapping("/addF")
155 public String saveFilm(
156 @RequestParam String name,
157 @RequestParam Integer duration,
158 @RequestParam String actors,
159 @RequestParam String genre,
160 @RequestParam String age_category,
161 @RequestParam String url,
162 @RequestParam String director,
163 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
164 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end_date
165 )
166 {
167 filmService.save(name,duration,actors,genre,age_category,url,director,start_date,end_date);
168 return "redirect:/home";
169 }
170
171}
Note: See TracBrowser for help on using the repository browser.