1 | package com.example.moviezone.web;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.moviezone.model.*;
|
---|
5 | import com.example.moviezone.model.exceptions.PasswordsDoNotMatchException;
|
---|
6 |
|
---|
7 | import com.example.moviezone.model.manytomany.ProjectionIsPlayedInRoom;
|
---|
8 |
|
---|
9 | import com.example.moviezone.model.procedures.FilmsReturnTable;
|
---|
10 |
|
---|
11 | import com.example.moviezone.service.*;
|
---|
12 | import org.springframework.format.annotation.DateTimeFormat;
|
---|
13 | import org.springframework.stereotype.Controller;
|
---|
14 | import org.springframework.ui.Model;
|
---|
15 | import org.springframework.web.bind.annotation.*;
|
---|
16 |
|
---|
17 | import javax.servlet.http.HttpSession;
|
---|
18 | import javax.transaction.Transactional;
|
---|
19 | import java.time.LocalDate;
|
---|
20 | import java.util.LinkedList;
|
---|
21 | import java.util.List;
|
---|
22 | import java.util.stream.Collectors;
|
---|
23 |
|
---|
24 | @Controller
|
---|
25 | @RequestMapping({"/","/home"})
|
---|
26 | public class HomeController {
|
---|
27 |
|
---|
28 | private final FilmService filmService;
|
---|
29 | private final UserService userService;
|
---|
30 | private final ProjectionService projectionService;
|
---|
31 | private final EventService eventService;
|
---|
32 | private final TicketService ticketService;
|
---|
33 | private final WorkerService workerService;
|
---|
34 | private final CustomerRatesFilmService customerRatesFilmService;
|
---|
35 | private final CinemaService cinemaService;
|
---|
36 | private final CinemaOrganizesEventService cinemaOrganizesEventService;
|
---|
37 | private final CinemaPlaysFilmService cinemaPlaysFilmService;
|
---|
38 | private final ProjectionIsPlayedInRoomService projectionIsPlayedInRoomService;
|
---|
39 | private final CategoryService categoryService;
|
---|
40 |
|
---|
41 | public HomeController(FilmService filmService, UserService userService, ProjectionService projectionService, EventService eventService, TicketService ticketService, WorkerService workerService, CustomerRatesFilmService customerRatesFilmService, CinemaService cinemaService, CinemaOrganizesEventService cinemaOrganizesEventService, CinemaPlaysFilmService cinemaPlaysFilmService, ProjectionIsPlayedInRoomService projectionIsPlayedInRoomService, CategoryService categoryService)
|
---|
42 | {
|
---|
43 |
|
---|
44 | this.filmService = filmService;
|
---|
45 | this.userService = userService;
|
---|
46 | this.projectionService = projectionService;
|
---|
47 | this.eventService = eventService;
|
---|
48 | this.ticketService = ticketService;
|
---|
49 | this.workerService = workerService;
|
---|
50 | this.customerRatesFilmService = customerRatesFilmService;
|
---|
51 | this.cinemaService = cinemaService;
|
---|
52 | this.cinemaOrganizesEventService = cinemaOrganizesEventService;
|
---|
53 | this.cinemaPlaysFilmService = cinemaPlaysFilmService;
|
---|
54 | this.projectionIsPlayedInRoomService = projectionIsPlayedInRoomService;
|
---|
55 | this.categoryService = categoryService;
|
---|
56 | }
|
---|
57 |
|
---|
58 | @GetMapping
|
---|
59 | public String getHomePage(Model model) {
|
---|
60 | List<Film> films=filmService.findAllFilms();
|
---|
61 | films=films.stream().limit(5).collect(Collectors.toList());
|
---|
62 | List <Event> events=eventService.findAllEvents().stream().limit(5).collect(Collectors.toList());
|
---|
63 | model.addAttribute("films", films);
|
---|
64 | model.addAttribute("events",events);
|
---|
65 | model.addAttribute("bodyContent", "home");
|
---|
66 |
|
---|
67 | return "master-template";
|
---|
68 | }
|
---|
69 | @GetMapping("/getFilm/{id}")
|
---|
70 | public String getFilm(@PathVariable Long id, Model model) {
|
---|
71 | Film film=filmService.getFilmById(id).get();
|
---|
72 | model.addAttribute("film", film);
|
---|
73 | List<String> genres= List.of(film.getGenre().split(","));
|
---|
74 | double r=customerRatesFilmService.avg_rating(film.getId_film());
|
---|
75 | model.addAttribute("rating",r);
|
---|
76 | model.addAttribute("genres", genres);
|
---|
77 | model.addAttribute("bodyContent", "film");
|
---|
78 |
|
---|
79 | return "master-template";
|
---|
80 | }
|
---|
81 | @GetMapping("/getEvent/{id}")
|
---|
82 | public String getEvent(@PathVariable Long id, Model model) {
|
---|
83 | Event event =eventService.getEventById(id).get();
|
---|
84 | model.addAttribute("event", event);
|
---|
85 | model.addAttribute("bodyContent", "event");
|
---|
86 |
|
---|
87 | return "master-template";
|
---|
88 | }
|
---|
89 | @GetMapping("/getProjections/{id}")
|
---|
90 | @Transactional
|
---|
91 | public String getProjectionsFromFilm(@PathVariable Long id, Model model) {
|
---|
92 | Film film=filmService.getFilmById(id).get();
|
---|
93 | model.addAttribute("film",film);
|
---|
94 | model.addAttribute("projections",projectionService.getProjectionsForFilms(id.intValue()));
|
---|
95 | model.addAttribute("categories",categoryService.findAllCategories());
|
---|
96 | model.addAttribute("bodyContent", "projectionsForFilm");
|
---|
97 |
|
---|
98 | return "master-template";
|
---|
99 | }
|
---|
100 | @GetMapping("/login")
|
---|
101 | public String getLoginPage(Model model)
|
---|
102 | {
|
---|
103 | model.addAttribute("bodyContent", "login");
|
---|
104 | return "master-template";
|
---|
105 | }
|
---|
106 |
|
---|
107 | @GetMapping("/register")
|
---|
108 | public String getRegisterPage(Model model)
|
---|
109 | {
|
---|
110 | model.addAttribute("bodyContent", "register");
|
---|
111 | return "master-template";
|
---|
112 | }
|
---|
113 |
|
---|
114 | @PostMapping("/login")
|
---|
115 | public String login(@RequestParam String username,
|
---|
116 | @RequestParam String password,Model model, HttpSession session)
|
---|
117 | {
|
---|
118 | // User user = null;
|
---|
119 | // try {
|
---|
120 | User user=userService.login(username,password);
|
---|
121 | System.out.println(user.getFirst_name());
|
---|
122 | // session.setAttribute("sessionUser",user);
|
---|
123 | // model.addAttribute("user",user);
|
---|
124 | return "redirect:/home";
|
---|
125 | //
|
---|
126 | // }catch (UserNotFoundException e)
|
---|
127 | // {
|
---|
128 | // model.addAttribute("hasError", true);
|
---|
129 | // model.addAttribute("error", e.getMessage());
|
---|
130 | // return "login";
|
---|
131 | // }
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | @PostMapping()
|
---|
136 | public String register(@RequestParam String username,
|
---|
137 | @RequestParam String first_name,
|
---|
138 | @RequestParam String last_name,
|
---|
139 | @RequestParam String password,
|
---|
140 | @RequestParam String repeatedPassword,
|
---|
141 | @RequestParam String email,
|
---|
142 | @RequestParam String number,
|
---|
143 | @RequestParam Role role)
|
---|
144 | {
|
---|
145 | try {
|
---|
146 | userService.register(first_name,last_name,username,email,number,password,role);
|
---|
147 | return "redirect:/login";
|
---|
148 | }catch (PasswordsDoNotMatchException exception)
|
---|
149 | {
|
---|
150 | return "redirect:/register?error=" + exception.getMessage();
|
---|
151 | }
|
---|
152 |
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | @GetMapping("/films")
|
---|
157 | @Transactional
|
---|
158 | public String getFilmsPage1(Model model,@RequestParam(required = false) Integer id_cinema){
|
---|
159 | model.addAttribute("cinemas",cinemaService.findAllCinemas());
|
---|
160 | if (id_cinema!=null) {
|
---|
161 | model.addAttribute("films",filmService.getFilmsFromCinema(id_cinema));
|
---|
162 | }else{
|
---|
163 | List<FilmsReturnTable> pom=new LinkedList<>();
|
---|
164 | model.addAttribute("films",filmService.findAllFilms());
|
---|
165 | }
|
---|
166 | model.addAttribute("bodyContent","films");
|
---|
167 | return "master-template";
|
---|
168 | }
|
---|
169 | @Transactional
|
---|
170 | @GetMapping("/projections")
|
---|
171 | public String getProjectionsPage(Model model,@RequestParam(required = false) Integer id_cinema)
|
---|
172 | {
|
---|
173 | model.addAttribute("cinemas",cinemaService.findAllCinemas());
|
---|
174 | if (id_cinema!=null) {
|
---|
175 | model.addAttribute("films",filmService.getFilmsFromCinemaNow(id_cinema));
|
---|
176 | }else{
|
---|
177 | List<FilmsReturnTable> pom=new LinkedList<>();
|
---|
178 | model.addAttribute("films",filmService.getFilmsNow());
|
---|
179 | }
|
---|
180 | model.addAttribute("bodyContent","projections");
|
---|
181 | return "master-template";
|
---|
182 | }
|
---|
183 | @GetMapping("/events")
|
---|
184 | @Transactional
|
---|
185 | public String getEventsPage(Model model,@RequestParam(required = false) Integer id_cinema)
|
---|
186 | {
|
---|
187 | model.addAttribute("cinemas",cinemaService.findAllCinemas());
|
---|
188 | if (id_cinema!=null) {
|
---|
189 | model.addAttribute("events",eventService.getEventsFromCinema(id_cinema));
|
---|
190 | }else{
|
---|
191 | List<FilmsReturnTable> pom=new LinkedList<>();
|
---|
192 | model.addAttribute("events",eventService.getEventsNow());
|
---|
193 | }
|
---|
194 | model.addAttribute("bodyContent","events");
|
---|
195 | return "master-template";
|
---|
196 | }
|
---|
197 | @GetMapping("/myTickets")
|
---|
198 | public String getMyTicketsPage(Model model,HttpSession session)
|
---|
199 | {
|
---|
200 | model.addAttribute("tickets",ticketService.findAllByCustomer((Customer) session.getAttribute("user")));
|
---|
201 | model.addAttribute("bodyContent","myTickets");
|
---|
202 | return "master-template";
|
---|
203 | }
|
---|
204 | @GetMapping("/addProjection")
|
---|
205 | public String getAddProjectionPage(Model model)
|
---|
206 | {
|
---|
207 | model.addAttribute("films",filmService.findAllFilms());
|
---|
208 | model.addAttribute("bodyContent","addProjection");
|
---|
209 | return "master-template";
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | @GetMapping("/addEvent")
|
---|
214 | public String getAddEventPage(Model model)
|
---|
215 | {
|
---|
216 | model.addAttribute("bodyContent","addEvent");
|
---|
217 | return "master-template";
|
---|
218 | }
|
---|
219 | @GetMapping("/addFilm")
|
---|
220 | public String getAddFilmPage(Model model)
|
---|
221 | {
|
---|
222 | model.addAttribute("bodyContent","addFilm");
|
---|
223 | return "master-template";
|
---|
224 | }
|
---|
225 |
|
---|
226 | @PostMapping("/addP")
|
---|
227 | public String saveProjection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_start,
|
---|
228 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_end,
|
---|
229 | @RequestParam String type_of_technology,
|
---|
230 | @RequestParam Integer id_film)
|
---|
231 | {
|
---|
232 | projectionService.save(date_time_start,date_time_end,type_of_technology,id_film);
|
---|
233 | return "redirect:/home";
|
---|
234 | }
|
---|
235 | @PostMapping("/addE")
|
---|
236 | public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
|
---|
237 | @RequestParam String theme,
|
---|
238 | @RequestParam String duration,
|
---|
239 | @RequestParam String img_url,
|
---|
240 | @RequestParam String repeating)
|
---|
241 | {
|
---|
242 | eventService.save(start_date,theme,duration,repeating,img_url);
|
---|
243 | return "redirect:/home";
|
---|
244 | }
|
---|
245 | @PostMapping("/addF")
|
---|
246 | public String saveFilm(
|
---|
247 | @RequestParam String name,
|
---|
248 | @RequestParam Integer duration,
|
---|
249 | @RequestParam String actors,
|
---|
250 | @RequestParam String genre,
|
---|
251 | @RequestParam String age_category,
|
---|
252 | @RequestParam String url,
|
---|
253 | @RequestParam String director,
|
---|
254 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
|
---|
255 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end_date
|
---|
256 | )
|
---|
257 | {
|
---|
258 | filmService.save(name,duration,actors,genre,age_category,url,director,start_date,end_date);
|
---|
259 | return "redirect:/home";
|
---|
260 | }
|
---|
261 |
|
---|
262 | @GetMapping("/workers")
|
---|
263 | public String getWorkersPage(Model model)
|
---|
264 | {
|
---|
265 | model.addAttribute("workers",workerService.findAllWorkers());
|
---|
266 | model.addAttribute("bodyContent", "workers");
|
---|
267 | return "master-template";
|
---|
268 | }
|
---|
269 |
|
---|
270 | @GetMapping("/addEventToCinema")
|
---|
271 | public String getCinemaOrganizesEventPage(Model model)
|
---|
272 | {
|
---|
273 | model.addAttribute("cinemas",cinemaService.findAllCinemas());
|
---|
274 | model.addAttribute("events",eventService.findAllEvents());
|
---|
275 | model.addAttribute("bodyContent","addEventToCinema");
|
---|
276 | return "master-template";
|
---|
277 | }
|
---|
278 | @PostMapping("/addCinemaOrganizesEvent")
|
---|
279 | public String saveCinemaOrganizesEvent(@RequestParam Integer id_cinema,
|
---|
280 | @RequestParam Integer id_event)
|
---|
281 | {
|
---|
282 |
|
---|
283 | cinemaOrganizesEventService.save(id_cinema,id_event);
|
---|
284 | return "redirect:/home";
|
---|
285 | }
|
---|
286 | @GetMapping("/addFilmToCinema")
|
---|
287 | public String getCinemaPlaysFilmPage(Model model)
|
---|
288 | {
|
---|
289 | model.addAttribute("cinemas",cinemaService.findAllCinemas());
|
---|
290 | model.addAttribute("films",filmService.findAllFilms());
|
---|
291 | model.addAttribute("bodyContent","addFilmToCinema");
|
---|
292 | return "master-template";
|
---|
293 | }
|
---|
294 | @PostMapping("/addCinemaPlaysFilm")
|
---|
295 | public String saveCinemaPlaysFilm(@RequestParam Integer id_cinema,
|
---|
296 | @RequestParam Integer id_film)
|
---|
297 | {
|
---|
298 | cinemaPlaysFilmService.save(id_cinema,id_film);
|
---|
299 | return "redirect:/home";
|
---|
300 | }
|
---|
301 |
|
---|
302 | @GetMapping("/getProjection/{id}")
|
---|
303 | public String getProjection(@PathVariable Integer id_projection,Model model)
|
---|
304 | {
|
---|
305 | List<Projection_Room> projectionRooms = null;
|
---|
306 | Projection projection=projectionService.findById(id_projection);
|
---|
307 |
|
---|
308 |
|
---|
309 | List<ProjectionIsPlayedInRoom> p= projectionIsPlayedInRoomService.getProjectionPlayedInRoom(id_projection);
|
---|
310 |
|
---|
311 | model.addAttribute("projection",projection);
|
---|
312 | model.addAttribute("p_rooms",projectionRooms);
|
---|
313 | model.addAttribute("bodyContent","projectionDetails");
|
---|
314 | return "master-template";
|
---|
315 | }
|
---|
316 |
|
---|
317 | @PostMapping("/makeReservation")
|
---|
318 | public String createTicketForReservation()
|
---|
319 | {
|
---|
320 | return "redirect:/myTickets";
|
---|
321 | }
|
---|
322 |
|
---|
323 | }
|
---|