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

Last change on this file since 5444409 was 5444409, checked in by milamihajlovska <mila.mihajlovska01@…>, 21 months ago

update register and registerWorker

  • Property mode set to 100644
File size: 16.9 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,HttpSession session)
262 {
263 model.addAttribute("tickets",ticketService.findAllByCustomer((Customer) session.getAttribute("user")));
264 model.addAttribute("bodyContent","myTickets");
265 return "master-template";
266 }
267 @GetMapping("/addProjection")
268 public String getAddProjectionPage(Model model)
269 {
270 model.addAttribute("films",filmService.findAllFilms());
271 model.addAttribute("bodyContent","addProjection");
272 return "master-template";
273 }
274
275
276 @GetMapping("/addEvent")
277 public String getAddEventPage(Model model)
278 {
279 model.addAttribute("bodyContent","addEvent");
280 return "master-template";
281 }
282 @GetMapping("/addFilm")
283 public String getAddFilmPage(Model model)
284 {
285 model.addAttribute("bodyContent","addFilm");
286 return "master-template";
287 }
288
289 @PostMapping("/addP")
290 public String saveProjection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_start,
291 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_end,
292 @RequestParam String type_of_technology,
293 @RequestParam Integer id_film)
294 {
295 projectionService.save(date_time_start,date_time_end,type_of_technology,id_film);
296 return "redirect:/home";
297 }
298 @PostMapping("/addE")
299 public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
300 @RequestParam String theme,
301 @RequestParam String duration,
302 @RequestParam String img_url,
303 @RequestParam String repeating)
304 {
305 eventService.save(start_date,theme,duration,repeating,img_url);
306 return "redirect:/home";
307 }
308 @PostMapping("/addF")
309 public String saveFilm(
310 @RequestParam String name,
311 @RequestParam Integer duration,
312 @RequestParam String actors,
313 @RequestParam String genre,
314 @RequestParam String age_category,
315 @RequestParam String url,
316 @RequestParam String director,
317 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
318 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end_date
319 )
320 {
321 filmService.save(name,duration,actors,genre,age_category,url,director,start_date,end_date);
322 return "redirect:/home";
323 }
324
325 @GetMapping("/workers")
326 public String getWorkersPage(Model model)
327 {
328 model.addAttribute("workers",workerService.findAllWorkers());
329 model.addAttribute("bodyContent", "workers");
330 return "master-template";
331 }
332
333 @GetMapping("/addEventToCinema")
334 public String getCinemaOrganizesEventPage(Model model)
335 {
336 model.addAttribute("cinemas",cinemaService.findAllCinemas());
337 model.addAttribute("events",eventService.findAllEvents());
338 model.addAttribute("bodyContent","addEventToCinema");
339 return "master-template";
340 }
341 @PostMapping("/addCinemaOrganizesEvent")
342 public String saveCinemaOrganizesEvent(@RequestParam Integer id_cinema,
343 @RequestParam Integer id_event)
344 {
345
346 cinemaOrganizesEventService.save(id_cinema,id_event);
347 return "redirect:/home";
348 }
349 @GetMapping("/addFilmToCinema")
350 public String getCinemaPlaysFilmPage(Model model)
351 {
352 model.addAttribute("cinemas",cinemaService.findAllCinemas());
353 model.addAttribute("films",filmService.findAllFilms());
354 model.addAttribute("bodyContent","addFilmToCinema");
355 return "master-template";
356 }
357 @PostMapping("/addCinemaPlaysFilm")
358 public String saveCinemaPlaysFilm(@RequestParam Integer id_cinema,
359 @RequestParam Integer id_film)
360 {
361 cinemaPlaysFilmService.save(id_cinema,id_film);
362 return "redirect:/home";
363 }
364
365 @GetMapping("/getProjection/{id}")
366 public String getProjection(@PathVariable Integer id_projection,Model model)
367 {
368 List<Projection_Room> projectionRooms = null;
369 Projection projection=projectionService.findById(id_projection);
370
371
372 List<ProjectionIsPlayedInRoom> p= projectionIsPlayedInRoomService.getProjectionPlayedInRoom(id_projection);
373
374 model.addAttribute("projection",projection);
375 model.addAttribute("p_rooms",projectionRooms);
376 model.addAttribute("bodyContent","projectionDetails");
377 return "master-template";
378 }
379
380 @PostMapping("/makeReservation")
381 @Transactional
382 public String createTicketForReservation(@RequestParam Long film,@RequestParam Long projection,@RequestParam Long id_seat,@RequestParam String discount)
383 {
384 Ticket t;
385 Projection projection1=projectionService.findById(projection.intValue());
386 if(projection1.getDiscount().equals(discount)){
387 t=ticketService.saveWithDiscount(LocalDate.now(),customerService.getCustomerById(2).get(),projection1,projection1.getDiscount(),seatService.getSeatById(id_seat.intValue()).get());
388 }else{
389 t=ticketService.saveWithout(LocalDate.now(),customerService.getCustomerById(4).get(),projection1,seatService.getSeatById(id_seat.intValue()).get());
390 }
391 Integer price=ticketService.priceForTicket(t.getId_ticket());
392 t.setPrice(price);
393 return "redirect:/home";
394 }
395
396}
Note: See TracBrowser for help on using the repository browser.