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

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

updates to login

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