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

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

Merge branch 'master' of https://github.com/DenicaKj/MovieZone

  • 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// public String getFilmsPage1(Model model,@RequestParam(required = false) Integer id_cinema){
162// model.addAttribute("cinemas",cinemaService.findAllCinemas());
163// if (id_cinema!=null) {
164// model.addAttribute("films",filmService.getFilmsFromCinema(id_cinema));
165// }else{
166//// List<FilmsReturnTable> pom=new LinkedList<>();
167// model.addAttribute("films",filmService.findAllFilms());
168// }
169// model.addAttribute("bodyContent","films");
170// return "master-template";
171// }
172
173// @PostMapping("/getFilmsFromCinema")
174// public String getFilmsFromCinema(@RequestParam Integer cinema, Model model){
175// return getFilmsPage1(model,cinema);
176// }
177 @GetMapping("/projections")
178 public String getProjectionsPage(Model model)
179 {
180 model.addAttribute("projections",projectionService.findAllProjections());
181 model.addAttribute("bodyContent","projections");
182 return "master-template";
183 }
184 @GetMapping("/events")
185 public String getEventsPage(Model model)
186 {
187 model.addAttribute("events",eventService.findAllEvents());
188 model.addAttribute("bodyContent","events");
189 return "master-template";
190 }
191 @GetMapping("/myTickets")
192 public String getMyTicketsPage(Model model,HttpSession session)
193 {
194 model.addAttribute("tickets",ticketService.findAllByCustomer((Customer) session.getAttribute("user")));
195 model.addAttribute("bodyContent","myTickets");
196 return "master-template";
197 }
198 @GetMapping("/addProjection")
199 public String getAddProjectionPage(Model model)
200 {
201 model.addAttribute("films",filmService.findAllFilms());
202 model.addAttribute("bodyContent","addProjection");
203 return "master-template";
204 }
205
206
207 @GetMapping("/addEvent")
208 public String getAddEventPage(Model model)
209 {
210 model.addAttribute("bodyContent","addEvent");
211 return "master-template";
212 }
213 @GetMapping("/addFilm")
214 public String getAddFilmPage(Model model)
215 {
216 model.addAttribute("bodyContent","addFilm");
217 return "master-template";
218 }
219
220 @PostMapping("/addP")
221 public String saveProjection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_start,
222 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_end,
223 @RequestParam String type_of_technology,
224 @RequestParam Integer id_film)
225 {
226 projectionService.save(date_time_start,date_time_end,type_of_technology,id_film);
227 return "redirect:/home";
228 }
229 @PostMapping("/addE")
230 public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
231 @RequestParam String theme,
232 @RequestParam String duration,
233 @RequestParam String img_url,
234 @RequestParam String repeating)
235 {
236 eventService.save(start_date,theme,duration,repeating,img_url);
237 return "redirect:/home";
238 }
239 @PostMapping("/addF")
240 public String saveFilm(
241 @RequestParam String name,
242 @RequestParam Integer duration,
243 @RequestParam String actors,
244 @RequestParam String genre,
245 @RequestParam String age_category,
246 @RequestParam String url,
247 @RequestParam String director,
248 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
249 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end_date
250 )
251 {
252 filmService.save(name,duration,actors,genre,age_category,url,director,start_date,end_date);
253 return "redirect:/home";
254 }
255
256 @GetMapping("/workers")
257 public String getWorkersPage(Model model)
258 {
259 model.addAttribute("workers",workerService.findAllWorkers());
260 model.addAttribute("bodyContent", "workers");
261 return "master-template";
262 }
263
264 @GetMapping("/addEventToCinema")
265 public String getCinemaOrganizesEventPage(Model model)
266 {
267 model.addAttribute("cinemas",cinemaService.findAllCinemas());
268 model.addAttribute("events",eventService.findAllEvents());
269 model.addAttribute("bodyContent","addEventToCinema");
270 return "master-template";
271 }
272 @PostMapping("/addCinemaOrganizesEvent")
273 public String saveCinemaOrganizesEvent(@RequestParam Integer id_cinema,
274 @RequestParam Integer id_event)
275 {
276
277 cinemaOrganizesEventService.save(id_cinema,id_event);
278 return "redirect:/home";
279 }
280 @GetMapping("/addFilmToCinema")
281 public String getCinemaPlaysFilmPage(Model model)
282 {
283 model.addAttribute("cinemas",cinemaService.findAllCinemas());
284 model.addAttribute("films",filmService.findAllFilms());
285 model.addAttribute("bodyContent","addFilmToCinema");
286 return "master-template";
287 }
288 @PostMapping("/addCinemaPlaysFilm")
289 public String saveCinemaPlaysFilm(@RequestParam Integer id_cinema,
290 @RequestParam Integer id_film)
291 {
292 cinemaPlaysFilmService.save(id_cinema,id_film);
293 return "redirect:/home";
294 }
295
296 @GetMapping("/getProjection/{id}")
297 public String getProjection(@PathVariable Integer id_projection,Model model)
298 {
299 List<Projection_Room> projectionRooms = null;
300 Projection projection=projectionService.findById(id_projection);
301
302
303 List<ProjectionIsPlayedInRoom> p= projectionIsPlayedInRoomService.getProjectionPlayedInRoom(id_projection);
304
305 model.addAttribute("projection",projection);
306 model.addAttribute("p_rooms",projectionRooms);
307 model.addAttribute("bodyContent","projectionDetails");
308 return "master-template";
309 }
310
311 @PostMapping("/makeReservation")
312 public String createTicketForReservation()
313 {
314 return "redirect:/myTickets";
315 }
316
317}
Note: See TracBrowser for help on using the repository browser.