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

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

Update HomeController.java

  • Property mode set to 100644
File size: 19.6 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 response.sendRedirect( "redirect:/login");
195 }catch (PasswordsDoNotMatchException exception)
196 {
197// return "redirect:/register?error=" + exception.getMessage();
198 }
199 }
200
201 }
202 @GetMapping("/registerWorker")
203 public String getRegisterWorkerPage(Model model){
204 model.addAttribute("cinemas",cinemaService.findAllCinemas());
205 model.addAttribute("bodyContent","registerWorker");
206 return "master-template";
207 }
208 @PostMapping("/finishRegister")
209 public void handleWorkerRegister(Model model, HttpServletResponse response, HttpSession session,
210 @RequestParam String position, @RequestParam String work_hours_from,
211 @RequestParam String work_hours_to,@RequestParam Integer id_cinema){
212 System.out.println("here?");
213 String username = (String) session.getAttribute("username");
214 String first_name = (String) session.getAttribute("first_name");
215 String last_name = (String) session.getAttribute("last_name");
216 String password = (String) session.getAttribute("password");
217 String email = (String) session.getAttribute("email");
218 String number = (String) session.getAttribute("number");
219 Cinema cinema=cinemaService.findCinemaById(id_cinema);
220 userService.registerWorker(first_name,last_name,username,email,number,password,position,work_hours_from,work_hours_to,cinema);
221 try {
222 response.sendRedirect("/login");
223 } catch (IOException e) {
224 throw new RuntimeException(e);
225 }
226 }
227
228 @GetMapping("/films")
229 @Transactional
230 public String getFilmsPage1(Model model,@RequestParam(required = false) Integer id_cinema){
231 model.addAttribute("cinemas",cinemaService.findAllCinemas());
232 if (id_cinema!=null) {
233 model.addAttribute("films",filmService.getFilmsFromCinema(id_cinema));
234 }else{
235 List<FilmsReturnTable> pom=new LinkedList<>();
236 model.addAttribute("films",filmService.findAllFilms());
237 }
238 model.addAttribute("bodyContent","films");
239 return "master-template";
240 }
241 @Transactional
242 @GetMapping("/projections")
243 public String getProjectionsPage(Model model,@RequestParam(required = false) Integer id_cinema)
244 {
245 model.addAttribute("cinemas",cinemaService.findAllCinemas());
246 if (id_cinema!=null) {
247 model.addAttribute("films",filmService.getFilmsFromCinemaNow(id_cinema));
248 }else{
249 List<FilmsReturnTable> pom=new LinkedList<>();
250 model.addAttribute("films",filmService.getFilmsNow());
251 }
252 model.addAttribute("bodyContent","projections");
253 return "master-template";
254 }
255 @GetMapping("/events")
256 @Transactional
257 public String getEventsPage(Model model,@RequestParam(required = false) Integer id_cinema)
258 {
259 model.addAttribute("cinemas",cinemaService.findAllCinemas());
260 if (id_cinema!=null) {
261 model.addAttribute("events",eventService.getEventsFromCinema(id_cinema));
262 }else{
263 List<FilmsReturnTable> pom=new LinkedList<>();
264 model.addAttribute("events",eventService.getEventsNow());
265 }
266 model.addAttribute("bodyContent","events");
267 return "master-template";
268 }
269 @GetMapping("/myTickets")
270 public String getMyTicketsPage(Model model,HttpServletRequest request)
271 {
272 Customer customer=customerService.findByUsername(request.getRemoteUser());
273 model.addAttribute("tickets",ticketService.findAllByCustomer(customer));
274 model.addAttribute("bodyContent","myTickets");
275 return "master-template";
276 }
277 @GetMapping("/addProjection")
278 public String getAddProjectionPage(Model model)
279 {
280 model.addAttribute("films",filmService.findAllFilms());
281 model.addAttribute("bodyContent","addProjection");
282 return "master-template";
283 }
284
285
286 @GetMapping("/addEvent")
287 public String getAddEventPage(Model model)
288 {
289 model.addAttribute("bodyContent","addEvent");
290 return "master-template";
291 }
292 @GetMapping("/addFilm")
293 public String getAddFilmPage(Model model)
294 {
295 model.addAttribute("bodyContent","addFilm");
296 return "master-template";
297 }
298
299 @PostMapping("/addP")
300 public String saveProjection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_start,
301 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_end,
302 @RequestParam String type_of_technology,
303 @RequestParam Integer id_film)
304 {
305 projectionService.save(date_time_start,date_time_end,type_of_technology,id_film);
306 return "redirect:/home";
307 }
308 @PostMapping("/addE")
309 public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
310 @RequestParam String theme,
311 @RequestParam String duration,
312 @RequestParam String img_url,
313 @RequestParam String repeating)
314 {
315 eventService.save(start_date,theme,duration,repeating,img_url);
316 return "redirect:/home";
317 }
318 @PostMapping("/addF")
319 public String saveFilm(
320 @RequestParam String name,
321 @RequestParam Integer duration,
322 @RequestParam String actors,
323 @RequestParam String genre,
324 @RequestParam String age_category,
325 @RequestParam String url,
326 @RequestParam String director,
327 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
328 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end_date
329 )
330 {
331 filmService.save(name,duration,actors,genre,age_category,url,director,start_date,end_date);
332 return "redirect:/home";
333 }
334
335 @GetMapping("/workers")
336 public String getWorkersPage(Model model)
337 {
338 model.addAttribute("workers",workerService.findAllWorkers());
339 model.addAttribute("bodyContent", "workers");
340 return "master-template";
341 }
342
343 @GetMapping("/addEventToCinema")
344 public String getCinemaOrganizesEventPage(Model model)
345 {
346 model.addAttribute("cinemas",cinemaService.findAllCinemas());
347 model.addAttribute("events",eventService.findAllEvents());
348 model.addAttribute("bodyContent","addEventToCinema");
349 return "master-template";
350 }
351 @PostMapping("/addCinemaOrganizesEvent")
352 public String saveCinemaOrganizesEvent(@RequestParam Integer id_cinema,
353 @RequestParam Integer id_event)
354 {
355
356 cinemaOrganizesEventService.save(id_cinema,id_event);
357 return "redirect:/home";
358 }
359 @GetMapping("/addFilmToCinema")
360 public String getCinemaPlaysFilmPage(Model model)
361 {
362 model.addAttribute("cinemas",cinemaService.findAllCinemas());
363 model.addAttribute("films",filmService.findAllFilms());
364 model.addAttribute("bodyContent","addFilmToCinema");
365 return "master-template";
366 }
367 @PostMapping("/addCinemaPlaysFilm")
368 public String saveCinemaPlaysFilm(@RequestParam Integer id_cinema,
369 @RequestParam Integer id_film)
370 {
371 cinemaPlaysFilmService.save(id_cinema,id_film);
372 return "redirect:/home";
373 }
374
375 @GetMapping("/getProjection/{id}")
376 public String getProjection(@PathVariable Integer id_projection,Model model)
377 {
378 List<Projection_Room> projectionRooms = null;
379 Projection projection=projectionService.findById(id_projection);
380
381
382 List<ProjectionIsPlayedInRoom> p= projectionIsPlayedInRoomService.getProjectionPlayedInRoom(id_projection);
383
384 model.addAttribute("projection",projection);
385 model.addAttribute("p_rooms",projectionRooms);
386 model.addAttribute("bodyContent","projectionDetails");
387 return "master-template";
388 }
389
390 @PostMapping("/makeReservation")
391 @Transactional
392 public String createTicketForReservation(@RequestParam Long film,@RequestParam Long projection,@RequestParam Long id_seat,@RequestParam String discount,HttpServletRequest request, HttpServletResponse respons)
393 {
394 Ticket t;
395 Customer customer=customerService.findByUsername(request.getRemoteUser());
396 Projection projection1=projectionService.findById(projection.intValue());
397 if(projection1.getDiscount().equals(discount)){
398 t=ticketService.saveWithDiscount(LocalDate.now(),customer,projection1,projection1.getDiscount(),seatService.getSeatById(id_seat.intValue()).get());
399 }else{
400 t=ticketService.saveWithout(LocalDate.now(),customer,projection1,seatService.getSeatById(id_seat.intValue()).get());
401 }
402 Integer price=ticketService.priceForTicket(t.getId_ticket());
403 t.setPrice(price);
404 return "redirect:/myTickets";
405 }
406 @PostMapping("/addRating/{id}")
407 public String addRating(@RequestParam Long rate,@PathVariable Long id,HttpServletRequest request, HttpServletResponse respons)
408 {
409 Customer customer=customerService.findByUsername(request.getRemoteUser());
410 System.out.println(customer.getFirst_name());
411 customerRatesFilmService.addRating(customer.getId_user(),Integer.valueOf(id.intValue()),Integer.valueOf(rate.intValue()));
412 return "redirect:/home/getFilm/"+id;
413 }
414 @GetMapping("/profileWorker")
415 public String getWorkerProfile(Model model,HttpServletRequest request)
416 {
417 Worker worker=workerService.getWorkerByUsername(request.getRemoteUser());
418 model.addAttribute("worker",worker);
419 model.addAttribute("bodyContent", "profileWorker");
420 return "master-template";
421 }
422 @GetMapping("/profileUser")
423 @Transactional
424 public String getUserProfile(Model model,HttpServletRequest request)
425 {
426 Customer customer=customerService.findByUsername(request.getRemoteUser());
427 System.out.println(customer.getFirst_name());
428 List<Event> events=eventService.getEventsForCustomer(customer.getId_user());
429 model.addAttribute("customer",customer);
430 model.addAttribute("events",events);
431 model.addAttribute("bodyContent", "profileUser");
432 return "master-template";
433 }
434 @PostMapping("/addInterestedEvent/{id}")
435 public String addInterestedEvent(@PathVariable Long id,HttpServletRequest request, HttpServletResponse respons)
436 {
437 Customer customer=customerService.findByUsername(request.getRemoteUser());
438 customerIsInterestedInEventService.add(customer.getId_user(),Integer.valueOf(id.intValue()));
439 return "redirect:/profileUser";
440 }
441 @PostMapping("/deleteInterestedEvent/{id}")
442 public String deleteInterestedEvent(@PathVariable Long id,HttpServletRequest request, HttpServletResponse respons)
443 {
444 Customer customer=customerService.findByUsername(request.getRemoteUser());
445 Event event=eventService.getEventById(id).get();
446 customerIsInterestedInEventService.delete(customer,event);
447 return "redirect:/profileUser";
448 }
449}
Note: See TracBrowser for help on using the repository browser.