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

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

Fixes

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