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

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

Merge branch 'master' of https://github.com/DenicaKj/MovieZone

  • 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, ProjectionIsPlayedInRoomRepository projectionIsPlayedInRoomRepository)
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 public String getFilmsPage1(Model model,Integer id_cinema){
147 model.addAttribute("cinemas",cinemaService.findAllCinemas());
148 if (id_cinema!=null) {
149 model.addAttribute("films",filmService.getFilmsFromCinema(id_cinema.intValue()));
150 }else{
151 List<FilmsReturnTable> pom=new LinkedList<>();
152 model.addAttribute("films",pom);
153 }
154
155 model.addAttribute("bodyContent","films");
156 return "master-template";
157 }
158 @PostMapping("/getFilmsFromCinema")
159 public String getFilmsFromCinema(@RequestParam Integer cinema, Model model){
160 return getFilmsPage1(model,cinema);
161 }
162 @GetMapping("/projections")
163 public String getProjectionsPage(Model model)
164 {
165 model.addAttribute("projections",projectionService.findAllProjections());
166 model.addAttribute("bodyContent","projections");
167 return "master-template";
168 }
169 @GetMapping("/events")
170 public String getEventsPage(Model model)
171 {
172 model.addAttribute("events",eventService.findAllEvents());
173 model.addAttribute("bodyContent","events");
174 return "master-template";
175 }
176 @GetMapping("/myTickets")
177 public String getMyTicketsPage(Model model,HttpSession session)
178 {
179 model.addAttribute("tickets",ticketService.findAllByCustomer((Customer) session.getAttribute("user")));
180 model.addAttribute("bodyContent","myTickets");
181 return "master-template";
182 }
183 @GetMapping("/addProjection")
184 public String getAddProjectionPage(Model model)
185 {
186 model.addAttribute("films",filmService.findAllFilms());
187 model.addAttribute("bodyContent","addProjection");
188 return "master-template";
189 }
190
191
192 @GetMapping("/addEvent")
193 public String getAddEventPage(Model model)
194 {
195 model.addAttribute("bodyContent","addEvent");
196 return "master-template";
197 }
198 @GetMapping("/addFilm")
199 public String getAddFilmPage(Model model)
200 {
201 model.addAttribute("bodyContent","addFilm");
202 return "master-template";
203 }
204
205 @PostMapping("/addP")
206 public String saveProjection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_start,
207 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_end,
208 @RequestParam String type_of_technology,
209 @RequestParam Integer id_film)
210 {
211 projectionService.save(date_time_start,date_time_end,type_of_technology,id_film);
212 return "redirect:/home";
213 }
214 @PostMapping("/addE")
215 public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
216 @RequestParam String theme,
217 @RequestParam String duration,
218 @RequestParam String img_url,
219 @RequestParam String repeating)
220 {
221 eventService.save(start_date,theme,duration,repeating,img_url);
222 return "redirect:/home";
223 }
224 @PostMapping("/addF")
225 public String saveFilm(
226 @RequestParam String name,
227 @RequestParam Integer duration,
228 @RequestParam String actors,
229 @RequestParam String genre,
230 @RequestParam String age_category,
231 @RequestParam String url,
232 @RequestParam String director,
233 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
234 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end_date
235 )
236 {
237 filmService.save(name,duration,actors,genre,age_category,url,director,start_date,end_date);
238 return "redirect:/home";
239 }
240
241 @GetMapping("/workers")
242 public String getWorkersPage(Model model)
243 {
244 model.addAttribute("workers",workerService.findAllWorkers());
245 model.addAttribute("bodyContent", "workers");
246 return "master-template";
247 }
248
249 @GetMapping("/addEventToCinema")
250 public String getCinemaOrganizesEventPage(Model model)
251 {
252 model.addAttribute("cinemas",cinemaService.findAllCinemas());
253 model.addAttribute("events",eventService.findAllEvents());
254 model.addAttribute("bodyContent","addEventToCinema");
255 return "master-template";
256 }
257 @PostMapping("/addCinemaOrganizesEvent")
258 public String saveCinemaOrganizesEvent(@RequestParam Integer id_cinema,
259 @RequestParam Integer id_event)
260 {
261
262 cinemaOrganizesEventService.save(id_cinema,id_event);
263 return "redirect:/home";
264 }
265 @GetMapping("/addFilmToCinema")
266 public String getCinemaPlaysFilmPage(Model model)
267 {
268 model.addAttribute("cinemas",cinemaService.findAllCinemas());
269 model.addAttribute("films",filmService.findAllFilms());
270 model.addAttribute("bodyContent","addFilmToCinema");
271 return "master-template";
272 }
273 @PostMapping("/addCinemaPlaysFilm")
274 public String saveCinemaPlaysFilm(@RequestParam Integer id_cinema,
275 @RequestParam Integer id_film)
276 {
277 cinemaPlaysFilmService.save(id_cinema,id_film);
278 return "redirect:/home";
279 }
280
281 @GetMapping("/getProjection/{id}")
282 public String getProjection(@PathVariable Integer id_projection,Model model)
283 {
284 List<Projection_Room> projectionRooms = null;
285 Projection projection=projectionService.findById(id_projection);
286
287
288 List<ProjectionIsPlayedInRoom> p= projectionIsPlayedInRoomService.getProjectionPlayedInRoom(id_projection);
289
290 model.addAttribute("projection",projection);
291 model.addAttribute("p_rooms",projectionRooms);
292 model.addAttribute("bodyContent","projectionDetails");
293 return "master-template";
294 }
295
296 @PostMapping("/makeReservation")
297 public String createTicketForReservation()
298 {
299 return "redirect:/myTickets";
300 }
301
302}
Note: See TracBrowser for help on using the repository browser.