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

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

Events and Event

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