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

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

update i homeController for getFilmPage1 method

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