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

Last change on this file since bcb4acc was bcb4acc, checked in by DenicaKj <dkorvezir@…>, 15 months ago

filter for genre

  • Property mode set to 100644
File size: 22.4 KB
Line 
1package com.example.moviezone.web;
2
3
4import com.example.moviezone.model.*;
5import com.example.moviezone.model.enums.GenreEnum;
6import com.example.moviezone.model.exceptions.PasswordsDoNotMatchException;
7
8import com.example.moviezone.model.exceptions.UserNotFoundException;
9import com.example.moviezone.model.manytomany.ProjectionIsPlayedInRoom;
10
11import com.example.moviezone.model.procedures.FilmsReturnTable;
12
13import com.example.moviezone.model.procedures.TicketsCancelClass;
14import com.example.moviezone.service.*;
15import org.springframework.format.annotation.DateTimeFormat;
16import org.springframework.stereotype.Controller;
17import org.springframework.ui.Model;
18import org.springframework.web.bind.annotation.*;
19
20import javax.servlet.http.HttpServletRequest;
21import javax.servlet.http.HttpServletResponse;
22import javax.servlet.http.HttpSession;
23import javax.transaction.Transactional;
24import java.io.IOException;
25import java.time.LocalDate;
26import java.time.LocalDateTime;
27import java.time.temporal.ChronoUnit;
28import java.util.ArrayList;
29import java.util.Collections;
30import java.util.LinkedList;
31import java.util.List;
32import java.util.stream.Collectors;
33
34@Controller
35@RequestMapping({"/","/home"})
36public class HomeController {
37
38private final FilmService filmService;
39private final UserService userService;
40private final ProjectionService projectionService;
41private final EventService eventService;
42private final TicketService ticketService;
43private final WorkerService workerService;
44private final CustomerRatesFilmService customerRatesFilmService;
45private final CinemaService cinemaService;
46private final CinemaOrganizesEventService cinemaOrganizesEventService;
47private final CinemaPlaysFilmService cinemaPlaysFilmService;
48private final ProjectionIsPlayedInRoomService projectionIsPlayedInRoomService;
49private final CategoryService categoryService;
50private final SeatService seatService;
51private final CustomerService customerService;
52private final Projection_RoomService projectionRoomService;
53private final CustomerIsInterestedInEventService customerIsInterestedInEventService;
54private final DiscountService discountService;
55
56 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, SeatService seatService, CustomerService customerService, Projection_RoomService projectionRoomService, CustomerIsInterestedInEventService customerIsInterestedInEventService, DiscountService discountService)
57 {
58
59 this.filmService = filmService;
60 this.userService = userService;
61 this.projectionService = projectionService;
62 this.eventService = eventService;
63 this.ticketService = ticketService;
64 this.workerService = workerService;
65 this.customerRatesFilmService = customerRatesFilmService;
66 this.cinemaService = cinemaService;
67 this.cinemaOrganizesEventService = cinemaOrganizesEventService;
68 this.cinemaPlaysFilmService = cinemaPlaysFilmService;
69 this.projectionIsPlayedInRoomService = projectionIsPlayedInRoomService;
70 this.categoryService = categoryService;
71 this.seatService = seatService;
72 this.customerService = customerService;
73 this.projectionRoomService = projectionRoomService;
74 this.customerIsInterestedInEventService = customerIsInterestedInEventService;
75 this.discountService = discountService;
76 }
77
78 @GetMapping
79 public String getHomePage(Model model) {
80 List<Film> films=filmService.findAllFilms();
81 Collections.reverse(films);
82 films=films.stream().limit(5).collect(Collectors.toList());
83 List <Event> events=eventService.findAllEvents();
84 Collections.reverse(events);
85 events=events.stream().limit(5).collect(Collectors.toList());
86 model.addAttribute("films", films);
87 model.addAttribute("events",events);
88 model.addAttribute("bodyContent", "home");
89
90 return "master-template";
91 }
92 @GetMapping("/getFilm/{id}")
93 public String getFilm(@PathVariable Long id, Model model) {
94 Film film=filmService.getFilmById(id).get();
95 model.addAttribute("film", film);
96 List<String> genres= List.of(film.getGenre().split(","));
97 double r=customerRatesFilmService.avg_rating(film.getId_film());
98 Double ra=Double.valueOf(r);
99 if(ra==null){
100 r=0;
101 }
102 model.addAttribute("rating",r);
103 model.addAttribute("genres", genres);
104 model.addAttribute("bodyContent", "film");
105
106 return "master-template";
107 }
108 @GetMapping("/getEvent/{id}")
109 public String getEvent(@PathVariable Long id, Model model) {
110 Event event =eventService.getEventById(id).get();
111 model.addAttribute("event", event);
112 model.addAttribute("bodyContent", "event");
113
114 return "master-template";
115 }
116 @GetMapping("/getProjections/{id}")
117 @Transactional
118 public String getProjectionsFromFilm(@PathVariable Long id, Model model) {
119 Film film=filmService.getFilmById(id).get();
120 model.addAttribute("film",film);
121 model.addAttribute("projections",projectionService.getProjectionsForFilms(id.intValue()));
122 model.addAttribute("categories",categoryService.findAllCategories());
123 model.addAttribute("bodyContent", "projectionsForFilm");
124
125 return "master-template";
126 }
127 @GetMapping("/getSeats/{id}")
128 @Transactional
129 public String getSeats(@PathVariable Long id, Model model,@RequestParam Long id_category,@RequestParam Long film) {
130 Category category=categoryService.getCategoryById(id_category.intValue()).get();
131 Projection projection=projectionService.findById(id.intValue());
132 model.addAttribute("film",filmService.getFilmById(film).get());
133 model.addAttribute("projection",projection);
134 model.addAttribute("category",category);
135
136 List<Seat> seats=seatService.findAllByRoomAndCategory(projection,projectionRoomService.getRoomByProjection(projection.getId_projection()).get(0),category);
137 model.addAttribute("seats",seats);
138 model.addAttribute("bodyContent", "seats");
139
140 return "master-template";
141 }
142 @GetMapping("/login")
143 public String getLoginPage(Model model)
144 {
145 model.addAttribute("bodyContent", "login");
146 return "master-template";
147 }
148
149 @GetMapping("/register")
150 public String getRegisterPage(Model model)
151 {
152 model.addAttribute("bodyContent", "register");
153 return "master-template";
154 }
155
156 @PostMapping("/login")
157 public String login(@RequestParam String username,
158 @RequestParam String password, Model model, HttpServletRequest request)
159 {
160// User user = null;
161 try {
162 User user=userService.login(username,password);
163 System.out.println(user.getFirst_name());
164 request.getSession().setAttribute("user", user);
165 // model.addAttribute("user",user);
166 return "redirect:/home";
167
168 }catch (UserNotFoundException e)
169 {
170 model.addAttribute("hasError", true);
171 model.addAttribute("error", e.getMessage());
172 return "login";
173 }
174
175 }
176
177 @PostMapping("/register")
178 public void register(@RequestParam String username,
179 @RequestParam String first_name,
180 @RequestParam String last_name,
181 @RequestParam String password,
182 @RequestParam String repeatedPassword,
183 @RequestParam String email,
184 @RequestParam String number,
185 @RequestParam Role role,HttpServletResponse response, HttpSession session) throws IOException {
186
187 System.out.println(username + first_name+ last_name + password + repeatedPassword + email + number + role);
188 if(role.equals(Role.ROLE_ADMIN)){
189 session.setAttribute("username", username);
190 session.setAttribute("first_name", first_name);
191 session.setAttribute("last_name", last_name);
192 session.setAttribute("password", password);
193 session.setAttribute("repeatedPassword", repeatedPassword);
194 session.setAttribute("email", email);
195 session.setAttribute("number", number);
196 response.sendRedirect("/registerWorker");
197 }
198 else {
199 try {
200 userService.register(first_name,last_name,username,email,number,password,role);
201
202 }catch (PasswordsDoNotMatchException exception)
203 {
204// return "redirect:/register?error=" + exception.getMessage();
205 }
206 response.sendRedirect("/login");
207 }
208
209 }
210 @GetMapping("/registerWorker")
211 public String getRegisterWorkerPage(Model model){
212 model.addAttribute("cinemas",cinemaService.findAllCinemas());
213 model.addAttribute("bodyContent","registerWorker");
214 return "master-template";
215 }
216 @PostMapping("/finishRegister")
217 public void handleWorkerRegister(Model model, HttpServletResponse response, HttpSession session,
218 @RequestParam String position, @RequestParam String work_hours_from,
219 @RequestParam String work_hours_to,@RequestParam Integer id_cinema){
220 System.out.println("here?");
221 String username = (String) session.getAttribute("username");
222 String first_name = (String) session.getAttribute("first_name");
223 String last_name = (String) session.getAttribute("last_name");
224 String password = (String) session.getAttribute("password");
225 String email = (String) session.getAttribute("email");
226 String number = (String) session.getAttribute("number");
227 Cinema cinema=cinemaService.findCinemaById(id_cinema);
228 userService.registerWorker(first_name,last_name,username,email,number,password,position,work_hours_from,work_hours_to,cinema);
229 try {
230 response.sendRedirect("/login");
231 } catch (IOException e) {
232 throw new RuntimeException(e);
233 }
234 }
235
236 @GetMapping("/films")
237 @Transactional
238 public String getFilmsPage1(Model model,@RequestParam(required = false) Integer id_cinema, @RequestParam(required = false) Integer id_genre){
239 model.addAttribute("cinemas",cinemaService.findAllCinemas());
240 List<GenreEnum> genres = List.of(GenreEnum.values());
241 model.addAttribute("genres", genres);
242 List<Film> films = filmService.findAllFilms();
243 if (id_cinema!=null) {
244 films = filmService.getFilmsFromCinema(id_cinema);
245 }
246 if ( id_genre != null){
247 List<Film> pom= new ArrayList<>();
248 for (int i = 0; i < films.size(); i++) {
249 if(films.get(i).getGenre().contains(genres.get(id_genre).name().toLowerCase())){
250 pom.add(films.get(i));
251 }
252 }
253 films=pom;
254 }
255 model.addAttribute("films",films);
256 model.addAttribute("bodyContent","films");
257 return "master-template";
258 }
259 @Transactional
260 @GetMapping("/projections")
261 public String getProjectionsPage(Model model,@RequestParam(required = false) Integer id_cinema)
262 {
263 model.addAttribute("cinemas",cinemaService.findAllCinemas());
264 if (id_cinema!=null) {
265 model.addAttribute("films",filmService.getFilmsFromCinemaNow(id_cinema));
266 }else{
267 List<FilmsReturnTable> pom=new LinkedList<>();
268 model.addAttribute("films",filmService.getFilmsNow());
269 }
270 model.addAttribute("bodyContent","projections");
271 return "master-template";
272 }
273 @GetMapping("/events")
274 @Transactional
275 public String getEventsPage(Model model,@RequestParam(required = false) Integer id_cinema)
276 {
277 model.addAttribute("cinemas",cinemaService.findAllCinemas());
278 if (id_cinema!=null) {
279 model.addAttribute("events",eventService.getEventsFromCinema(id_cinema));
280 }else{
281 model.addAttribute("events",eventService.getEventsNow());
282 }
283 model.addAttribute("bodyContent","events");
284 return "master-template";
285 }
286 @GetMapping("/myTickets")
287 public String getMyTicketsPage(Model model,HttpServletRequest request)
288 {
289 Customer customer=customerService.findByUsername(request.getRemoteUser());
290 List<Ticket> tickets = ticketService.findAllByCustomer(customer);
291 List<TicketsCancelClass> ticketsCancelClass = new ArrayList<>();
292 LocalDateTime oneDayLater = LocalDateTime.now().plus(1, ChronoUnit.DAYS);
293 for (int i = 0; i < tickets.size(); i++) {
294 if(tickets.get(i).getProjection().getDate_time_start().isAfter(oneDayLater)){
295 ticketsCancelClass.add(new TicketsCancelClass(tickets.get(i),true));
296 }else{
297 ticketsCancelClass.add(new TicketsCancelClass(tickets.get(i),false));
298 }
299 }
300 model.addAttribute("tickets",ticketsCancelClass);
301 model.addAttribute("bodyContent","myTickets");
302 return "master-template";
303 }
304
305 @PostMapping("/cancelTicket/{id}")
306 public String getSeats(@PathVariable Long id, Model model) {
307 ticketService.delete(id.intValue());
308 return "redirect:/myTickets";
309 }
310
311 @GetMapping("/addProjection")
312 @Transactional
313 public String getAddProjectionPage(Model model)
314 {
315 model.addAttribute("films",filmService.findAllFilms());
316 model.addAttribute("projection_rooms", projectionRoomService.findAllProjectionRooms());
317 model.addAttribute("discounts",discountService.getValidDiscounts());
318 model.addAttribute("bodyContent","addProjection");
319 return "master-template";
320 }
321 @GetMapping("/addDiscount")
322 public String getAddDiscountPage(Model model)
323 {
324 model.addAttribute("bodyContent","addDiscount");
325 return "master-template";
326 }
327 @GetMapping("/addEvent")
328 public String getAddEventPage(Model model)
329 {
330 model.addAttribute("bodyContent","addEvent");
331 return "master-template";
332 }
333 @GetMapping("/addFilm")
334 public String getAddFilmPage(Model model)
335 {
336 model.addAttribute("bodyContent","addFilm");
337 return "master-template";
338 }
339
340 @PostMapping("/addD")
341 public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate validity,
342 @RequestParam String type,
343 @RequestParam String code,
344 @RequestParam Integer percent)
345 {
346 discountService.save(code,type,validity,percent);
347 return "redirect:/home";
348 }
349
350 @PostMapping("/addP")
351 public String saveProjection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date_time_start,
352 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date_time_end,
353 @RequestParam String type_of_technology,
354 @RequestParam Integer id_film,
355 @RequestParam Integer id_room,
356 @RequestParam Integer id_discount)
357 {
358 projectionService.save(date_time_start,date_time_end,type_of_technology,id_film,id_room,id_discount);
359 return "redirect:/home";
360 }
361 @PostMapping("/addE")
362 public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
363 @RequestParam String theme,
364 @RequestParam String duration,
365 @RequestParam String img_url,
366 @RequestParam String repeating)
367 {
368 eventService.save(start_date,theme,duration,repeating,img_url);
369 return "redirect:/home";
370 }
371 @PostMapping("/addF")
372 public String saveFilm(
373 @RequestParam String name,
374 @RequestParam Integer duration,
375 @RequestParam String actors,
376 @RequestParam String genre,
377 @RequestParam String age_category,
378 @RequestParam String url,
379 @RequestParam String director,
380 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
381 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end_date
382 )
383 {
384 filmService.save(name,duration,actors,genre,age_category,url,director,start_date,end_date);
385 return "redirect:/home";
386 }
387
388 @GetMapping("/workers")
389 public String getWorkersPage(Model model)
390 {
391 model.addAttribute("workers",workerService.findAllWorkers());
392 model.addAttribute("bodyContent", "workers");
393 return "master-template";
394 }
395
396 @GetMapping("/addEventToCinema")
397 public String getCinemaOrganizesEventPage(Model model)
398 {
399 model.addAttribute("cinemas",cinemaService.findAllCinemas());
400 model.addAttribute("events",eventService.findAllEvents());
401 model.addAttribute("bodyContent","addEventToCinema");
402 return "master-template";
403 }
404
405 @PostMapping("/addCinemaOrganizesEvent")
406 public String saveCinemaOrganizesEvent(@RequestParam Integer id_cinema,
407 @RequestParam Integer id_event)
408 {
409
410 cinemaOrganizesEventService.save(id_cinema,id_event);
411 return "redirect:/home";
412 }
413 @GetMapping("/addFilmToCinema")
414 public String getCinemaPlaysFilmPage(Model model)
415 {
416 model.addAttribute("cinemas",cinemaService.findAllCinemas());
417 model.addAttribute("films",filmService.findAllFilms());
418 model.addAttribute("bodyContent","addFilmToCinema");
419 return "master-template";
420 }
421 @PostMapping("/addCinemaPlaysFilm")
422 public String saveCinemaPlaysFilm(@RequestParam Integer id_cinema,
423 @RequestParam Integer id_film)
424 {
425 cinemaPlaysFilmService.save(id_cinema,id_film);
426 return "redirect:/home";
427 }
428
429 @GetMapping("/getProjection/{id}")
430 public String getProjection(@PathVariable Integer id_projection,Model model)
431 {
432 List<Projection_Room> projectionRooms = null;
433 Projection projection=projectionService.findById(id_projection);
434
435
436 List<ProjectionIsPlayedInRoom> p= projectionIsPlayedInRoomService.getProjectionPlayedInRoom(id_projection);
437
438 model.addAttribute("projection",projection);
439 model.addAttribute("p_rooms",projectionRooms);
440 model.addAttribute("bodyContent","projectionDetails");
441 return "master-template";
442 }
443
444 @PostMapping("/makeReservation")
445 @Transactional
446 public String createTicketForReservation(@RequestParam Long film,@RequestParam Long projection,@RequestParam Long id_seat,@RequestParam String discount,HttpServletRequest request, HttpServletResponse respons)
447 {
448 Ticket t;
449 Customer customer=customerService.findByUsername(request.getRemoteUser());
450 Projection projection1=projectionService.findById(projection.intValue());
451 if(projection1.getDiscount()!=null && projection1.getDiscount().getCode().equals(discount)){
452 t=ticketService.saveWithDiscount(LocalDate.now(),customer,projection1,projection1.getDiscount(),seatService.getSeatById(id_seat.intValue()).get());
453 Integer price=ticketService.priceForTicket(t.getId_ticket());
454 price+=seatService.getSeatById(id_seat.intValue()).get().getCategory().getExtra_amount();
455 price-=(price*projection1.getDiscount().getPercent())/100;
456 t.setPrice(price);
457 }else{
458 t=ticketService.saveWithout(LocalDate.now(),customer,projection1,seatService.getSeatById(id_seat.intValue()).get());
459 Integer price=ticketService.priceForTicket(t.getId_ticket());
460 price+=seatService.getSeatById(id_seat.intValue()).get().getCategory().getExtra_amount();
461 t.setPrice(price);
462 }
463
464 return "redirect:/myTickets";
465 }
466 @PostMapping("/addRating/{id}")
467 public String addRating(@RequestParam Long rate,@PathVariable Long id,HttpServletRequest request, HttpServletResponse respons)
468 {
469 Customer customer=customerService.findByUsername(request.getRemoteUser());
470 System.out.println(customer.getFirst_name());
471 customerRatesFilmService.addRating(customer.getId_user(),Integer.valueOf(id.intValue()),Integer.valueOf(rate.intValue()));
472 return "redirect:/home/getFilm/"+id;
473 }
474 @GetMapping("/profileWorker")
475 public String getWorkerProfile(Model model,HttpServletRequest request)
476 {
477 Worker worker=workerService.getWorkerByUsername(request.getRemoteUser());
478 model.addAttribute("worker",worker);
479 model.addAttribute("bodyContent", "profileWorker");
480 return "master-template";
481 }
482 @GetMapping("/profileUser")
483 @Transactional
484 public String getUserProfile(Model model,HttpServletRequest request)
485 {
486 Customer customer=customerService.findByUsername(request.getRemoteUser());
487 System.out.println(customer.getFirst_name());
488 List<Event> events=eventService.getEventsForCustomer(customer.getId_user());
489 model.addAttribute("customer",customer);
490 model.addAttribute("events",events);
491 model.addAttribute("bodyContent", "profileUser");
492 return "master-template";
493 }
494 @PostMapping("/addInterestedEvent/{id}")
495 public String addInterestedEvent(@PathVariable Long id,HttpServletRequest request, HttpServletResponse respons)
496 {
497 Customer customer=customerService.findByUsername(request.getRemoteUser());
498 customerIsInterestedInEventService.add(customer.getId_user(),Integer.valueOf(id.intValue()));
499 return "redirect:/profileUser";
500 }
501 @PostMapping("/deleteInterestedEvent/{id}")
502 public String deleteInterestedEvent(@PathVariable Long id,HttpServletRequest request, HttpServletResponse respons)
503 {
504 Customer customer=customerService.findByUsername(request.getRemoteUser());
505 Event event=eventService.getEventById(id).get();
506 customerIsInterestedInEventService.delete(customer,event);
507 return "redirect:/profileUser";
508 }
509}
Note: See TracBrowser for help on using the repository browser.