source: src/main/java/com/example/moviezone/web/HomeController.java@ 7926d68

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

Profiles for user and worker

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