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