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

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

Interested in event

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