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

Last change on this file since 8a18cf5 was 8a18cf5, checked in by DenicaKj <dkorvezir@…>, 21 months ago

fixes

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