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

Last change on this file since 86e187f was 86e187f, checked in by DenicaKj <dkorvezir@…>, 22 months ago

style

  • Property mode set to 100644
File size: 12.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.manytomany.ProjectionIsPlayedInRoom;
8import com.example.moviezone.repository.ProjectionIsPlayedInRoomRepository;
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.HttpSession;
19import javax.transaction.Transactional;
20import java.time.LocalDate;
21import java.util.LinkedList;
22import java.util.List;
23import java.util.stream.Collectors;
24
25@Controller
26@RequestMapping({"/","/home"})
27public class HomeController {
28
29private final FilmService filmService;
30private final UserService userService;
31private final ProjectionService projectionService;
32private final EventService eventService;
33private final TicketService ticketService;
34private final WorkerService workerService;
35private final CustomerRatesFilmService customerRatesFilmService;
36private final CinemaService cinemaService;
37private final CinemaOrganizesEventService cinemaOrganizesEventService;
38private final CinemaPlaysFilmService cinemaPlaysFilmService;
39private 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// public String getFilmsPage(Model model){
138// model.addAttribute("cinemas",cinemaService.findAllCinemas());
139// List<FilmsReturnTable> pom=new LinkedList<>();
140// model.addAttribute("films",pom);
141// boolean h=pom.isEmpty();
142// List<FilmsReturnTable> help=filmService.getFilmsFromCinema(2);
143// model.addAttribute("bodyContent","films");
144// return "master-template";
145// }
146
147 //@GetMapping("/films")
148 @Transactional
149 public String getFilmsPage(Model model){
150 model.addAttribute("cinemas",cinemaService.findAllCinemas());
151 List<FilmsReturnTable> pom=new LinkedList<>();
152 model.addAttribute("films",pom);
153 boolean h=pom.isEmpty();
154 List<FilmsReturnTable> help=filmService.getFilmsFromCinema(2);
155 model.addAttribute("bodyContent","films");
156 return "master-template";
157 }
158
159
160 @GetMapping("/films")
161 @Transactional
162 public String getFilmsPage1(Model model,@RequestParam(required = false) Integer id_cinema){
163 model.addAttribute("cinemas",cinemaService.findAllCinemas());
164 if (id_cinema!=null) {
165 model.addAttribute("films",filmService.getFilmsFromCinema(id_cinema));
166 }else{
167 List<FilmsReturnTable> pom=new LinkedList<>();
168 model.addAttribute("films",filmService.findAllFilms());
169 }
170 model.addAttribute("bodyContent","films");
171 return "master-template";
172 }
173
174// @PostMapping("/getFilmsFromCinema")
175// public String getFilmsFromCinema(@RequestParam Integer cinema, Model model){
176// return getFilmsPage1(model,cinema);
177// }
178 @GetMapping("/projections")
179 public String getProjectionsPage(Model model)
180 {
181 model.addAttribute("projections",projectionService.findAllProjections());
182 model.addAttribute("bodyContent","projections");
183 return "master-template";
184 }
185 @GetMapping("/events")
186 public String getEventsPage(Model model)
187 {
188 model.addAttribute("events",eventService.findAllEvents());
189 model.addAttribute("bodyContent","events");
190 return "master-template";
191 }
192 @GetMapping("/myTickets")
193 public String getMyTicketsPage(Model model,HttpSession session)
194 {
195 model.addAttribute("tickets",ticketService.findAllByCustomer((Customer) session.getAttribute("user")));
196 model.addAttribute("bodyContent","myTickets");
197 return "master-template";
198 }
199 @GetMapping("/addProjection")
200 public String getAddProjectionPage(Model model)
201 {
202 model.addAttribute("films",filmService.findAllFilms());
203 model.addAttribute("bodyContent","addProjection");
204 return "master-template";
205 }
206
207
208 @GetMapping("/addEvent")
209 public String getAddEventPage(Model model)
210 {
211 model.addAttribute("bodyContent","addEvent");
212 return "master-template";
213 }
214 @GetMapping("/addFilm")
215 public String getAddFilmPage(Model model)
216 {
217 model.addAttribute("bodyContent","addFilm");
218 return "master-template";
219 }
220
221 @PostMapping("/addP")
222 public String saveProjection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_start,
223 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_end,
224 @RequestParam String type_of_technology,
225 @RequestParam Integer id_film)
226 {
227 projectionService.save(date_time_start,date_time_end,type_of_technology,id_film);
228 return "redirect:/home";
229 }
230 @PostMapping("/addE")
231 public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
232 @RequestParam String theme,
233 @RequestParam String duration,
234 @RequestParam String img_url,
235 @RequestParam String repeating)
236 {
237 eventService.save(start_date,theme,duration,repeating,img_url);
238 return "redirect:/home";
239 }
240 @PostMapping("/addF")
241 public String saveFilm(
242 @RequestParam String name,
243 @RequestParam Integer duration,
244 @RequestParam String actors,
245 @RequestParam String genre,
246 @RequestParam String age_category,
247 @RequestParam String url,
248 @RequestParam String director,
249 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
250 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end_date
251 )
252 {
253 filmService.save(name,duration,actors,genre,age_category,url,director,start_date,end_date);
254 return "redirect:/home";
255 }
256
257 @GetMapping("/workers")
258 public String getWorkersPage(Model model)
259 {
260 model.addAttribute("workers",workerService.findAllWorkers());
261 model.addAttribute("bodyContent", "workers");
262 return "master-template";
263 }
264
265 @GetMapping("/addEventToCinema")
266 public String getCinemaOrganizesEventPage(Model model)
267 {
268 model.addAttribute("cinemas",cinemaService.findAllCinemas());
269 model.addAttribute("events",eventService.findAllEvents());
270 model.addAttribute("bodyContent","addEventToCinema");
271 return "master-template";
272 }
273 @PostMapping("/addCinemaOrganizesEvent")
274 public String saveCinemaOrganizesEvent(@RequestParam Integer id_cinema,
275 @RequestParam Integer id_event)
276 {
277
278 cinemaOrganizesEventService.save(id_cinema,id_event);
279 return "redirect:/home";
280 }
281 @GetMapping("/addFilmToCinema")
282 public String getCinemaPlaysFilmPage(Model model)
283 {
284 model.addAttribute("cinemas",cinemaService.findAllCinemas());
285 model.addAttribute("films",filmService.findAllFilms());
286 model.addAttribute("bodyContent","addFilmToCinema");
287 return "master-template";
288 }
289 @PostMapping("/addCinemaPlaysFilm")
290 public String saveCinemaPlaysFilm(@RequestParam Integer id_cinema,
291 @RequestParam Integer id_film)
292 {
293 cinemaPlaysFilmService.save(id_cinema,id_film);
294 return "redirect:/home";
295 }
296
297 @GetMapping("/getProjection/{id}")
298 public String getProjection(@PathVariable Integer id_projection,Model model)
299 {
300 List<Projection_Room> projectionRooms = null;
301 Projection projection=projectionService.findById(id_projection);
302
303
304 List<ProjectionIsPlayedInRoom> p= projectionIsPlayedInRoomService.getProjectionPlayedInRoom(id_projection);
305
306 model.addAttribute("projection",projection);
307 model.addAttribute("p_rooms",projectionRooms);
308 model.addAttribute("bodyContent","projectionDetails");
309 return "master-template";
310 }
311
312 @PostMapping("/makeReservation")
313 public String createTicketForReservation()
314 {
315 return "redirect:/myTickets";
316 }
317
318}
Note: See TracBrowser for help on using the repository browser.