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

Last change on this file since 4bfcc04 was 00fa72f, checked in by DenicaKj <dkorvezir@…>, 21 months ago

Reservation Implemented

  • Property mode set to 100644
File size: 14.5 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.manytomany.ProjectionIsPlayedInRoom;
8
9import com.example.moviezone.model.procedures.FilmsReturnTable;
10
11import com.example.moviezone.service.*;
12import org.springframework.format.annotation.DateTimeFormat;
13import org.springframework.stereotype.Controller;
14import org.springframework.ui.Model;
15import org.springframework.web.bind.annotation.*;
16
17import javax.servlet.http.HttpSession;
18import javax.transaction.Transactional;
19import java.time.LocalDate;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.stream.Collectors;
23
24@Controller
25@RequestMapping({"/","/home"})
26public class HomeController {
27
28private final FilmService filmService;
29private final UserService userService;
30private final ProjectionService projectionService;
31private final EventService eventService;
32private final TicketService ticketService;
33private final WorkerService workerService;
34private final CustomerRatesFilmService customerRatesFilmService;
35private final CinemaService cinemaService;
36private final CinemaOrganizesEventService cinemaOrganizesEventService;
37private final CinemaPlaysFilmService cinemaPlaysFilmService;
38private final ProjectionIsPlayedInRoomService projectionIsPlayedInRoomService;
39private final CategoryService categoryService;
40private final SeatService seatService;
41private final CustomerService customerService;
42private final Projection_RoomService projectionRoomService;
43
44 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)
45 {
46
47 this.filmService = filmService;
48 this.userService = userService;
49 this.projectionService = projectionService;
50 this.eventService = eventService;
51 this.ticketService = ticketService;
52 this.workerService = workerService;
53 this.customerRatesFilmService = customerRatesFilmService;
54 this.cinemaService = cinemaService;
55 this.cinemaOrganizesEventService = cinemaOrganizesEventService;
56 this.cinemaPlaysFilmService = cinemaPlaysFilmService;
57 this.projectionIsPlayedInRoomService = projectionIsPlayedInRoomService;
58 this.categoryService = categoryService;
59 this.seatService = seatService;
60 this.customerService = customerService;
61 this.projectionRoomService = projectionRoomService;
62 }
63
64 @GetMapping
65 public String getHomePage(Model model) {
66 List<Film> films=filmService.findAllFilms();
67 films=films.stream().limit(5).collect(Collectors.toList());
68 List <Event> events=eventService.findAllEvents().stream().limit(5).collect(Collectors.toList());
69 model.addAttribute("films", films);
70 model.addAttribute("events",events);
71 model.addAttribute("bodyContent", "home");
72
73 return "master-template";
74 }
75 @GetMapping("/getFilm/{id}")
76 public String getFilm(@PathVariable Long id, Model model) {
77 Film film=filmService.getFilmById(id).get();
78 model.addAttribute("film", film);
79 List<String> genres= List.of(film.getGenre().split(","));
80 double r=customerRatesFilmService.avg_rating(film.getId_film());
81 model.addAttribute("rating",r);
82 model.addAttribute("genres", genres);
83 model.addAttribute("bodyContent", "film");
84
85 return "master-template";
86 }
87 @GetMapping("/getEvent/{id}")
88 public String getEvent(@PathVariable Long id, Model model) {
89 Event event =eventService.getEventById(id).get();
90 model.addAttribute("event", event);
91 model.addAttribute("bodyContent", "event");
92
93 return "master-template";
94 }
95 @GetMapping("/getProjections/{id}")
96 @Transactional
97 public String getProjectionsFromFilm(@PathVariable Long id, Model model) {
98 Film film=filmService.getFilmById(id).get();
99 model.addAttribute("film",film);
100 model.addAttribute("projections",projectionService.getProjectionsForFilms(id.intValue()));
101 model.addAttribute("categories",categoryService.findAllCategories());
102 model.addAttribute("bodyContent", "projectionsForFilm");
103
104 return "master-template";
105 }
106 @GetMapping("/getSeats/{id}")
107 @Transactional
108 public String getSeats(@PathVariable Long id, Model model,@RequestParam Long id_category,@RequestParam Long film) {
109 Category category=categoryService.getCategoryById(id_category.intValue()).get();
110 Projection projection=projectionService.findById(id.intValue());
111 model.addAttribute("film",filmService.getFilmById(film).get());
112 model.addAttribute("projection",projection);
113 model.addAttribute("category",category);
114
115 List<Seat> seats=seatService.findAllByRoomAndCategory(projectionRoomService.getRoomByProjection(projection.getId_projection()).get(0),category);
116 model.addAttribute("seats",seats);
117 model.addAttribute("bodyContent", "seats");
118
119 return "master-template";
120 }
121 @GetMapping("/login")
122 public String getLoginPage(Model model)
123 {
124 model.addAttribute("bodyContent", "login");
125 return "master-template";
126 }
127
128 @GetMapping("/register")
129 public String getRegisterPage(Model model)
130 {
131 model.addAttribute("bodyContent", "register");
132 return "master-template";
133 }
134
135 @PostMapping("/login")
136 public String login(@RequestParam String username,
137 @RequestParam String password,Model model, HttpSession session)
138 {
139// User user = null;
140// try {
141 User user=userService.login(username,password);
142 System.out.println(user.getFirst_name());
143// session.setAttribute("sessionUser",user);
144// model.addAttribute("user",user);
145 return "redirect:/home";
146//
147// }catch (UserNotFoundException e)
148// {
149// model.addAttribute("hasError", true);
150// model.addAttribute("error", e.getMessage());
151// return "login";
152// }
153
154 }
155
156 @PostMapping()
157 public String register(@RequestParam String username,
158 @RequestParam String first_name,
159 @RequestParam String last_name,
160 @RequestParam String password,
161 @RequestParam String repeatedPassword,
162 @RequestParam String email,
163 @RequestParam String number,
164 @RequestParam Role role)
165 {
166 try {
167 userService.register(first_name,last_name,username,email,number,password,role);
168 return "redirect:/login";
169 }catch (PasswordsDoNotMatchException exception)
170 {
171 return "redirect:/register?error=" + exception.getMessage();
172 }
173
174 }
175
176
177 @GetMapping("/films")
178 @Transactional
179 public String getFilmsPage1(Model model,@RequestParam(required = false) Integer id_cinema){
180 model.addAttribute("cinemas",cinemaService.findAllCinemas());
181 if (id_cinema!=null) {
182 model.addAttribute("films",filmService.getFilmsFromCinema(id_cinema));
183 }else{
184 List<FilmsReturnTable> pom=new LinkedList<>();
185 model.addAttribute("films",filmService.findAllFilms());
186 }
187 model.addAttribute("bodyContent","films");
188 return "master-template";
189 }
190 @Transactional
191 @GetMapping("/projections")
192 public String getProjectionsPage(Model model,@RequestParam(required = false) Integer id_cinema)
193 {
194 model.addAttribute("cinemas",cinemaService.findAllCinemas());
195 if (id_cinema!=null) {
196 model.addAttribute("films",filmService.getFilmsFromCinemaNow(id_cinema));
197 }else{
198 List<FilmsReturnTable> pom=new LinkedList<>();
199 model.addAttribute("films",filmService.getFilmsNow());
200 }
201 model.addAttribute("bodyContent","projections");
202 return "master-template";
203 }
204 @GetMapping("/events")
205 @Transactional
206 public String getEventsPage(Model model,@RequestParam(required = false) Integer id_cinema)
207 {
208 model.addAttribute("cinemas",cinemaService.findAllCinemas());
209 if (id_cinema!=null) {
210 model.addAttribute("events",eventService.getEventsFromCinema(id_cinema));
211 }else{
212 List<FilmsReturnTable> pom=new LinkedList<>();
213 model.addAttribute("events",eventService.getEventsNow());
214 }
215 model.addAttribute("bodyContent","events");
216 return "master-template";
217 }
218 @GetMapping("/myTickets")
219 public String getMyTicketsPage(Model model,HttpSession session)
220 {
221 model.addAttribute("tickets",ticketService.findAllByCustomer((Customer) session.getAttribute("user")));
222 model.addAttribute("bodyContent","myTickets");
223 return "master-template";
224 }
225 @GetMapping("/addProjection")
226 public String getAddProjectionPage(Model model)
227 {
228 model.addAttribute("films",filmService.findAllFilms());
229 model.addAttribute("bodyContent","addProjection");
230 return "master-template";
231 }
232
233
234 @GetMapping("/addEvent")
235 public String getAddEventPage(Model model)
236 {
237 model.addAttribute("bodyContent","addEvent");
238 return "master-template";
239 }
240 @GetMapping("/addFilm")
241 public String getAddFilmPage(Model model)
242 {
243 model.addAttribute("bodyContent","addFilm");
244 return "master-template";
245 }
246
247 @PostMapping("/addP")
248 public String saveProjection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_start,
249 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_end,
250 @RequestParam String type_of_technology,
251 @RequestParam Integer id_film)
252 {
253 projectionService.save(date_time_start,date_time_end,type_of_technology,id_film);
254 return "redirect:/home";
255 }
256 @PostMapping("/addE")
257 public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
258 @RequestParam String theme,
259 @RequestParam String duration,
260 @RequestParam String img_url,
261 @RequestParam String repeating)
262 {
263 eventService.save(start_date,theme,duration,repeating,img_url);
264 return "redirect:/home";
265 }
266 @PostMapping("/addF")
267 public String saveFilm(
268 @RequestParam String name,
269 @RequestParam Integer duration,
270 @RequestParam String actors,
271 @RequestParam String genre,
272 @RequestParam String age_category,
273 @RequestParam String url,
274 @RequestParam String director,
275 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
276 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end_date
277 )
278 {
279 filmService.save(name,duration,actors,genre,age_category,url,director,start_date,end_date);
280 return "redirect:/home";
281 }
282
283 @GetMapping("/workers")
284 public String getWorkersPage(Model model)
285 {
286 model.addAttribute("workers",workerService.findAllWorkers());
287 model.addAttribute("bodyContent", "workers");
288 return "master-template";
289 }
290
291 @GetMapping("/addEventToCinema")
292 public String getCinemaOrganizesEventPage(Model model)
293 {
294 model.addAttribute("cinemas",cinemaService.findAllCinemas());
295 model.addAttribute("events",eventService.findAllEvents());
296 model.addAttribute("bodyContent","addEventToCinema");
297 return "master-template";
298 }
299 @PostMapping("/addCinemaOrganizesEvent")
300 public String saveCinemaOrganizesEvent(@RequestParam Integer id_cinema,
301 @RequestParam Integer id_event)
302 {
303
304 cinemaOrganizesEventService.save(id_cinema,id_event);
305 return "redirect:/home";
306 }
307 @GetMapping("/addFilmToCinema")
308 public String getCinemaPlaysFilmPage(Model model)
309 {
310 model.addAttribute("cinemas",cinemaService.findAllCinemas());
311 model.addAttribute("films",filmService.findAllFilms());
312 model.addAttribute("bodyContent","addFilmToCinema");
313 return "master-template";
314 }
315 @PostMapping("/addCinemaPlaysFilm")
316 public String saveCinemaPlaysFilm(@RequestParam Integer id_cinema,
317 @RequestParam Integer id_film)
318 {
319 cinemaPlaysFilmService.save(id_cinema,id_film);
320 return "redirect:/home";
321 }
322
323 @GetMapping("/getProjection/{id}")
324 public String getProjection(@PathVariable Integer id_projection,Model model)
325 {
326 List<Projection_Room> projectionRooms = null;
327 Projection projection=projectionService.findById(id_projection);
328
329
330 List<ProjectionIsPlayedInRoom> p= projectionIsPlayedInRoomService.getProjectionPlayedInRoom(id_projection);
331
332 model.addAttribute("projection",projection);
333 model.addAttribute("p_rooms",projectionRooms);
334 model.addAttribute("bodyContent","projectionDetails");
335 return "master-template";
336 }
337
338 @PostMapping("/makeReservation")
339 @Transactional
340 public String createTicketForReservation(@RequestParam Long film,@RequestParam Long projection,@RequestParam Long id_seat,@RequestParam String discount)
341 {
342 Ticket t;
343 Projection projection1=projectionService.findById(projection.intValue());
344 if(projection1.getDiscount().equals(discount)){
345 t=ticketService.saveWithDiscount(LocalDate.now(),customerService.getCustomerById(2).get(),projection1,projection1.getDiscount(),seatService.getSeatById(id_seat.intValue()).get());
346 }else{
347 t=ticketService.saveWithout(LocalDate.now(),customerService.getCustomerById(4).get(),projection1,seatService.getSeatById(id_seat.intValue()).get());
348 }
349 Integer price=ticketService.priceForTicket(t.getId_ticket());
350 t.setPrice(price);
351 return "redirect:/home";
352 }
353
354}
Note: See TracBrowser for help on using the repository browser.