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

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

reservation for projection started

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