1 | package com.example.moviezone.web;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.moviezone.model.Customer;
|
---|
5 | import com.example.moviezone.model.Film;
|
---|
6 | import com.example.moviezone.model.User;
|
---|
7 | import com.example.moviezone.model.exceptions.UserNotFoundException;
|
---|
8 | import com.example.moviezone.service.*;
|
---|
9 | import org.springframework.format.annotation.DateTimeFormat;
|
---|
10 | import org.springframework.stereotype.Controller;
|
---|
11 | import org.springframework.ui.Model;
|
---|
12 | import org.springframework.web.bind.annotation.*;
|
---|
13 |
|
---|
14 | import javax.servlet.http.HttpSession;
|
---|
15 | import java.time.LocalDate;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Optional;
|
---|
18 | import java.util.stream.Collectors;
|
---|
19 |
|
---|
20 | @Controller
|
---|
21 | @RequestMapping({"/","/home"})
|
---|
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 | private final TicketService ticketService;
|
---|
29 | private final WorkerService workerService;
|
---|
30 |
|
---|
31 | public HomeController(FilmService filmService, UserService userService, ProjectionService projectionService, EventService eventService, TicketService ticketService, WorkerService workerService) {
|
---|
32 | this.filmService = filmService;
|
---|
33 | this.userService = userService;
|
---|
34 | this.projectionService = projectionService;
|
---|
35 | this.eventService = eventService;
|
---|
36 | this.ticketService = ticketService;
|
---|
37 | this.workerService = workerService;
|
---|
38 | }
|
---|
39 |
|
---|
40 | @GetMapping
|
---|
41 | public String getHomePage(Model model) {
|
---|
42 | List<Film> films=filmService.findAllFilms();
|
---|
43 | films=films.stream().limit(5).collect(Collectors.toList());
|
---|
44 | model.addAttribute("films", films);
|
---|
45 | model.addAttribute("bodyContent", "home");
|
---|
46 |
|
---|
47 | return "master-template";
|
---|
48 | }
|
---|
49 | @GetMapping("/getFilm/{id}")
|
---|
50 | public String getFilm(@PathVariable Long id, Model model) {
|
---|
51 | Optional<Film> film=filmService.getFilmById(id);
|
---|
52 | model.addAttribute("film", film);
|
---|
53 | model.addAttribute("bodyContent", "home");
|
---|
54 |
|
---|
55 | return "master-template";
|
---|
56 | }
|
---|
57 |
|
---|
58 | @GetMapping("/login")
|
---|
59 | public String getLoginPage(Model model)
|
---|
60 | {
|
---|
61 | model.addAttribute("bodyContent", "login");
|
---|
62 | return "master-template";
|
---|
63 | }
|
---|
64 |
|
---|
65 | @GetMapping("/register")
|
---|
66 | public String getRegisterPage(Model model)
|
---|
67 | {
|
---|
68 | model.addAttribute("bodyContent", "register");
|
---|
69 | return "master-template";
|
---|
70 | }
|
---|
71 |
|
---|
72 | @PostMapping("/login")
|
---|
73 | public String login(@RequestParam String username,@RequestParam String password,Model model, HttpSession session)
|
---|
74 | {
|
---|
75 | User user = null;
|
---|
76 | try {
|
---|
77 | user=userService.login(username,password);
|
---|
78 | session.setAttribute("sessionUser",user);
|
---|
79 | model.addAttribute("user",user);
|
---|
80 | return "redirect:/home";
|
---|
81 |
|
---|
82 | }catch (UserNotFoundException e)
|
---|
83 | {
|
---|
84 | model.addAttribute("hasError", true);
|
---|
85 | model.addAttribute("error", e.getMessage());
|
---|
86 | return "login";
|
---|
87 | }
|
---|
88 |
|
---|
89 | }
|
---|
90 |
|
---|
91 | @PostMapping("register")
|
---|
92 | public String register(@RequestParam String username, @RequestParam String first_name, @RequestParam String last_name,
|
---|
93 | @RequestParam String password, @RequestParam String repeatedPassword,
|
---|
94 | @RequestParam String email, @RequestParam String number,
|
---|
95 | @RequestParam String role)
|
---|
96 | {
|
---|
97 | User user = null;
|
---|
98 | user=userService.register(first_name,last_name,username,email,number,password,repeatedPassword,role);
|
---|
99 | return "redirect:/login";
|
---|
100 | }
|
---|
101 |
|
---|
102 | @GetMapping("/films")
|
---|
103 | public String getFilmsPage(Model model){
|
---|
104 | model.addAttribute("films",filmService.findAllFilms());
|
---|
105 | model.addAttribute("bodyContent","films");
|
---|
106 | return "master-template";
|
---|
107 | }
|
---|
108 |
|
---|
109 | @GetMapping("/projections")
|
---|
110 | public String getProjectionsPage(Model model)
|
---|
111 | {
|
---|
112 | model.addAttribute("projections",projectionService.findAllProjections());
|
---|
113 | model.addAttribute("bodyContent","projections");
|
---|
114 | return "master-template";
|
---|
115 | }
|
---|
116 | @GetMapping("/events")
|
---|
117 | public String getEventsPage(Model model)
|
---|
118 | {
|
---|
119 | model.addAttribute("events",eventService.findAllEvents());
|
---|
120 | model.addAttribute("bodyContent","events");
|
---|
121 | return "master-template";
|
---|
122 | }
|
---|
123 | @GetMapping("/myTickets")
|
---|
124 | public String getMyTicketsPage(Model model,HttpSession session)
|
---|
125 | {
|
---|
126 | model.addAttribute("tickets",ticketService.findAllByCustomer((Customer) session.getAttribute("user")));
|
---|
127 | model.addAttribute("bodyContent","myTickets");
|
---|
128 | return "master-template";
|
---|
129 | }
|
---|
130 | @GetMapping("/addProjection")
|
---|
131 | public String getAddProjectionPage(Model model)
|
---|
132 | {
|
---|
133 | model.addAttribute("films",filmService.findAllFilms());
|
---|
134 | model.addAttribute("bodyContent","addProjection");
|
---|
135 | return "master-template";
|
---|
136 | }
|
---|
137 | @GetMapping("/addEvent")
|
---|
138 | public String getAddEventPage(Model model)
|
---|
139 | {
|
---|
140 | model.addAttribute("bodyContent","addEvent");
|
---|
141 | return "master-template";
|
---|
142 | }
|
---|
143 | @GetMapping("/addFilm")
|
---|
144 | public String getAddFilmPage(Model model)
|
---|
145 | {
|
---|
146 | model.addAttribute("bodyContent","addFilm");
|
---|
147 | return "master-template";
|
---|
148 | }
|
---|
149 |
|
---|
150 | @PostMapping("/addP")
|
---|
151 | public String saveProjection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_start,
|
---|
152 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_end,
|
---|
153 | @RequestParam String type_of_technology,
|
---|
154 | @RequestParam Integer id_film)
|
---|
155 | {
|
---|
156 | projectionService.save(date_time_start,date_time_end,type_of_technology,id_film);
|
---|
157 | return "redirect:/home";
|
---|
158 | }
|
---|
159 | @PostMapping("/addE")
|
---|
160 | public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
|
---|
161 | @RequestParam String theme,
|
---|
162 | @RequestParam String duration,
|
---|
163 | @RequestParam String repeating)
|
---|
164 | {
|
---|
165 | eventService.save(start_date,theme,duration,repeating);
|
---|
166 | return "redirect:/home";
|
---|
167 | }
|
---|
168 | @PostMapping("/addF")
|
---|
169 | public String saveFilm(
|
---|
170 | @RequestParam String name,
|
---|
171 | @RequestParam Integer duration,
|
---|
172 | @RequestParam String actors,
|
---|
173 | @RequestParam String genre,
|
---|
174 | @RequestParam String age_category,
|
---|
175 | @RequestParam String url,
|
---|
176 | @RequestParam String director,
|
---|
177 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
|
---|
178 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end_date
|
---|
179 | )
|
---|
180 | {
|
---|
181 | filmService.save(name,duration,actors,genre,age_category,url,director,start_date,end_date);
|
---|
182 | return "redirect:/home";
|
---|
183 | }
|
---|
184 |
|
---|
185 | @GetMapping("/workers")
|
---|
186 | public String getWorkersPage(Model model)
|
---|
187 | {
|
---|
188 | model.addAttribute("workers",workerService.findAllWorkers());
|
---|
189 | model.addAttribute("bodyContent", "workers");
|
---|
190 | return "master-template";
|
---|
191 | }
|
---|
192 | }
|
---|