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

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

fix

  • Property mode set to 100644
File size: 22.1 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){
239 model.addAttribute("cinemas",cinemaService.findAllCinemas());
240 model.addAttribute("genres", GenreEnum.values());
241 if (id_cinema!=null) {
242 model.addAttribute("films",filmService.getFilmsFromCinema(id_cinema));
243 }else{
244 List<FilmsReturnTable> pom=new LinkedList<>();
245 model.addAttribute("films",filmService.findAllFilms());
246 }
247 model.addAttribute("bodyContent","films");
248 return "master-template";
249 }
250 @Transactional
251 @GetMapping("/projections")
252 public String getProjectionsPage(Model model,@RequestParam(required = false) Integer id_cinema)
253 {
254 model.addAttribute("cinemas",cinemaService.findAllCinemas());
255 if (id_cinema!=null) {
256 model.addAttribute("films",filmService.getFilmsFromCinemaNow(id_cinema));
257 }else{
258 List<FilmsReturnTable> pom=new LinkedList<>();
259 model.addAttribute("films",filmService.getFilmsNow());
260 }
261 model.addAttribute("bodyContent","projections");
262 return "master-template";
263 }
264 @GetMapping("/events")
265 @Transactional
266 public String getEventsPage(Model model,@RequestParam(required = false) Integer id_cinema)
267 {
268 model.addAttribute("cinemas",cinemaService.findAllCinemas());
269 if (id_cinema!=null) {
270 model.addAttribute("events",eventService.getEventsFromCinema(id_cinema));
271 }else{
272 List<FilmsReturnTable> pom=new LinkedList<>();
273 model.addAttribute("events",eventService.getEventsNow());
274 }
275 model.addAttribute("bodyContent","events");
276 return "master-template";
277 }
278 @GetMapping("/myTickets")
279 public String getMyTicketsPage(Model model,HttpServletRequest request)
280 {
281 Customer customer=customerService.findByUsername(request.getRemoteUser());
282 List<Ticket> tickets = ticketService.findAllByCustomer(customer);
283 List<TicketsCancelClass> ticketsCancelClass = new ArrayList<>();
284 LocalDateTime oneDayLater = LocalDateTime.now().plus(1, ChronoUnit.DAYS);
285 for (int i = 0; i < tickets.size(); i++) {
286 if(tickets.get(i).getProjection().getDate_time_start().isAfter(oneDayLater)){
287 ticketsCancelClass.add(new TicketsCancelClass(tickets.get(i),true));
288 }else{
289 ticketsCancelClass.add(new TicketsCancelClass(tickets.get(i),false));
290 }
291 }
292 model.addAttribute("tickets",ticketsCancelClass);
293 model.addAttribute("bodyContent","myTickets");
294 return "master-template";
295 }
296
297 @PostMapping("/cancelTicket/{id}")
298 public String getSeats(@PathVariable Long id, Model model) {
299 ticketService.delete(id.intValue());
300 return "redirect:/myTickets";
301 }
302
303 @GetMapping("/addProjection")
304 @Transactional
305 public String getAddProjectionPage(Model model)
306 {
307 model.addAttribute("films",filmService.findAllFilms());
308 model.addAttribute("projection_rooms", projectionRoomService.findAllProjectionRooms());
309 model.addAttribute("discounts",discountService.getValidDiscounts());
310 model.addAttribute("bodyContent","addProjection");
311 return "master-template";
312 }
313 @GetMapping("/addDiscount")
314 public String getAddDiscountPage(Model model)
315 {
316 model.addAttribute("bodyContent","addDiscount");
317 return "master-template";
318 }
319 @GetMapping("/addEvent")
320 public String getAddEventPage(Model model)
321 {
322 model.addAttribute("bodyContent","addEvent");
323 return "master-template";
324 }
325 @GetMapping("/addFilm")
326 public String getAddFilmPage(Model model)
327 {
328 model.addAttribute("bodyContent","addFilm");
329 return "master-template";
330 }
331
332 @PostMapping("/addD")
333 public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate validity,
334 @RequestParam String type,
335 @RequestParam String code,
336 @RequestParam Integer percent)
337 {
338 discountService.save(code,type,validity,percent);
339 return "redirect:/home";
340 }
341
342 @PostMapping("/addP")
343 public String saveProjection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date_time_start,
344 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date_time_end,
345 @RequestParam String type_of_technology,
346 @RequestParam Integer id_film,
347 @RequestParam Integer id_room,
348 @RequestParam Integer id_discount)
349 {
350 projectionService.save(date_time_start,date_time_end,type_of_technology,id_film,id_room,id_discount);
351 return "redirect:/home";
352 }
353 @PostMapping("/addE")
354 public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
355 @RequestParam String theme,
356 @RequestParam String duration,
357 @RequestParam String img_url,
358 @RequestParam String repeating)
359 {
360 eventService.save(start_date,theme,duration,repeating,img_url);
361 return "redirect:/home";
362 }
363 @PostMapping("/addF")
364 public String saveFilm(
365 @RequestParam String name,
366 @RequestParam Integer duration,
367 @RequestParam String actors,
368 @RequestParam String genre,
369 @RequestParam String age_category,
370 @RequestParam String url,
371 @RequestParam String director,
372 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
373 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end_date
374 )
375 {
376 filmService.save(name,duration,actors,genre,age_category,url,director,start_date,end_date);
377 return "redirect:/home";
378 }
379
380 @GetMapping("/workers")
381 public String getWorkersPage(Model model)
382 {
383 model.addAttribute("workers",workerService.findAllWorkers());
384 model.addAttribute("bodyContent", "workers");
385 return "master-template";
386 }
387
388 @GetMapping("/addEventToCinema")
389 public String getCinemaOrganizesEventPage(Model model)
390 {
391 model.addAttribute("cinemas",cinemaService.findAllCinemas());
392 model.addAttribute("events",eventService.findAllEvents());
393 model.addAttribute("bodyContent","addEventToCinema");
394 return "master-template";
395 }
396
397 @PostMapping("/addCinemaOrganizesEvent")
398 public String saveCinemaOrganizesEvent(@RequestParam Integer id_cinema,
399 @RequestParam Integer id_event)
400 {
401
402 cinemaOrganizesEventService.save(id_cinema,id_event);
403 return "redirect:/home";
404 }
405 @GetMapping("/addFilmToCinema")
406 public String getCinemaPlaysFilmPage(Model model)
407 {
408 model.addAttribute("cinemas",cinemaService.findAllCinemas());
409 model.addAttribute("films",filmService.findAllFilms());
410 model.addAttribute("bodyContent","addFilmToCinema");
411 return "master-template";
412 }
413 @PostMapping("/addCinemaPlaysFilm")
414 public String saveCinemaPlaysFilm(@RequestParam Integer id_cinema,
415 @RequestParam Integer id_film)
416 {
417 cinemaPlaysFilmService.save(id_cinema,id_film);
418 return "redirect:/home";
419 }
420
421 @GetMapping("/getProjection/{id}")
422 public String getProjection(@PathVariable Integer id_projection,Model model)
423 {
424 List<Projection_Room> projectionRooms = null;
425 Projection projection=projectionService.findById(id_projection);
426
427
428 List<ProjectionIsPlayedInRoom> p= projectionIsPlayedInRoomService.getProjectionPlayedInRoom(id_projection);
429
430 model.addAttribute("projection",projection);
431 model.addAttribute("p_rooms",projectionRooms);
432 model.addAttribute("bodyContent","projectionDetails");
433 return "master-template";
434 }
435
436 @PostMapping("/makeReservation")
437 @Transactional
438 public String createTicketForReservation(@RequestParam Long film,@RequestParam Long projection,@RequestParam Long id_seat,@RequestParam String discount,HttpServletRequest request, HttpServletResponse respons)
439 {
440 Ticket t;
441 Customer customer=customerService.findByUsername(request.getRemoteUser());
442 Projection projection1=projectionService.findById(projection.intValue());
443 if(projection1.getDiscount()!=null && projection1.getDiscount().getCode().equals(discount)){
444 t=ticketService.saveWithDiscount(LocalDate.now(),customer,projection1,projection1.getDiscount(),seatService.getSeatById(id_seat.intValue()).get());
445 Integer price=ticketService.priceForTicket(t.getId_ticket());
446 price+=seatService.getSeatById(id_seat.intValue()).get().getCategory().getExtra_amount();
447 price-=(price*projection1.getDiscount().getPercent())/100;
448 t.setPrice(price);
449 }else{
450 t=ticketService.saveWithout(LocalDate.now(),customer,projection1,seatService.getSeatById(id_seat.intValue()).get());
451 Integer price=ticketService.priceForTicket(t.getId_ticket());
452 price+=seatService.getSeatById(id_seat.intValue()).get().getCategory().getExtra_amount();
453 t.setPrice(price);
454 }
455
456 return "redirect:/myTickets";
457 }
458 @PostMapping("/addRating/{id}")
459 public String addRating(@RequestParam Long rate,@PathVariable Long id,HttpServletRequest request, HttpServletResponse respons)
460 {
461 Customer customer=customerService.findByUsername(request.getRemoteUser());
462 System.out.println(customer.getFirst_name());
463 customerRatesFilmService.addRating(customer.getId_user(),Integer.valueOf(id.intValue()),Integer.valueOf(rate.intValue()));
464 return "redirect:/home/getFilm/"+id;
465 }
466 @GetMapping("/profileWorker")
467 public String getWorkerProfile(Model model,HttpServletRequest request)
468 {
469 Worker worker=workerService.getWorkerByUsername(request.getRemoteUser());
470 model.addAttribute("worker",worker);
471 model.addAttribute("bodyContent", "profileWorker");
472 return "master-template";
473 }
474 @GetMapping("/profileUser")
475 @Transactional
476 public String getUserProfile(Model model,HttpServletRequest request)
477 {
478 Customer customer=customerService.findByUsername(request.getRemoteUser());
479 System.out.println(customer.getFirst_name());
480 List<Event> events=eventService.getEventsForCustomer(customer.getId_user());
481 model.addAttribute("customer",customer);
482 model.addAttribute("events",events);
483 model.addAttribute("bodyContent", "profileUser");
484 return "master-template";
485 }
486 @PostMapping("/addInterestedEvent/{id}")
487 public String addInterestedEvent(@PathVariable Long id,HttpServletRequest request, HttpServletResponse respons)
488 {
489 Customer customer=customerService.findByUsername(request.getRemoteUser());
490 customerIsInterestedInEventService.add(customer.getId_user(),Integer.valueOf(id.intValue()));
491 return "redirect:/profileUser";
492 }
493 @PostMapping("/deleteInterestedEvent/{id}")
494 public String deleteInterestedEvent(@PathVariable Long id,HttpServletRequest request, HttpServletResponse respons)
495 {
496 Customer customer=customerService.findByUsername(request.getRemoteUser());
497 Event event=eventService.getEventById(id).get();
498 customerIsInterestedInEventService.delete(customer,event);
499 return "redirect:/profileUser";
500 }
501}
Note: See TracBrowser for help on using the repository browser.