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